Skip to content

Commit aef576a

Browse files
committed
more streamlined
1 parent 66c9653 commit aef576a

3 files changed

Lines changed: 20 additions & 375 deletions

File tree

scripts/suggest-optimize.sh

Lines changed: 1 addition & 369 deletions
Original file line numberDiff line numberDiff line change
@@ -111,37 +111,6 @@ if [ -f "$SEEN_MARKER" ] && grep -qF "$COMMIT_HASH" "$SEEN_MARKER" 2>/dev/null;
111111
fi
112112
echo "$COMMIT_HASH" >> "$SEEN_MARKER"
113113

114-
# --- From here on, we know there are new commits to optimize ---
115-
116-
# --- Check if CODEFLASH_API_KEY is available ---
117-
OAUTH_SCRIPT="$(dirname "$0")/oauth-login.sh"
118-
119-
has_api_key() {
120-
# Check env var
121-
if [ -n "${CODEFLASH_API_KEY:-}" ] && [[ "${CODEFLASH_API_KEY}" == cf-* ]]; then
122-
return 0
123-
fi
124-
# Check Unix shell RC files
125-
for rc in "$HOME/.zshrc" "$HOME/.bashrc" "$HOME/.profile" "$HOME/.kshrc" "$HOME/.cshrc"; do
126-
if [ -f "$rc" ] && grep -q '^export CODEFLASH_API_KEY="cf-' "$rc" 2>/dev/null; then
127-
return 0
128-
fi
129-
done
130-
# Check Windows-specific files (PowerShell / CMD, matching codeflash CLI)
131-
for rc in "$HOME/codeflash_env.ps1" "$HOME/codeflash_env.bat"; do
132-
if [ -f "$rc" ] && grep -q 'CODEFLASH_API_KEY.*cf-' "$rc" 2>/dev/null; then
133-
return 0
134-
fi
135-
done
136-
return 1
137-
}
138-
139-
LOGIN_STEP=""
140-
if ! has_api_key; then
141-
LOGIN_STEP="
142-
- Run \`${OAUTH_SCRIPT}\` to log in to Codeflash. If it exits with code 0, the key is saved. If it exits with code 2 (headless environment), parse the JSON output for the \`url\` and \`state_file\`, ask the user to visit the URL and provide the authorization code, then run \`${OAUTH_SCRIPT} --exchange-code <state_file> <code>\` to complete the login."
143-
fi
144-
145114
# Walk from $PWD upward to $REPO_ROOT looking for project config.
146115
# Sets: PROJECT_TYPE, PROJECT_DIR, PROJECT_CONFIG_PATH, PROJECT_CONFIGURED
147116
detect_project() {
@@ -151,16 +120,6 @@ detect_project() {
151120
PROJECT_CONFIGURED="false"
152121
local search_dir="$PWD"
153122
while true; do
154-
# Check codeflash.toml first (Java projects)
155-
if [ -f "$search_dir/codeflash.toml" ]; then
156-
PROJECT_TYPE="java"
157-
PROJECT_DIR="$search_dir"
158-
PROJECT_CONFIG_PATH="$search_dir/codeflash.toml"
159-
if grep -q '\[tool\.codeflash\]' "$search_dir/codeflash.toml" 2>/dev/null; then
160-
PROJECT_CONFIGURED="true"
161-
fi
162-
break
163-
fi
164123
if [ -f "$search_dir/pyproject.toml" ]; then
165124
PROJECT_TYPE="python"
166125
PROJECT_DIR="$search_dir"
@@ -197,171 +156,9 @@ detect_project() {
197156
# Discover project config
198157
detect_project
199158

200-
CHECK_DIR="${PROJECT_DIR:-$PWD}"
201-
202159
# --- JS/TS project path ---------------------------------------------------
203160
if [ "$PROJECT_TYPE" = "js" ] && [ "$HAS_JS_CHANGES" = "true" ]; then
204-
INSTALL_CMD="npm install --save-dev codeflash"
205-
206-
# Check if codeflash npm package is available
207-
CODEFLASH_INSTALLED="false"
208-
if (cd "$CHECK_DIR" && npx codeflash --version >/dev/null 2>&1); then
209-
CODEFLASH_INSTALLED="true"
210-
fi
211-
212-
# Not configured → prompt for config (+ install if needed)
213-
if [ "$PROJECT_CONFIGURED" != "true" ]; then
214-
if [ -n "$PROJECT_CONFIG_PATH" ]; then
215-
TARGET_MSG="Add a \`\"codeflash\"\` key to \`$PROJECT_CONFIG_PATH\`"
216-
else
217-
TARGET_MSG="Create \`$REPO_ROOT/package.json\` (via \`npm init -y\`) and add a \`\"codeflash\"\` key"
218-
fi
219-
220-
INSTALL_STEP=""
221-
if [ "$CODEFLASH_INSTALLED" != "true" ]; then
222-
INSTALL_STEP="
223-
- First, install codeflash by running \`${INSTALL_CMD}\` in \`${CHECK_DIR}\`."
224-
fi
225-
226-
SETUP_MSG="JS/TS files were changed in a recent commit, but codeflash is not configured yet.
227-
228-
Set up codeflash to optimize the JavaScript/TypeScript code for performance. Automatically determine the following by exploring the project's actual directory structure using Glob and Read (do NOT present options to the user — discover the correct paths yourself):
229-
1. The relative path with respect to package.json to the root of the JS/TS module — typically \".\" for the root directory or \"src\".
230-
2. The relative path with respect to package.json to the tests folder — look for existing directories named \`tests\`, \`test\`, \`__tests__\`, or folders containing files that start with \`test_\` or end with \`.test.js\`/\`.spec.ts\`. If none exist, default to \`tests\`.
231-
232-
Once determined:${INSTALL_STEP}
233-
- If the tests folder does not exist, create it with mkdir -p.
234-
- ${TARGET_MSG}. Read the existing package.json, parse it, add/update the \"codeflash\" key with the discovered values, and write back with 2-space indentation:
235-
236-
{
237-
\"codeflash\": {
238-
\"moduleRoot\": \"<module root>\",
239-
\"testsRoot\": \"<tests folder>\",
240-
\"formatterCmds\": [\"disabled\"],
241-
\"ignorePaths\": [\"dist\", \"**/node_modules\", \"**/__tests__\"]
242-
}
243-
}
244-
245-
Once the configuration is written, run \`npx codeflash --subagent\` as a non-blocking background Bash task (run_in_background: true) with a 10min timeout."
246-
247-
# # Append auto-allow setup instructions if not already configured
248-
# if [ "$CODEFLASH_AUTO_ALLOWED" != "true" ]; then
249-
# SETUP_MSG="${SETUP_MSG}
250-
#
251-
#Then, add \`Bash(*codeflash*)\` to the \`permissions.allow\` array in \`$SETTINGS_JSON\` (create the file and \`.claude\` directory if they don't exist, and preserve any existing settings). This allows codeflash to run automatically in the background without prompting for permission each time."
252-
# fi
253-
254-
jq -nc --arg reason "$SETUP_MSG" '{"decision": "block", "reason": $reason, "systemMessage": $reason}'
255-
exit 0
256-
fi
257-
258-
# Configured but not installed
259-
if [ "$CODEFLASH_INSTALLED" != "true" ]; then
260-
INSTALL_MSG="JS/TS files were changed in a recent commit, but the codeflash npm package is not installed.
261-
262-
Install codeflash to optimize the JavaScript/TypeScript code for performance by running:
263-
264-
${INSTALL_CMD}
265-
266-
in \`${CHECK_DIR}\`, then run \`npx codeflash --subagent\` as a non-blocking background Bash task (run_in_background: true) with a 10min timeout."
267-
268-
# # Append auto-allow setup instructions if not already configured
269-
# if [ "$CODEFLASH_AUTO_ALLOWED" != "true" ]; then
270-
# INSTALL_MSG="${INSTALL_MSG}
271-
#
272-
#Then, add \`Bash(*codeflash*)\` to the \`permissions.allow\` array in \`$SETTINGS_JSON\` (create the file and \`.claude\` directory if they don't exist, and preserve any existing settings). This allows codeflash to run automatically in the background without prompting for permission each time."
273-
# fi
274-
275-
jq -nc --arg reason "$INSTALL_MSG" '{"decision": "block", "reason": $reason, "systemMessage": $reason}'
276-
exit 0
277-
fi
278-
279-
# Build codeflash command for JS/TS — must run from project root
280-
if [ -n "$PROJECT_DIR" ] && [ "$PROJECT_DIR" != "$PWD" ]; then
281-
RUN_CMD="cd $PROJECT_DIR && npx codeflash --subagent"
282-
else
283-
RUN_CMD="npx codeflash --subagent"
284-
fi
285-
286-
MESSAGE="JS/TS files were changed in a recent commit. Use the codeflash:optimize skill WITHOUT ANY ARGUMENTS to to optimize the JavaScript/TypeScript code for performance."
287-
288-
# # Append auto-allow setup instructions if not already configured
289-
# if [ "$CODEFLASH_AUTO_ALLOWED" != "true" ]; then
290-
# MESSAGE="${MESSAGE}
291-
#
292-
#Then, add \`Bash(*codeflash*)\` to the \`permissions.allow\` array in \`$SETTINGS_JSON\` (create the file and \`.claude\` directory if they don't exist, and preserve any existing settings). This allows codeflash to run automatically in the background without prompting for permission each time."
293-
# fi
294-
295-
jq -nc --arg reason "$MESSAGE" '{"decision": "block", "reason": $reason, "systemMessage": $reason}'
296-
exit 0
297-
fi
298-
299-
# --- Java project path -----------------------------------------------------
300-
if [ "$PROJECT_TYPE" = "java" ]; then
301-
INSTALL_CMD="pip install codeflash"
302-
303-
# Find codeflash binary: check PATH first, then uv run (Java projects don't use venvs)
304-
CODEFLASH_BIN=""
305-
CODEFLASH_INSTALLED="false"
306-
if command -v codeflash >/dev/null 2>&1; then
307-
CODEFLASH_BIN="codeflash"
308-
CODEFLASH_INSTALLED="true"
309-
elif uv run codeflash --version >/dev/null 2>&1; then
310-
CODEFLASH_BIN="uv run codeflash"
311-
CODEFLASH_INSTALLED="true"
312-
fi
313-
314-
# Not installed
315-
if [ "$CODEFLASH_INSTALLED" != "true" ]; then
316-
INSTALL_MSG="Java files were changed in a recent commit, but codeflash is not installed.
317-
318-
Install codeflash to optimize the Java code for performance by running:
319-
320-
${INSTALL_CMD}
321-
322-
Then run \`codeflash --subagent\` as a non-blocking background Bash task (run_in_background: true) with a 10min timeout."
323-
324-
# if [ "$CODEFLASH_AUTO_ALLOWED" != "true" ]; then
325-
# INSTALL_MSG="${INSTALL_MSG}
326-
#
327-
#Then, add \`Bash(*codeflash*)\` to the \`permissions.allow\` array in \`$SETTINGS_JSON\` (create the file and \`.claude\` directory if they don't exist, and preserve any existing settings). This allows codeflash to run automatically in the background without prompting for permission each time."
328-
# fi
329-
330-
jq -nc --arg reason "$INSTALL_MSG" '{"decision": "block", "reason": $reason, "systemMessage": $reason}'
331-
exit 0
332-
fi
333-
334-
# Not configured
335-
if [ "$PROJECT_CONFIGURED" != "true" ]; then
336-
SETUP_MSG="Java files were changed in a recent commit, but codeflash is not configured yet.
337-
338-
Run \`${CODEFLASH_BIN} init --yes\` in \`${CHECK_DIR}\` to automatically detect and configure the Java project, then run \`${CODEFLASH_BIN} --subagent\` as a non-blocking background Bash task (run_in_background: true) with a 10min timeout to optimize the code for performance."
339-
340-
# if [ "$CODEFLASH_AUTO_ALLOWED" != "true" ]; then
341-
# SETUP_MSG="${SETUP_MSG}
342-
#
343-
#Then, add \`Bash(*codeflash*)\` to the \`permissions.allow\` array in \`$SETTINGS_JSON\` (create the file and \`.claude\` directory if they don't exist, and preserve any existing settings). This allows codeflash to run automatically in the background without prompting for permission each time."
344-
# fi
345-
346-
jq -nc --arg reason "$SETUP_MSG" '{"decision": "block", "reason": $reason, "systemMessage": $reason}'
347-
exit 0
348-
fi
349-
350-
# Everything ready — build codeflash command for Java
351-
if [ -n "$PROJECT_DIR" ] && [ "$PROJECT_DIR" != "$PWD" ]; then
352-
RUN_CMD="cd $PROJECT_DIR && $CODEFLASH_BIN --subagent"
353-
else
354-
RUN_CMD="$CODEFLASH_BIN --subagent"
355-
fi
356-
357-
MESSAGE="Java files were changed in a recent commit. Use the codeflash:optimize skill WITHOUT ANY ARGUMENTS to to optimize the Java code for performance."
358-
359-
# if [ "$CODEFLASH_AUTO_ALLOWED" != "true" ]; then
360-
# MESSAGE="${MESSAGE}
361-
#
362-
#Then, add \`Bash(*codeflash*)\` to the \`permissions.allow\` array in \`$SETTINGS_JSON\` (create the file and \`.claude\` directory if they don't exist, and preserve any existing settings). This allows codeflash to run automatically in the background without prompting for permission each time."
363-
# fi
364-
161+
MESSAGE="JS/TS files were changed in a recent commit. Use the codeflash:optimize skill WITHOUT ANY ARGUMENTS to to optimize the JavaScript/TypeScript code for performance. Use npx to execute codeflash"
365162
jq -nc --arg reason "$MESSAGE" '{"decision": "block", "reason": $reason, "systemMessage": $reason}'
366163
exit 0
367164
fi
@@ -371,171 +168,6 @@ if [ "$HAS_PYTHON_CHANGES" != "true" ]; then
371168
exit 0
372169
fi
373170

374-
INSTALL_CMD="pip install codeflash"
375-
376-
# Only use codeflash from the current virtual environment.
377-
# If no venv is active, try to find and activate one automatically.
378-
# shellcheck disable=SC1091
379-
source "$(dirname "$0")/find-venv.sh"
380-
381-
if [ -z "${VIRTUAL_ENV:-}" ]; then
382-
# No venv found — instruct Claude to create one, install codeflash, configure, and run
383-
384-
# Build config setup instructions if not configured
385-
PYPROJECT_SETUP=""
386-
if [ "$PROJECT_CONFIGURED" != "true" ]; then
387-
if [ -n "$PROJECT_CONFIG_PATH" ]; then
388-
TARGET_MSG="Append to \`$PROJECT_CONFIG_PATH\`"
389-
else
390-
TARGET_MSG="Create \`$REPO_ROOT/pyproject.toml\`"
391-
fi
392-
393-
PYPROJECT_SETUP="
394-
3. Automatically determine the following two values by exploring the project's actual directory structure using Glob and Read (do NOT present options to the user — discover the correct paths yourself):
395-
a. The relative path with respect to pyproject.toml to the root of the Python module — the module root is where tests import from. for example, if the module root is abc/ then the tests would be importing code as \`from abc import xyz\`.
396-
b. The relative path with respect to pyproject.toml to the tests folder — look for existing directories named \`tests\` or \`test\`, or folders containing files that start with \`test_\`. If none exist, default to \`tests\`.
397-
398-
4. Once determined:
399-
- If the tests folder does not exist, create it with mkdir -p.
400-
- ${TARGET_MSG} (create the file if it does not exist), adding the following section with the discovered values substituted:
401-
402-
[tool.codeflash]
403-
# All paths are relative to this pyproject.toml's directory.
404-
module-root = \"<module root>\"
405-
tests-root = \"<tests folder>\"
406-
ignore-paths = []
407-
formatter-cmds = [\"disabled\"]
408-
409-
5. Run \`codeflash --subagent\` as a non-blocking background Bash task (run_in_background: true) with a 10min timeout."
410-
else
411-
PYPROJECT_SETUP="
412-
3. Run \`codeflash --subagent\` as a non-blocking background Bash task (run_in_background: true) with a 10min timeout."
413-
fi
414-
415-
# # Build auto-allow setup step if not already configured
416-
# SETUP_PERMISSIONS_STEP=""
417-
# if [ "$CODEFLASH_AUTO_ALLOWED" != "true" ]; then
418-
# SETUP_PERMISSIONS_STEP="
419-
#Then, add \`Bash(*codeflash*)\` to the \`permissions.allow\` array in \`$SETTINGS_JSON\` (create the file and \`.claude\` directory if they don't exist, and preserve any existing settings). This allows codeflash to run automatically in the background without prompting for permission each time."
420-
# fi
421-
422-
VENV_MSG="Python files were changed in a recent commit, but no Python virtual environment was found.
423-
424-
Set up codeflash to optimize the Python code for performance. Perform the following steps:
425-
${LOGIN_STEP}
426-
1. Create a virtual environment by running: \`python3 -m venv .venv\` in \`$REPO_ROOT\`, then activate it by running: \`source $REPO_ROOT/.venv/bin/activate\`.
427-
428-
2. Install codeflash by running: \`${INSTALL_CMD}\`.
429-
${PYPROJECT_SETUP}
430-
${SETUP_PERMISSIONS_STEP}"
431-
432-
jq -nc --arg reason "$VENV_MSG" '{"decision": "block", "reason": $reason, "systemMessage": $reason}'
433-
exit 0
434-
fi
435-
436-
CODEFLASH_BIN="${VIRTUAL_ENV}/bin/codeflash"
437-
438-
# Check if codeflash is installed in the venv
439-
CODEFLASH_INSTALLED="false"
440-
if [ -x "$CODEFLASH_BIN" ] && "$CODEFLASH_BIN" --version >/dev/null 2>&1; then
441-
CODEFLASH_INSTALLED="true"
442-
fi
443-
444-
# Check if codeflash is configured in this project
445-
if [ "$PROJECT_CONFIGURED" != "true" ]; then
446-
# Build a human-friendly target path for the setup message
447-
if [ -n "$PROJECT_CONFIG_PATH" ]; then
448-
TARGET_MSG="Append to \`$PROJECT_CONFIG_PATH\`"
449-
else
450-
TARGET_MSG="Create \`$REPO_ROOT/pyproject.toml\`"
451-
fi
452-
453-
# Include install step if codeflash is not installed
454-
INSTALL_STEP=""
455-
if [ "$CODEFLASH_INSTALLED" != "true" ]; then
456-
INSTALL_STEP="
457-
- First, install codeflash by running \`${INSTALL_CMD}\` in \`${CHECK_DIR}\`."
458-
fi
459-
460-
SETUP_MSG="Python files were changed in a recent commit, but codeflash is not configured yet.
461-
462-
Set up codeflash to optimize the Python code for performance:
463-
${LOGIN_STEP}
464-
Automatically determine the following by exploring the project's actual directory structure using Glob and Read (do NOT present options to the user — discover the correct paths yourself):
465-
1. The relative path with respect to pyproject.toml to the root of the Python module — the module root is where tests import from. for example, if the module root is abc/ then the tests would be importing code as \`from abc import xyz\`.
466-
2. The relative path with respect to pyproject.toml to the tests folder — look for existing directories named \`tests\` or \`test\`, or folders containing files that start with \`test_\`. If none exist, default to \`tests\`.
467-
468-
Once determined:${INSTALL_STEP}
469-
- If the tests folder does not exist, create it with mkdir -p.
470-
- ${TARGET_MSG} (create the file if it does not exist), adding the following section with the discovered values substituted:
471-
472-
[tool.codeflash]
473-
# All paths are relative to this pyproject.toml's directory.
474-
module-root = \"<module root>\"
475-
tests-root = \"<tests folder>\"
476-
ignore-paths = []
477-
formatter-cmds = [\"disabled\"]
478-
479-
Once the pyproject.toml configuration is written, run \`codeflash --subagent\` as a non-blocking background Bash task (run_in_background: true) with a 10min timeout."
480-
481-
# # Append auto-allow setup instructions if not already configured
482-
# if [ "$CODEFLASH_AUTO_ALLOWED" != "true" ]; then
483-
# SETUP_MSG="${SETUP_MSG}
484-
#
485-
#Then, add \`Bash(*codeflash*)\` to the \`permissions.allow\` array in \`$SETTINGS_JSON\` (create the file and \`.claude\` directory if they don't exist, and preserve any existing settings). This allows codeflash to run automatically in the background without prompting for permission each time."
486-
# fi
487-
488-
jq -nc --arg reason "$SETUP_MSG" '{"decision": "block", "reason": $reason, "systemMessage": $reason}'
489-
exit 0
490-
fi
491-
492-
# Codeflash is configured but not installed in the venv
493-
if [ "$CODEFLASH_INSTALLED" != "true" ]; then
494-
INSTALL_MSG="Python files were changed in a recent commit, but codeflash is not installed in the active virtual environment (\`$VIRTUAL_ENV\`).
495-
${LOGIN_STEP}
496-
Install codeflash to optimize the Python code for performance by running:
497-
498-
${INSTALL_CMD}
499-
500-
in \`${CHECK_DIR}\`, then run \`codeflash --subagent\` as a non-blocking background Bash task (run_in_background: true) with a 10min timeout."
501-
502-
# # Append auto-allow setup instructions if not already configured
503-
# if [ "$CODEFLASH_AUTO_ALLOWED" != "true" ]; then
504-
# INSTALL_MSG="${INSTALL_MSG}
505-
#
506-
#Then, add \`Bash(*codeflash*)\` to the \`permissions.allow\` array in \`$SETTINGS_JSON\` (create the file and \`.claude\` directory if they don't exist, and preserve any existing settings). This allows codeflash to run automatically in the background without prompting for permission each time."
507-
# fi
508-
509-
jq -nc --arg reason "$INSTALL_MSG" '{"decision": "block", "reason": $reason, "systemMessage": $reason}'
510-
exit 0
511-
fi
512-
513-
# Check for API key before running codeflash
514-
if ! has_api_key; then
515-
LOGIN_MSG="Python files were changed in a recent commit, but no Codeflash API key was found.
516-
517-
Run \`${OAUTH_SCRIPT}\` to log in to Codeflash. If it exits with code 0, the key is saved. If it exits with code 2 (headless environment), parse the JSON output for the \`url\` and \`state_file\`, ask the user to visit the URL and provide the authorization code, then run \`${OAUTH_SCRIPT} --exchange-code <state_file> <code>\` to complete the login.
518-
519-
After login, run \`codeflash --subagent\` as a non-blocking background Bash task (run_in_background: true) with a 10min timeout to optimize the code."
520-
521-
jq -nc --arg reason "$LOGIN_MSG" '{"decision": "block", "reason": $reason}'
522-
exit 0
523-
fi
524-
525-
# Instruct Claude to run codeflash as a background subagent
526-
if [ -n "$PROJECT_DIR" ] && [ "$PROJECT_DIR" != "$PWD" ]; then
527-
RUN_CMD="cd $PROJECT_DIR && $CODEFLASH_BIN --subagent"
528-
else
529-
RUN_CMD="$CODEFLASH_BIN --subagent"
530-
fi
531-
532171
MESSAGE="Python files were changed in a recent commit. Use the codeflash:optimize skill WITHOUT ANY ARGUMENTS to to optimize the Python code for performance."
533172

534-
## Append auto-allow setup instructions if not already configured
535-
#if [ "$CODEFLASH_AUTO_ALLOWED" != "true" ]; then
536-
# MESSAGE="${MESSAGE}
537-
#
538-
#Then, add \`Bash(*codeflash*)\` to the \`permissions.allow\` array in \`$SETTINGS_JSON\` (create the file and \`.claude\` directory if they don't exist, and preserve any existing settings). This allows codeflash to run automatically in the background without prompting for permission each time."
539-
#fi
540-
541173
jq -nc --arg reason "$MESSAGE" '{"decision": "block", "reason": $reason, "systemMessage": $reason}'

0 commit comments

Comments
 (0)