From 48a2a910e04f39861dc2975c99f57fb8c05abb6e Mon Sep 17 00:00:00 2001 From: Alex Selimov Date: Tue, 20 Oct 2026 10:48:25 -0400 Subject: [PATCH] Add goal-loop script --- Dockerfile | 10 ++++- goal-loop.sh | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 2 deletions(-) create mode 100755 goal-loop.sh diff --git a/Dockerfile b/Dockerfile index b3b3c55..94b0083 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM --platform=linux/arm64 debian:bookworm-slim +FROM --platform=linux/amd64 debian:bookworm-slim ARG PYTHON_VERSION=3.11 ARG NODE_MAJOR=22 @@ -59,6 +59,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ python3-pip \ python3-venv \ python3-dev \ + fonts-dejavu-core \ && ln -sf /usr/bin/python3 /usr/bin/python \ && rm -rf /var/lib/apt/lists/* @@ -152,7 +153,12 @@ WORKDIR /workspace # symlinked into /root/.pi/agent at runtime by entrypoint.sh. RUN mkdir -p /root/.pi -# Entrypoint script for runtime auth +# goal-loop: drive a checkbox plan to completion one task at a time via pi. +# Baked onto PATH so it is available in every new dev_sandbox. +COPY goal-loop.sh /usr/local/bin/goal-loop +RUN chmod +x /usr/local/bin/goal-loop + +# Entrypoint script for runtime auth and workspace-provided internal CA import COPY entrypoint.sh /usr/local/bin/entrypoint.sh RUN chmod +x /usr/local/bin/entrypoint.sh ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] diff --git a/goal-loop.sh b/goal-loop.sh new file mode 100755 index 0000000..5581c7a --- /dev/null +++ b/goal-loop.sh @@ -0,0 +1,124 @@ +#!/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 [--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 '- [-] '. + +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