Files
ernie-poc/run-ai.sh
2026-03-02 10:07:03 -05:00

80 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
# AI Overnight Web App Builder - Runner Script
# Called by cron hourly during nighttime
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOCK_FILE="$SCRIPT_DIR/.ai-builder.lock"
LOG_FILE="$SCRIPT_DIR/ai-builder.log"
# Check if another instance is already running
if [ -f "$LOCK_FILE" ]; then
PID=$(cat "$LOCK_FILE")
if kill -0 "$PID" 2>/dev/null; then
echo "$(date): Another instance is running (PID $PID). Exiting." >> "$LOG_FILE"
exit 0
else
echo "$(date): Stale lock file found (PID $PID). Removing." >> "$LOG_FILE"
rm -f "$LOCK_FILE"
fi
fi
# Acquire lock
echo $$ > "$LOCK_FILE"
trap 'rm -f "$LOCK_FILE"' EXIT
echo "$(date): Starting AI build session..." >> "$LOG_FILE"
# Read current context from worklog
WORKLOG="$SCRIPT_DIR/WORKLOG.md"
INSTRUCTIONS="$SCRIPT_DIR/ai-instructions.md"
if [ ! -f "$WORKLOG" ]; then
echo "$(date): ERROR: WORKLOG.md not found" >> "$LOG_FILE"
exit 1
fi
if [ ! -f "$INSTRUCTIONS" ]; then
echo "$(date): ERROR: ai-instructions.md not found" >> "$LOG_FILE"
exit 1
fi
# Build the task prompt for opencode
TASK_PROMPT="You are an AI building a web application incrementally.
Read the following instructions first:
$(cat "$INSTRUCTIONS")
---
Current worklog state:
$(cat "$WORKLOG")
---
Your task: Continue building the web application. Read the current state from WORKLOG.md, make meaningful progress on the next item(s), then update WORKLOG.md with what you accomplished and what should happen next. Make a git commit with descriptive message."
# Run opencode with the task
cd "$SCRIPT_DIR"
/home/ernie/.opencode/bin/opencode run "$TASK_PROMPT" 2>&1 | tee -a "$LOG_FILE"
EXIT_CODE=${PIPESTATUS[0]}
if [ $EXIT_CODE -eq 0 ]; then
echo "$(date): AI build session completed successfully" >> "$LOG_FILE"
# Check if there are changes to commit
if git diff --quiet && git diff --cached --quiet; then
echo "$(date): No changes to commit" >> "$LOG_FILE"
else
git add -A
git commit -m "AI: $(date '+%Y-%m-%d %H:%M') - incremental progress" >> "$LOG_FILE" 2>&1 || true
echo "$(date): Changes committed" >> "$LOG_FILE"
fi
else
echo "$(date): AI build session failed with exit code $EXIT_CODE" >> "$LOG_FILE"
fi
echo "$(date): Session finished" >> "$LOG_FILE"