codeflash-agent/plugin/hooks/session-start.sh
HeshamHM28 6274dca2d5 feat: enhance Java optimization flow with extended sessions, Pareto tracking, and MCP visibility
Add iterative optimization capabilities inspired by Kimi K2.6: thread topology & spin-wait
strategies, allocation profiling, cross-function scope, behavioral equivalence verification,
Pareto frontier tracking with chart generation, extended session protocol (10-15+ hours),
session interruption detection/recovery via hooks, and MCP endpoint visibility so users
can follow the profiling pipeline.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-28 16:17:45 +03:00

115 lines
4.4 KiB
Bash
Executable file

#!/usr/bin/env bash
# Plugin SessionStart hook: Set up .codeflash/ session directory.
#
# Two modes:
# Dogfood: Plugin loaded from codeflash-agent/dist/ (--plugin-dir).
# Redirects .codeflash/ → agent repo's .codeflash/{teammember}/{org}/{project}/ via symlink.
# Normal: Plugin installed normally.
# Creates .codeflash/ in the project directory.
cd "$CLAUDE_PROJECT_DIR" 2>/dev/null || exit 0
# Parse org/project from git remote
REMOTE=$(git remote get-url origin 2>/dev/null)
[ -z "$REMOTE" ] && exit 0
PATH_PART=""
if echo "$REMOTE" | grep -qE '^git@'; then
PATH_PART=$(echo "$REMOTE" | sed -E 's/^git@[^:]*://' | sed 's/\.git$//')
elif echo "$REMOTE" | grep -qE '^https?://'; then
PATH_PART=$(echo "$REMOTE" | sed -E 's|^https?://[^/]*/||' | sed 's/\.git$//')
elif echo "$REMOTE" | grep -qE '^ssh://'; then
PATH_PART=$(echo "$REMOTE" | sed -E 's|^ssh://[^/]*/||' | sed 's/\.git$//')
fi
ORG=$(echo "$PATH_PART" | cut -d'/' -f1 | tr '[:upper:]' '[:lower:]')
PROJECT=$(echo "$PATH_PART" | cut -d'/' -f2 | tr '[:upper:]' '[:lower:]')
[ -z "$ORG" ] || [ -z "$PROJECT" ] && exit 0
# Derive team member from git user (lowercase, no spaces)
MEMBER=$(git config user.name 2>/dev/null | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
[ -z "$MEMBER" ] && MEMBER="unknown"
# Detect dogfood mode: CLAUDE_PLUGIN_ROOT's parent has .codeflash/ (the agent repo)
AGENT_REPO=""
if [ -n "$CLAUDE_PLUGIN_ROOT" ]; then
CANDIDATE=$(cd "$CLAUDE_PLUGIN_ROOT/.." 2>/dev/null && pwd)
if [ -d "$CANDIDATE/.codeflash" ]; then
AGENT_REPO="$CANDIDATE"
fi
fi
if [ -n "$AGENT_REPO" ]; then
# --- Dogfood mode ---
TARGET="$AGENT_REPO/.codeflash/$MEMBER/$ORG/$PROJECT"
mkdir -p "$TARGET"
if [ ! -e "$CLAUDE_PROJECT_DIR/.codeflash" ]; then
# No .codeflash yet — create symlink
ln -s "$TARGET" "$CLAUDE_PROJECT_DIR/.codeflash"
elif [ -L "$CLAUDE_PROJECT_DIR/.codeflash" ]; then
# Already a symlink — verify it points to the right place
CURRENT=$(readlink "$CLAUDE_PROJECT_DIR/.codeflash")
if [ "$CURRENT" != "$TARGET" ]; then
rm "$CLAUDE_PROJECT_DIR/.codeflash"
ln -s "$TARGET" "$CLAUDE_PROJECT_DIR/.codeflash"
fi
fi
# If .codeflash is a real directory, leave it alone
DATA_DIR="$TARGET"
else
# --- Normal mode ---
mkdir -p "$CLAUDE_PROJECT_DIR/.codeflash"
DATA_DIR="$CLAUDE_PROJECT_DIR/.codeflash"
fi
# Build session context from existing state
MSG=""
# Detect interrupted session (active but not completed/plateau)
SESSION_STATUS=""
if [ -f "$DATA_DIR/HANDOFF.md" ]; then
SESSION_STATUS=$(grep "Session status:" "$DATA_DIR/HANDOFF.md" 2>/dev/null | head -1 | sed 's/.*Session status: *//')
fi
if [ "$SESSION_STATUS" = "active" ]; then
# Session was interrupted — this is the highest priority signal
KEPT=$(grep -c "^.*keep" "$DATA_DIR/results.tsv" 2>/dev/null || echo "0")
TOTAL=$(wc -l < "$DATA_DIR/results.tsv" 2>/dev/null | tr -d ' ' || echo "0")
TOTAL=$((TOTAL - 1)) # subtract header
[ "$TOTAL" -lt 0 ] && TOTAL=0
MSG="INTERRUPTED SESSION DETECTED: A previous optimization session on branch $(git branch --show-current 2>/dev/null) was interrupted (Session status: active). $TOTAL experiments completed ($KEPT kept)."
MSG="$MSG Read .codeflash/HANDOFF.md, .codeflash/results.tsv, and .codeflash/strategy-plan.md to understand where it stopped."
MSG="$MSG Ask the user: 'I found an interrupted optimization session. Would you like me to continue where it left off, or start fresh?'"
if [ -f "$DATA_DIR/pareto-frontier.md" ]; then
MSG="$MSG Pareto frontier at .codeflash/pareto-frontier.md shows the optimization trajectory so far."
fi
elif [ -f "$DATA_DIR/HANDOFF.md" ]; then
MSG="Previous session state found at .codeflash/HANDOFF.md (status: ${SESSION_STATUS:-unknown}) — read it before starting new work."
fi
if [ -f "$DATA_DIR/results.tsv" ]; then
LINES=$(wc -l < "$DATA_DIR/results.tsv" | tr -d ' ')
MSG="${MSG:+$MSG }Experiment history at .codeflash/results.tsv ($LINES entries)."
fi
if [ -f "$DATA_DIR/learnings.md" ]; then
MSG="${MSG:+$MSG }Learnings from previous sessions at .codeflash/learnings.md."
fi
if [ -f "$DATA_DIR/strategy-plan.md" ]; then
MSG="${MSG:+$MSG }Strategy plan at .codeflash/strategy-plan.md — check remaining strategies."
fi
[ -z "$MSG" ] && exit 0
cat <<EOF
{
"systemMessage": "$MSG"
}
EOF
exit 0