intermediatebash
OpenSpec Apply Loop
Loop through pending tasks in an OpenSpec change, implement each one, and mark it complete.
Code
#!/usr/bin/env bash
# Apply all pending tasks in an OpenSpec change.
# Usage: CHANGE=my-feature bash apply-loop.sh
set -euo pipefail
CHANGE="${CHANGE:?Set CHANGE env var}"
# Get instructions
STATE=$(openspec instructions apply --change "$CHANGE" --json)
STATUS=$(echo "$STATE" | jq -r '.state')
if [[ "$STATUS" == 'all_done' ]]; then
echo "All tasks complete — archive with: openspec archive $CHANGE"
exit 0
fi
if [[ "$STATUS" == 'blocked' ]]; then
echo "Blocked: $(echo \"$STATE\" | jq -r '.message')"
exit 1
fi
# Print progress
echo "Progress: $(echo \"$STATE\" | jq -r '.progress.complete')/$(echo \"$STATE\" | jq -r '.progress.total') tasks"
echo ""
echo "Next task:"
echo "$STATE" | jq -r '.tasks[] | select(.done == false) | .description' | head -1How it works
The `openspec instructions apply` command returns machine-readable state including which tasks are pending. Pair this with your implementation loop to work through tasks systematically without losing track of progress.