#!/usr/bin/env bash set -uo pipefail MAX_RETRIES="${MAX_RETRIES:-3}" PYTHON="${PYTHON:-python}" STATE_FILE=.build_state LOG_DIR=.build_logs mkdir -p "$LOG_DIR" [[ -f "$STATE_FILE" ]] || printf '0\n' > "$STATE_FILE" phase_name() { case "$1" in 1) printf 'Foundation' ;; 2) printf 'Core Systems' ;; 3) printf 'Content Layer' ;; 4) printf 'Polish & Persist' ;; esac } phase_prompt() { case "$1" in 1) cat <<'PROMPT' Read AGENTS.md and plan.md completely. Implement Phase 1 Foundation only: data/constants.py, entities/entity.py, entities/player.py, entities/enemy.py, engine/game_state.py, BSP engine/map_gen.py, and a minimal playable pygame main.py. Use dataclass(slots=True), full type hints, no stubs. Implement tests/smoke.py phase 1 gate: generate 5 maps; require >=3 non-overlapping rooms; flood-fill every floor tile from spawn; print one ASCII map. Run python tests/smoke.py --phase 1 and fix it before returning. Do not implement later phases. PROMPT ;; 3) cat <<'PROMPT' Read AGENTS.md and plan.md completely. Implement Phase 3 Content Layer against the existing phases: the complete fixed item catalog, 26-slot inventory, equipment, all potions and scrolls, XP and every level threshold, all six regular enemy types and Ancient Dragon with exact stats/behaviors, slime splitting, dragon breath, depth-gated spawning, and the floor-9 boss arena with two healing potions. Keep GameSession the only mutable-state root and log every event. Replace the phase 3 smoke gate with real assertions for every enemy's stats, equipment stat changes, item effects, XP/level-up, splitting, and boss arena. Run python tests/smoke.py --phase 3 and fix failures. No stubs or TODOs. PROMPT ;; 4) cat <<'PROMPT' Read AGENTS.md and plan.md completely. Implement Phase 4 Polish & Persist: exact JSON save/load of GameSession and all 10 floors including timers, menu/New Game/conditional Continue/Quit, save-and-quit, permadeath save deletion, death/victory screens, camera, colored pygame tile rendering, FOV/exploration, all UI panels, inventory/equipment/item selection, help, and every documented control. Preserve headless testability. Replace phase 4 smoke gate with real save/load equality and state/menu reachability assertions. Run python tests/smoke.py --phase 4 and fix failures. No stubs or TODOs. PROMPT ;; esac } run_pi() { local label="$1" prompt="$2" log log="$LOG_DIR/$label.log" printf '\n==> %s\n' "$label" pi -p "$prompt" 2>&1 | tee "$log" return "${PIPESTATUS[0]}" } run_gate() { local phase="$1" log log="$LOG_DIR/phase-${phase}-gate.log" "$PYTHON" tests/smoke.py --phase "$phase" 2>&1 | tee "$log" return "${PIPESTATUS[0]}" } fix_phase() { local phase="$1" attempt="$2" output output="$(<"$LOG_DIR/phase-${phase}-gate.log")" run_pi "phase-${phase}-fix-${attempt}" "Read AGENTS.md and plan.md. Phase ${phase} ($(phase_name "$phase")) failed its gate. Fix the implementation or strengthen/correct the gate without deleting valid assertions. Run the gate before returning. Actual stdout/stderr:\n\n${output}" } build_phase_2() { local attempt="$1" pids=() rc=0 local common='Read AGENTS.md and plan.md completely. Implement only the named Phase 2 subsystem against existing code, with full type hints and no stubs. Do not edit unrelated subsystem files. Add focused assertions to tests/smoke.py only if safe under concurrent edits; otherwise leave gate integration to the follow-up.' run_pi "phase-2-fov-${attempt}" "$common Implement engine/fov.py recursive shadowcasting in 8 octants, radius 8, updating visible and explored." & pids+=("$!") run_pi "phase-2-path-${attempt}" "$common Implement engine/pathfinding.py A* with Manhattan heuristic and occupied-tile avoidance." & pids+=("$!") run_pi "phase-2-combat-${attempt}" "$common Implement engine/combat.py exact hit clamp, armor reduction, critical hits, death/XP, and human-readable logging." & pids+=("$!") for pid in "${pids[@]}"; do wait "$pid" || rc=1; done (( rc == 0 )) || return 1 run_pi "phase-2-ai-${attempt}" 'Read AGENTS.md and plan.md completely. FOV, A*, and combat now exist. Implement ai/behaviors.py with every Phase 2 behavior integration required by the design, then implement a real tests/smoke.py phase 2 gate covering walled-in FOV, A* between floor tiles, 1000 combat attacks with bounded outcomes, and an enemy chasing through a corridor. Run python tests/smoke.py --phase 2 and fix it. No stubs or TODOs.' } last="$(tr -dc '0-4' < "$STATE_FILE")" last="${last:-0}" for phase in 1 2 3 4; do (( phase <= last )) && continue printf '\n######## Phase %s: %s ########\n' "$phase" "$(phase_name "$phase")" passed=0 for ((attempt=1; attempt<=MAX_RETRIES; attempt++)); do if (( phase == 2 )); then if (( attempt == 1 )); then build_phase_2 "$attempt" || true; else fix_phase "$phase" "$attempt" || true; fi elif (( attempt == 1 )); then run_pi "phase-${phase}-build" "$(phase_prompt "$phase")" || true else fix_phase "$phase" "$attempt" || true fi if run_gate "$phase"; then passed=1; break; fi done if (( passed == 0 )); then printf 'Phase %s failed after %s attempts. See %s.\n' "$phase" "$MAX_RETRIES" "$LOG_DIR" >&2 exit 1 fi printf '%s\n' "$phase" > "$STATE_FILE" done printf '\n######## Goal backstop ########\n' goal_passed=0 for ((attempt=1; attempt<=MAX_RETRIES; attempt++)); do run_pi "goal-audit-${attempt}" 'Read AGENTS.md and plan.md completely. Audit every checkbox in plan.md Acceptance Criteria against the real game. Run the full smoke suite and a deterministic SDL_VIDEODRIVER=dummy headless playthrough that exercises movement, combat, pickup/use/equip, stairs, save/load, death, and victory. Fix every failure you find; do not merely report it. Keep tests honest. Return only after all criteria are implemented and python tests/smoke.py passes.' || true if "$PYTHON" tests/smoke.py 2>&1 | tee "$LOG_DIR/goal-gate.log"; then goal_passed=1 break fi gate_output="$(<"$LOG_DIR/goal-gate.log")" run_pi "goal-fix-${attempt}" "The final acceptance gate failed. Read AGENTS.md and plan.md, fix every actual cause, and rerun the full suite. Actual stdout/stderr:\n\n${gate_output}" || true done if (( goal_passed == 0 )); then printf 'Final acceptance gate failed after %s attempts. See %s.\n' "$MAX_RETRIES" "$LOG_DIR" >&2 exit 1 fi printf '\nUnderdepths build complete: all phase and final gates pass.\n'