#!/usr/bin/env bash set -euo pipefail if [[ $# -ne 3 ]]; then echo "Usage: bash scripts/orchestrate-codex-worker.sh " >&2 exit 1 fi task_file="$1" handoff_file="$2" status_file="$3" timestamp() { date -u +"%Y-%m-%dT%H:%M:%SZ" } write_status() { local state="$1" local details="$2" cat > "$status_file" < "$handoff_file" exit 1 fi write_status "running" "- Task file: \`$task_file\`" prompt_file="$(mktemp)" output_file="$(mktemp)" cleanup() { rm -f "$prompt_file" "$output_file" } trap cleanup EXIT cat > "$prompt_file" < "$handoff_file" write_status "completed" "- Handoff file: \`$handoff_file\`" else { echo "# Handoff" echo echo "- Failed: $(timestamp)" echo "- Branch: \`$(git rev-parse --abbrev-ref HEAD)\`" echo "- Worktree: \`$(pwd)\`" echo echo "The Codex worker exited with a non-zero status." } > "$handoff_file" write_status "failed" "- Handoff file: \`$handoff_file\`" exit 1 fi