#!/usr/bin/env bash set -euo pipefail if [[ $# -lt 1 ]]; then echo "Usage: $0 [extra prompt]" >&2 exit 2 fi STATUS_FILE="$1" EXTRA_PROMPT="${2:-}" if [[ ! -f "$STATUS_FILE" ]]; then echo "Missing status/handoff file: $STATUS_FILE" >&2 exit 2 fi ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)" STAMP="$(date -u +%Y%m%dT%H%M%SZ)" OUT_DIR="${ROOT}/Saved/AiEscalations/${STAMP}" mkdir -p "$OUT_DIR" PROMPT_FILE="${OUT_DIR}/codex_prompt.txt" LOG_FILE="${OUT_DIR}/codex_exec.log" { echo "You are Codex being called as an escalation worker for Agrarian." echo "Use the repository at: ${ROOT}" echo echo "Local AI stopped and requested escalation. Review the status below," echo "inspect the repo, make only the needed changes, run verification, and" echo "summarize the result. Do not hide uncertainty." echo echo "Status / handoff:" cat "$STATUS_FILE" if [[ -n "$EXTRA_PROMPT" ]]; then echo echo "Extra prompt:" echo "$EXTRA_PROMPT" fi } > "$PROMPT_FILE" echo "Prompt written to ${PROMPT_FILE}" if command -v codex >/dev/null 2>&1; then codex exec "$(cat "$PROMPT_FILE")" 2>&1 | tee "$LOG_FILE" else npx -y @openai/codex exec "$(cat "$PROMPT_FILE")" 2>&1 | tee "$LOG_FILE" fi echo "Codex escalation log written to ${LOG_FILE}"