Files
loop-loop/setup.sh
Sheldon Finlay ad58a49182 feat: auto-archive completed runs before starting new features
When /agent-loop:run detects a previous run with all stories passed (or the
feature branch deleted after merge), it archives the old artifacts and resets
.loop/ automatically — no more manual rm -rf .loop.

- Add archive_and_reset() for on-demand archiving from skills
- Add runs.log index tracking all archived runs
- Update /run and /stories skills to detect completed runs
- setup.sh archives instead of hard-failing when prd.json exists
- Bump version to 0.9.0
2026-04-02 07:40:07 -04:00

140 lines
4.0 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 — archiving previous run..."
# Source state.sh (needed by archive.sh for story queries) and archive.sh
LOOP_DIR="$LOOP_DIR" source "$LOOP_DIR/lib/state.sh" 2>/dev/null || true
LOOP_DIR="$LOOP_DIR" source "$LOOP_DIR/lib/archive.sh" 2>/dev/null || true
if type archive_and_reset &>/dev/null; then
archive_and_reset "$LOOP_DIR"
else
# Fallback for old harness versions without archive_and_reset
echo "[setup] WARNING: Could not archive (old harness version). To re-initialize, delete .loop/ first: rm -rf .loop"
exit 1
fi
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": 3,
"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 ""