Sub topic 13
Delivering Results as Branches and Pull Requests
This chapter assumes you can use Git branches and pull requests, write shell scripts, and read exit codes. It also assumes your job already has a wrapper script with a timeout and a lock.
Why not push to the main branch
An unattended agent has no one watching it. If it commits directly to main, a bad change ships before anyone reads it. A branch plus a pull request puts a human review between the agent and your default branch. It also leaves a record of what the agent changed and when.
Enforce this with permissions, not only with the prompt. Give the job credentials that can push branches and open pull requests, and turn on branch protection for main so that even a misbehaving run cannot write to it.
A branch name that makes reruns safe
A scheduled job runs many times. If every run creates a new branch, you get a pile of duplicate pull requests. Derive the branch name from something stable, such as the job name and the date, and check whether the work already exists before you start.
A delivery script
This script runs after your wrapper has the agent do its edits. It assumes the git and GitHub gh command-line tools are installed and authenticated, and that AGENT_CMD is set as in the earlier wrapper.
#!/usr/bin/env bash
# deliver.sh
set -euo pipefail
: "${AGENT_CMD:?set AGENT_CMD to your agent's non-interactive command}"
BRANCH="agent/nightly-$(date -u +%Y%m%d)"
cd /srv/repos/my-project
git fetch origin main
# Idempotency: stop if a pull request for this branch is already open.
if [ -n "$(gh pr list --head "$BRANCH" --state open --json number --jq '.[].number')" ]; then
echo "pull request for $BRANCH already open, skipping"
exit 0
fi
git checkout -B "$BRANCH" origin/main
# AGENT_CMD is intentionally unquoted so a command with arguments splits into words.
timeout 20m $AGENT_CMD < prompts/nightly.md
# Commit only if the agent changed something.
if [ -z "$(git status --porcelain)" ]; then
echo "no changes, nothing to deliver"
exit 0
fi
git add -A
git commit -m "Nightly agent changes ($(date -u +%F))"
git push --force-with-lease origin "$BRANCH"
gh pr create \
--base main \
--head "$BRANCH" \
--title "Nightly agent changes $(date -u +%F)" \
--body "Automated run. Review the diff before merging."Three details matter here:
git checkout -Bresets the branch toorigin/main, so a rerun on the same day starts clean.--force-with-leaseoverwrites an earlier push of the same branch but refuses if someone else has pushed to it.- The "no changes" exit returns 0. An empty result is a normal outcome, not a failure.
What the pull request should contain
Reviewers decide faster when the description says what was attempted. Ask the agent to write a short summary, and put the log path or run ID in the body. Keep changes small: tell the prompt to limit scope, because a 2,000-line agent diff gets skimmed, not reviewed.
Exercise
A nightly job creates a branch named with a random suffix on every run. After a week, the repository has seven open pull requests with near-identical changes. Give two changes that fix this.
Show answer
First, name the branch from stable inputs such as the job name and date, for example agent/nightly-20261128. Second, check for an open pull request on that branch before running, and exit 0 if one exists. Using git checkout -B and --force-with-lease also makes a same-day rerun update the existing branch instead of creating a new one.