dev_sandbox/goal-loop.sh

124 lines
4 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# goal-loop.sh — drive a plan file to completion one task at a time with pi.
#
# Polls a checkbox plan (GitHub-style "- [ ]" items) and, while any task is
# still open, submits a single pi run that selects ONE task, implements it, and
# marks it done. Loops until every checkbox is checked.
#
# Usage:
# ./goal-loop.sh PLAN.md
# ./goal-loop.sh PLAN.md --max-iter 20 --model sonnet:high
#
# Env:
# PI_CMD pi binary (default: pi)
# IN_PROGRESS treat in-progress "[~]" boxes as remaining (default: 1)
#
set -euo pipefail
# ---- args -------------------------------------------------------------------
PLAN=""
MAX_ITER=50
PI_ARGS=() # forwarded to pi after -p
while [[ $# -gt 0 ]]; do
case "$1" in
--max-iter) MAX_ITER="${2:?}"; shift 2 ;;
--model) PI_ARGS+=(--model "${2:?}"); shift 2 ;;
--thinking) PI_ARGS+=(--thinking "${2:?}"); shift 2 ;;
-h|--help)
grep '^#' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
*)
if [[ -z "$PLAN" ]]; then PLAN="$1"
else PI_ARGS+=("$1"); fi
shift ;;
esac
done
PLAN="${PLAN:?usage: goal-loop.sh <plan.md> [--max-iter N] [--model PAT] [--thinking LEVEL]}"
PI_CMD="${PI_CMD:-pi}"
IN_PROGRESS="${IN_PROGRESS:-1}"
if [[ ! -f "$PLAN" ]]; then
echo "error: plan file not found: $PLAN" >&2
exit 1
fi
# ---- helpers ----------------------------------------------------------------
# Count remaining (unchecked) boxes. Always counts empty "[ ]"; optionally
# counts in-progress "[~]" when IN_PROGRESS=1.
count_remaining() {
local n=0
n=$(( n + $(grep -cE '(^|[^xX~])\[ \]' "$PLAN" 2>/dev/null || true) ))
if [[ "$IN_PROGRESS" == "1" ]]; then
n=$(( n + $(grep -cE '\[~\]' "$PLAN" 2>/dev/null || true) ))
fi
echo "$n"
}
run_pi() {
local prompt="$1"
echo "------------------------------------------------------------"
echo ">> submitting pi run (remaining: $(count_remaining) task(s))"
echo "------------------------------------------------------------"
# -p = print (non-interactive). We hand pi the plan path and ask it to do
# exactly one task per invocation; the loop handles repetition.
"$PI_CMD" -p "${PI_ARGS[@]}" -- "$prompt"
}
# ---- loop -------------------------------------------------------------------
iter=0
remaining=$(count_remaining)
echo "Goal loop: $PLAN"
echo "Remaining open tasks: $remaining"
if [[ "$remaining" -eq 0 ]]; then
echo "All tasks already complete — nothing to do."
exit 0
fi
while [[ "$remaining" -gt 0 && "$iter" -lt "$MAX_ITER" ]]; do
iter=$((iter + 1))
before="$remaining"
echo
echo "[iter $iter/$MAX_ITER] $(date '+%H:%M:%S') — $before task(s) open"
PROMPT="You are running inside a goal loop that repeatedly invokes you, once per task.
Read the plan file at: $PLAN
Then:
1. Select exactly ONE single open task (a '- [ ]' checkbox). Pick the next
one in order that has no unmet dependencies on other open tasks.
2. Implement that task fully and verify it works.
3. When done, mark that task's checkbox as '- [x]' in the plan file using the
edit tool. Also check off any sub-tasks ('- [ ]' nested under it) that you
completed, and add a short note if you had to skip one with '- [-] <reason>'.
Do ONLY the one task — do not start a second one. Do not close the loop or edit
anything outside the plan and the files needed for the task. When finished,
print which task you did and which boxes you flipped."
# pi -p exits non-zero on some aborts; don't let that kill the loop.
if ! run_pi "$PROMPT"; then
echo "!! pi run exited non-zero (iter $iter)"
fi
remaining=$(count_remaining)
echo "Remaining open tasks: $remaining"
if [[ "$remaining" -ge "$before" ]]; then
echo "!! no progress this iteration (still $remaining open) — stopping to avoid an infinite loop."
exit 2
fi
done
if [[ "$remaining" -eq 0 ]]; then
echo
echo "Goal complete — all tasks in $PLAN are checked off."
exit 0
else
echo
echo "Stopped after $iter iteration(s) with $remaining task(s) still open."
exit 3
fi