---
title: Babysit pull requests
description: An agent keeps your open PRs merged with their base, and pings you when it can't.
order: 9
icon: git-pull-request
tags: [automations, agents, git]
related: [parallel-agents, ssh-and-git-push, vscode]
---
import AgentPrompt from '../../components/AgentPrompt.astro';
import AgentPromptLink from '../../components/AgentPromptLink.astro';
import Agent from '../../components/Agent.astro';
import AgentOnly from '../../components/AgentOnly.astro';
import AgentPicker from '../../components/AgentPicker.astro';
import BabysitDiagram from '../../components/BabysitDiagram.astro';
## The problem
Open pull requests rot: the base branch moves on, conflicts appear, and nobody notices until review. The fix is mostly mechanical: merge, resolve, push.
A **dispatcher** sandbox runs a shell loop you own. For each PR that is conflicted or behind, it creates a **child** instance on the PR's branch, in its own worktree, merges the base in and starts <Agent /> only on a conflict. When it gives up, you get a notification.
<BabysitDiagram />
<AgentPromptLink />
<AgentPicker />
```toml title="devsandboxes.toml" agent="claude"
[template.base]
image = "node:22"
mounts = ["source=${sharedVolumes}/gh,target=/root/.config/gh,type=bind"]
[template.base.features]
"ghcr.io/devcontainers/features/github-cli:1" = true
[sandbox.pr-dispatcher]
extends = "base"
folder = "../pr-dispatcher" autostart = "runtime" dispatcher = { spawn = ["web"] } postStartCommand = "nohup ./babysit-loop.sh >babysit.log 2>&1 &"
[sandbox.web] extends = "base"
folder = "../web" mounts = ["source=${localEnv:HOME}/.claude,target=/root/.claude,type=bind"]
containerEnv = { CLAUDE_CONFIG_DIR = "/root/.claude" }
[sandbox.web.postCreateCommand]
git-auth = "gh auth setup-git" git-identity = "git config --global user.name 'PR babysitter' && git config --global user.email you@example.com"
agent = "npm install -g @anthropic-ai/claude-code"
```
```toml title="devsandboxes.toml" agent="codex"
[template.base]
image = "node:22"
mounts = ["source=${sharedVolumes}/gh,target=/root/.config/gh,type=bind"]
[template.base.features]
"ghcr.io/devcontainers/features/github-cli:1" = true
[sandbox.pr-dispatcher]
extends = "base"
folder = "../pr-dispatcher" autostart = "runtime" dispatcher = { spawn = ["web"] } postStartCommand = "nohup ./babysit-loop.sh >babysit.log 2>&1 &"
[sandbox.web] extends = "base"
folder = "../web" mounts = ["source=${localEnv:HOME}/.codex,target=/root/.codex,type=bind"]
[sandbox.web.postCreateCommand]
git-auth = "gh auth setup-git" git-identity = "git config --global user.name 'PR babysitter' && git config --global user.email you@example.com"
agent = "npm install -g @openai/codex"
```
<AgentOnly agent="claude">
On macOS <Agent /> keeps its login in the Keychain: run `/login` once in any instance.
</AgentOnly>
The loop, in `pr-dispatcher/` next to `devsandboxes.toml`, with a `.gitignore` for `babysit.log` and `.babysit/`. Set `REPO` and `chmod +x` it:
```sh title="pr-dispatcher/babysit-loop.sh" agent="claude" collapsed
set -u
REPO=owner/web
STATE=.babysit mkdir -p "$STATE"
FIX='
set -u
fail() {
git merge --abort 2>/dev/null
devsbd notify --level error --key "pr-$PR_NUMBER" -- "PR $PR_NUMBER: $1"
exit 1
}
git fetch origin "$BASE_BRANCH" || fail "git fetch failed"
if ! git merge --no-edit "origin/$BASE_BRANCH"; then
claude -p "Merging origin/$BASE_BRANCH into this PR branch stopped on
conflicts. Resolve every conflict, keeping the intent of both sides, and run
the tests if the project has any. Then git add the resolved files and
git commit --no-edit. Do not push. If a conflict needs a human decision,
stop and leave the merge unfinished." \
--permission-mode acceptEdits --allowedTools "Bash(git *),Bash(npm *)" ||
fail "claude exited with an error"
if [ -n "$(git diff --name-only --diff-filter=U)" ] ||
git rev-parse -q --verify MERGE_HEAD >/dev/null; then
fail "conflicts need a human"
fi
fi
git push || fail "git push failed"
'
ctl() {
while :; do
"$@" </dev/null; rc=$?
[ "$rc" -eq 75 ] || return "$rc"
sleep 60
done
}
OPEN='.[] | select(.isCrossRepository | not)
| "\(.number) \(.headRefName) \(.baseRefName) \(.headRefOid)"'
needs_attention() {
case $(gh pr view "$1" -R "$REPO" --json mergeStateStatus -q .mergeStateStatus </dev/null) in
DIRTY|BEHIND) return 0 ;;
*) return 1 ;;
esac
}
while :; do
if ! gh pr list -R "$REPO" -q "$OPEN" \
--json number,headRefName,baseRefName,headRefOid,isCrossRepository \
>"$STATE/open.new"; then
sleep 300
continue
fi
mv "$STATE/open.new" "$STATE/open"
while read -r num branch base head; do
key="pr-$num"
if [ -f "$STATE/$key" ]; then
read -r id last <"$STATE/$key"
case $(ctl devsbd run wait "$key" "$id" --timeout 0) in
running) continue ;; "exited 0") ;;
*) [ "$last" = "$head" ] && continue ;; esac
fi
needs_attention "$num" || continue
ctl devsbd ensure web --key "$key" --branch "$branch" \
--env PR_NUMBER="$num" --env BASE_BRANCH="$base" >/dev/null || {
devsbd notify --level error --key "$key" "PR $num: cannot start a child (exit $?)"
continue
}
ctl devsbd run prune "$key" --keep 5 >/dev/null id=$(ctl devsbd exec "$key" --detach -- sh -c "$FIX") || continue
echo "$id $head" >"$STATE/$key"
done <"$STATE/open"
for f in "$STATE"/pr-*; do
[ -e "$f" ] || continue
key=${f##*/}
grep -q "^${key#pr-} " "$STATE/open" && continue
ctl devsbd rm "$key" && rm -f "$f"
done
sleep 300
done
```
```sh title="pr-dispatcher/babysit-loop.sh" agent="codex" collapsed
set -u
REPO=owner/web
STATE=.babysit mkdir -p "$STATE"
FIX='
set -u
fail() {
git merge --abort 2>/dev/null
devsbd notify --level error --key "pr-$PR_NUMBER" -- "PR $PR_NUMBER: $1"
exit 1
}
git fetch origin "$BASE_BRANCH" || fail "git fetch failed"
if ! git merge --no-edit "origin/$BASE_BRANCH"; then
# No Codex sandbox or approvals: the container is the sandbox.
codex exec --dangerously-bypass-approvals-and-sandbox "Merging
origin/$BASE_BRANCH into this PR branch stopped on conflicts. Resolve every
conflict, keeping the intent of both sides, and run the tests if the project
has any. Then git add the resolved files and git commit --no-edit. Do not push.
If a conflict needs a human decision, stop and leave the merge unfinished." ||
fail "codex exited with an error"
if [ -n "$(git diff --name-only --diff-filter=U)" ] ||
git rev-parse -q --verify MERGE_HEAD >/dev/null; then
fail "conflicts need a human"
fi
fi
git push || fail "git push failed"
'
ctl() {
while :; do
"$@" </dev/null; rc=$?
[ "$rc" -eq 75 ] || return "$rc"
sleep 60
done
}
OPEN='.[] | select(.isCrossRepository | not)
| "\(.number) \(.headRefName) \(.baseRefName) \(.headRefOid)"'
needs_attention() {
case $(gh pr view "$1" -R "$REPO" --json mergeStateStatus -q .mergeStateStatus </dev/null) in
DIRTY|BEHIND) return 0 ;;
*) return 1 ;;
esac
}
while :; do
if ! gh pr list -R "$REPO" -q "$OPEN" \
--json number,headRefName,baseRefName,headRefOid,isCrossRepository \
>"$STATE/open.new"; then
sleep 300
continue
fi
mv "$STATE/open.new" "$STATE/open"
while read -r num branch base head; do
key="pr-$num"
if [ -f "$STATE/$key" ]; then
read -r id last <"$STATE/$key"
case $(ctl devsbd run wait "$key" "$id" --timeout 0) in
running) continue ;; "exited 0") ;;
*) [ "$last" = "$head" ] && continue ;; esac
fi
needs_attention "$num" || continue
ctl devsbd ensure web --key "$key" --branch "$branch" \
--env PR_NUMBER="$num" --env BASE_BRANCH="$base" >/dev/null || {
devsbd notify --level error --key "$key" "PR $num: cannot start a child (exit $?)"
continue
}
ctl devsbd run prune "$key" --keep 5 >/dev/null id=$(ctl devsbd exec "$key" --detach -- sh -c "$FIX") || continue
echo "$id $head" >"$STATE/$key"
done <"$STATE/open"
for f in "$STATE"/pr-*; do
[ -e "$f" ] || continue
key=${f##*/}
grep -q "^${key#pr-} " "$STATE/open" && continue
ctl devsbd rm "$key" && rm -f "$f"
done
sleep 300
done
```
It is the [sample from the automations guide](https://github.com/Nemikolh/devsandboxes/blob/main/docs/automations-guide.md#sample-pr-babysit-dispatcher), adapted: the child merges the base itself, a failure notifies you once per PR head, and fork PRs are skipped.
```bash
devsandbox run pr-dispatcher devsandbox exec -it pr-dispatcher gh auth login devsandbox ```
Every five minutes the loop checks the PRs; a child `web-pr-<number>` appears in the Instances tab for each one that needs work. Failures land in the Inbox tab (`4`) and on your desktop. Follow along:
```bash
devsandbox exec pr-dispatcher tail -f babysit.log
devsandbox exec pr-dispatcher devsbd ls devsandbox exec -it web-pr-123 bash ```
- [`dispatcher`](/docs/config#devsandbox-extras) lets the dispatcher create, stop and remove children of the sandboxes in `spawn`; `devsbd ensure` / `exec` / `rm` are those requests.
- Control commands are served by the [host daemon](/docs/host-daemon), which `run` started and the running dispatcher keeps up, dashboard open or not. Exit `75` means no daemon is reachable (after a reboot, until your first `devsandbox` command; `devsandbox serve install` avoids that), which `ctl` retries; notifications wait for it.
- `autostart = "runtime"` restarts the dispatcher, and so the loop, after a reboot.
- Each child runs <Agent /> unattended with your `gh` login and pushes to your PR branches: keep `spawn` to what it needs and review what it pushes.
- Same-repo PRs only: a fork's branch isn't on `origin`.
- Children outlive the dispatcher; remove orphans with `devsandbox rm web-pr-123`.
- `ensure` and `rm` run git on the host, which refuses a repo whose `.git/config` sets command-running keys (`core.sshCommand`, `credential.*`, …).
<AgentPrompt
goal="Set up a devsandbox PR babysitter for the GitHub repo <owner/repo> (checked out at <repo path>): a dispatcher spawns a child per open PR that is conflicted or behind, merges the base in, runs <agent> non-interactively on conflicts, pushes, and notifies me on failure."
steps={[
'Read docs/automations-guide.md in the devsandboxes repo (https://github.com/Nemikolh/devsandboxes/blob/main/docs/automations-guide.md); use only the devsbd commands and flags it documents.',
'In devsandboxes.toml, add a sandbox for the repo (`folder` = <repo path>) with the github-cli Feature, a shared `${sharedVolumes}/gh` mount, `gh auth setup-git` and a git identity of <your name> / <your email> in `postCreateCommand`. Install <agent> there too and mount its host config dir (its login) into the container.',
'Add a `[sandbox.pr-dispatcher]` with its own `folder` (a new git repo next to the config dir), `autostart = "runtime"`, `dispatcher = { spawn = ["<sandbox name>"] }` (never `"*"`) and `postStartCommand = "nohup ./babysit-loop.sh >babysit.log 2>&1 &"`.',
'Write babysit-loop.sh in that folder from https://devsandboxes.com/examples/babysit-pr (the <agent> variant), with `REPO=<owner/repo>` and the sandbox name in `devsbd ensure`; `chmod +x` it, gitignore `babysit.log` and `.babysit/`, check it with `sh -n`.',
]}
verify={[
'Ask me before starting anything. Then `devsandbox -C <config dir> run pr-dispatcher` and tell me to run `devsandbox -C <config dir> exec -it pr-dispatcher gh auth login` myself.',
'Then check `devsandbox -C <config dir> exec pr-dispatcher tail babysit.log` and `devsandbox -C <config dir> exec pr-dispatcher devsbd ls` after the first round. Never push, merge or remove children yourself.',
]}
/>