Files
loop-loop/setup.sh

132 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
# Agent Loop — project setup script
# Scaffolds .loop/ directory and generates config.json.
# Called by /agent-loop:setup or /agent-loop:run, or run directly.
#
# Usage:
# setup.sh <mode> # mode: implement, explore, or fix
# setup.sh implement # scaffold + config for implement mode
# setup.sh # defaults to implement
set -euo pipefail
MODE="${1:-implement}"
# --- Validate mode ---
if [[ ! "$MODE" =~ ^(implement|explore|fix)$ ]]; then
echo "[setup] ERROR: Invalid mode '$MODE'. Must be: implement, explore, fix"
exit 1
fi
# --- Find harness source ---
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
HARNESS_SRC=""
# Option 1: Running from the plugin cache or repo (script is next to prompts/)
if [ -d "$SCRIPT_DIR/prompts" ]; then
HARNESS_SRC="$SCRIPT_DIR"
fi
# Option 2: Plugin cache (glob for any version)
if [ -z "$HARNESS_SRC" ]; then
HARNESS_SRC=$(ls -d ~/.claude/plugins/cache/agent-loop/agent-loop/*/prompts/.. 2>/dev/null | head -1)
fi
# Option 3: Global install
if [ -z "$HARNESS_SRC" ] && [ -d "$HOME/.claude/loop/prompts" ]; then
HARNESS_SRC="$HOME/.claude/loop"
fi
if [ -z "$HARNESS_SRC" ]; then
echo "[setup] ERROR: Cannot find agent-loop harness files."
echo "[setup] Install the plugin: /plugin install agent-loop@agent-loop"
exit 1
fi
echo "[setup] Harness source: $HARNESS_SRC"
# --- Ensure git repo exists ---
if ! git rev-parse --git-dir &>/dev/null; then
echo "[setup] No git repo found. Initializing..."
git init
git checkout -b main 2>/dev/null || true
fi
# --- Scaffold .loop/ ---
PROJECT_ROOT="$(pwd)"
LOOP_DIR="$PROJECT_ROOT/.loop"
if [ -d "$LOOP_DIR" ] && [ -f "$LOOP_DIR/prd.json" ]; then
echo "[setup] .loop/ already exists with prd.json."
echo "[setup] To re-initialize, delete .loop/ first: rm -rf .loop"
exit 1
fi
mkdir -p "$LOOP_DIR"
# Copy harness files
cp -r "$HARNESS_SRC/prompts" "$LOOP_DIR/"
cp -r "$HARNESS_SRC/templates" "$LOOP_DIR/"
cp -r "$HARNESS_SRC/lib" "$LOOP_DIR/"
cp "$HARNESS_SRC/loop.sh" "$LOOP_DIR/"
chmod +x "$LOOP_DIR/loop.sh"
# Verify critical files
for f in prompts/generator/_base.md prompts/evaluator/_base.md templates/progress.md.template lib/state.sh loop.sh; do
if [ ! -f "$LOOP_DIR/$f" ]; then
echo "[setup] ERROR: Missing $LOOP_DIR/$f — harness copy failed"
exit 1
fi
done
# --- Create .gitignore ---
cat > "$LOOP_DIR/.gitignore" << 'GITIGNORE'
prd.json
progress.md
progress-archive.md
config.json
init.sh
contracts/
triage/
archive/
.archive-staging/
.last-branch
.loop.lock
GITIGNORE
# --- Generate config.json ---
cat > "$LOOP_DIR/config.json" << EOF
{
"tool": "claude",
"mode": "$MODE",
"maxIterations": 20,
"skipEval": false,
"evalRetries": 2,
"autoHooks": true,
"branchPrefix": "loop/",
"scopeBudgets": {
"explore": { "maxFilesToRead": 15, "maxLinesToWrite": 0, "maxFilesToModify": 0 },
"implement": { "maxFilesToRead": 50, "maxLinesToWrite": 500, "maxFilesToModify": 10 },
"fix": { "maxFilesToRead": 30, "maxLinesToWrite": 200, "maxFilesToModify": 5 }
}
}
EOF
# --- Generate init.sh stub ---
cat > "$LOOP_DIR/init.sh" << 'INITSH'
#!/bin/bash
set -euo pipefail
echo "[init] Environment ready."
INITSH
chmod +x "$LOOP_DIR/init.sh"
# --- Done ---
echo ""
echo "[setup] .loop/ scaffolded successfully."
echo "[setup] Mode: $MODE"
echo "[setup] Config: .loop/config.json"
echo ""
echo "Next steps (in Claude Code):"
echo " /agent-loop:stories # Generate stories from your spec or description"
echo ""