#!/usr/bin/env bash set -euo pipefail # Repeatedly asks opencode to continue autonomous project work. # # Configuration: # OPENCODE_BIN Command to run. Default: opencode # OPENCODE_PROJECT Project/workspace path. Default: current directory # OPENCODE_SESSION Session id to continue. If unset, uses --continue # OPENCODE_MODEL Optional model, e.g. openai/gpt-5.5 # OPENCODE_AGENT Optional agent name # OPENCODE_EXTRA_ARGS Optional extra args inserted before the message, shell-split # AUTOPILOT_MESSAGE Message sent each iteration. Default: keep working prompt below # AUTOPILOT_INTERVAL Seconds to sleep between successful iterations. Default: 10 # AUTOPILOT_MAX_RUNS Max iterations. 0 means forever. Default: 0 # AUTOPILOT_STOP_FILE Stop when this file exists. Default: .opencode-autopilot-stop # AUTOPILOT_ALLOW_PERMISSIONS Automatically approve tool permissions. Default: 1 OPENCODE_BIN=${OPENCODE_BIN:-opencode} OPENCODE_PROJECT=${OPENCODE_PROJECT:-$(pwd)} AUTOPILOT_INTERVAL=${AUTOPILOT_INTERVAL:-10} AUTOPILOT_MAX_RUNS=${AUTOPILOT_MAX_RUNS:-0} AUTOPILOT_STOP_FILE=${AUTOPILOT_STOP_FILE:-.opencode-autopilot-stop} AUTOPILOT_ALLOW_PERMISSIONS=${AUTOPILOT_ALLOW_PERMISSIONS:-1} AUTOPILOT_MESSAGE=${AUTOPILOT_MESSAGE:-Continue working autonomously in this configured session. Keep working on the current task list. If every task is finished, inspect the project, decide the next highest-value tasks, add or update the task list, implement them, verify them, and continue until blocked. Do not stop just because one task is complete; stop only for a real blocker that requires user input.} run_count=0 while true; do if [[ -e "$AUTOPILOT_STOP_FILE" ]]; then printf 'stop file exists: %s\n' "$AUTOPILOT_STOP_FILE" exit 0 fi if [[ "$AUTOPILOT_MAX_RUNS" != "0" && "$run_count" -ge "$AUTOPILOT_MAX_RUNS" ]]; then printf 'reached AUTOPILOT_MAX_RUNS=%s\n' "$AUTOPILOT_MAX_RUNS" exit 0 fi args=(run --dir "$OPENCODE_PROJECT") if [[ "$AUTOPILOT_ALLOW_PERMISSIONS" != "0" ]]; then args+=(--dangerously-skip-permissions) fi if [[ -n "${OPENCODE_SESSION:-}" ]]; then args+=(--session "$OPENCODE_SESSION") else args+=(--continue) fi if [[ -n "${OPENCODE_MODEL:-}" ]]; then args+=(--model "$OPENCODE_MODEL") fi if [[ -n "${OPENCODE_AGENT:-}" ]]; then args+=(--agent "$OPENCODE_AGENT") fi if [[ -n "${OPENCODE_EXTRA_ARGS:-}" ]]; then # shellcheck disable=SC2206 extra_args=($OPENCODE_EXTRA_ARGS) args+=("${extra_args[@]}") fi args+=("$AUTOPILOT_MESSAGE") run_count=$((run_count + 1)) printf '[%s] autopilot iteration %d\n' "$(date -Is)" "$run_count" "$OPENCODE_BIN" "${args[@]}" sleep "$AUTOPILOT_INTERVAL" done