Sub topic 16
Testing the job before enabling the schedule
A scheduled agent job runs unattended, often at 02:00, so you won't see the first failure as it happens. This chapter gives you a sequence of tests that moves from "works in my terminal" to "works under the scheduler" before you turn on the real schedule.
It assumes you have the pieces from earlier chapters: a wrapper script like nightly-agent.sh (lock, timeout, log, exit status), a scheduler entry (cron, a systemd timer, or a CI workflow), and some alerting on non-zero exit. You should be comfortable with shell exit codes and git status.
Why a manual run is not enough
When you run the wrapper from your terminal, it inherits your PATH, your shell startup files, your logged-in credentials, your current directory, and a terminal. The scheduler provides none of these. Cron gives a minimal environment, systemd gives only what the unit declares, and CI gives a fresh checkout on a clean machine.
A job that passes in your shell can therefore fail under the scheduler for reasons unrelated to the agent. A second risk is that the real agent, with real credentials, can do real damage during a test. Your test plan has to deal with both.
The test ladder
Each rung adds realism and risk. Fix problems at the lowest rung where they show up.
Rung 1: test the wrapper with a fake agent
The wrapper's logic (lock, timeout, exit status, log, alert) does not depend on the real agent. Test it with a stub that you control. This is fast, free, and safe.
#!/usr/bin/env bash
# /home/deploy/bin/fake-agent.sh
# Stands in for the real agent. Behavior is controlled by environment variables.
cat > /dev/null # consume the prompt on stdin
echo "fake agent started"
sleep "${FAKE_SLEEP:-1}"
exit "${FAKE_STATUS:-0}"To make the timeout testable, change the wrapper's timeout line so the limit can be overridden. The default stays 20 minutes.
timeout "${AGENT_TIMEOUT:-20m}" $AGENT_CMD < prompts/nightly.md > "$LOG" 2>&1Then exercise each behavior. Run the stub with an absolute path, because the wrapper changes directory before it launches the agent.
chmod +x /home/deploy/bin/fake-agent.sh
export AGENT_CMD=/home/deploy/bin/fake-agent.sh
# Success: expect status 0 and a new log file.
/home/deploy/bin/nightly-agent.sh; echo "status=$?"
# Agent failure: expect status 3 and a "failed with status 3" message on stderr.
FAKE_STATUS=3 /home/deploy/bin/nightly-agent.sh; echo "status=$?"
# Timeout: expect status 124 and "timed out" appended to the log.
AGENT_TIMEOUT=2s FAKE_SLEEP=30 /home/deploy/bin/nightly-agent.sh; echo "status=$?"
# Overlap: the second run should skip and exit 0.
FAKE_SLEEP=20 /home/deploy/bin/nightly-agent.sh &
sleep 1
/home/deploy/bin/nightly-agent.sh; echo "status=$?"
cat "$HOME/agent-logs/skipped.log"
waitThe skipped run exits 0, so your alerting must not treat that as a failure. If you want to hear about repeated skips, alert on the contents of skipped.log or on silence, as described in the alerting chapter.
Rung 2: simulate the scheduler's environment
Next, run the wrapper with the environment stripped down. env -i clears every inherited variable, and you add back only what cron or the unit would provide.
#!/usr/bin/env bash
# /home/deploy/bin/test-as-cron.sh
set -u
cd "$HOME" || exit 1 # cron starts jobs in the home directory
env -i \
HOME="$HOME" \
LOGNAME="${USER:-$(id -un)}" \
SHELL=/bin/sh \
PATH=/usr/local/bin:/usr/bin:/bin \
AGENT_CMD="${AGENT_CMD:?set AGENT_CMD}" \
/home/deploy/bin/nightly-agent.sh < /dev/null
echo "exit status: $?"Typical failures at this rung are "command not found" for git, jq, or the agent itself, a missing credential variable, and a script that relied on a shell function or alias. Fix them in the crontab, the unit's EnvironmentFile, or the script. Do not fix them by editing your shell profile.
Rung 3: trigger through the real scheduler
Now use the real scheduler, but start it by hand instead of waiting for the clock.
| Scheduler | Trigger by hand | Where to look |
|---|---|---|
| systemd | `sudo systemctl start nightly-agent.service` | `journalctl -u nightly-agent.service -n 50`, plus the wrapper's log |
| cron | temporarily use `* * * * *` (see rung 5) | the wrapper's log, plus the system's cron log (location varies by OS) |
| GitHub Actions | `gh workflow run nightly-agent.yml` | `gh run watch`, then the run's logs |
Starting the service directly tests the unit file: WorkingDirectory, EnvironmentFile, the user it runs as, and TimeoutStartSec. For a oneshot service, systemctl start waits until the job finishes, and the command's own exit status tells you whether the service succeeded.
Also check that the timer expression means what you think it means, and that systemd has scheduled the next run:
systemd-analyze calendar "Mon..Fri 02:00"
systemctl list-timers nightly-agent.timersystemd-analyze calendar prints the normalized expression and the next time it fires. For cron, remember that the schedule uses the system's local time zone, and GitHub Actions schedules use UTC. State the time zone in your test notes so "02:00" is unambiguous.
The workflow_dispatch trigger only works if the workflow file exists on the repository's default branch. Scheduled workflows also run only from the default branch, so a workflow on a feature branch will never fire on its schedule.
Rung 4: run the real agent in dry-run mode
Here the real agent runs, but it must not be able to change anything that matters. Enforce this with permissions and credentials, not with the prompt.
- Use a read-only tool allowlist, with no file writes and no
git push. - Use a token with read access only, or no remote credentials at all.
- Point the job at a throwaway clone or a scratch repository.
- Use a separate prompt file, such as
prompts/nightly-dry-run.md, that asks the agent to report what it would change.
A sentence like "do not modify anything" in the prompt is a request, not a control. Treat it as a convenience that makes the output easier to read, and rely on the permission settings to prevent changes.
After the run, verify three things:
- The log shows the agent finished the task without asking a question or stalling on a permission prompt.
- The result passes your validation, for example the
jq -echeck onresult.json. - Nothing changed:
git -C /srv/repos/my-project status --shortis empty, and no unexpected branches or pull requests exist on the remote.
Then run it a second time. The second run should produce the same outcome and no duplicates, which confirms the job is idempotent.
Rung 5: use a short temporary schedule
Before the real schedule, use a schedule that fires within minutes, so the scheduler itself launches the job. For cron, that is a temporary line:
# crontab -e (remove this line after the test)
PATH=/usr/local/bin:/usr/bin:/bin
AGENT_CMD=/usr/local/bin/my-agent-headless
*/5 * * * * /home/deploy/bin/nightly-agent.shLet it fire at least twice. This shows that the lock cleans up between runs, the log directory fills with distinct timestamped files, and no leftover state from a previous run breaks the next one. Keep the dry-run prompt and read-only credentials in place during this rung.
Use this rung to test alerting. Set FAKE_STATUS=1 (or break the prompt path on purpose) and confirm the failure notification arrives and contains the log path. Also stop the job entirely and confirm the silence alert fires. An alert that has never fired is untested.
Rung 6: enable the real schedule and watch
Switch to the production prompt, the production tool allowlist, and the real schedule. Don't leave it unattended yet. For the first few runs:
- Be at your desk for the first run, or check the log and result within an hour of it.
- Check the wall-clock start time against the intended time zone.
- Review the first pull requests as carefully as you would a new colleague's work.
- Check the run's duration and token or API usage against your cost limits, and tighten the timeout if the run is much shorter than the limit.
Common findings and where they appear
| Symptom | Likely cause | First rung that catches it |
|---|---|---|
| `command not found` | Minimal `PATH` | 2 |
| Agent exits at once with an authentication error | Credential only in your login shell | 2 or 3 |
| Wrong files or "not a git repository" | Wrong working directory | 3 |
| Job hangs, then times out | Agent waiting for a permission prompt | 4 |
| Runs an hour off | Time zone mismatch (local vs UTC) | 3 |
| Second run opens a duplicate pull request | Job not idempotent | 4 |
| No alert when the job fails | Alert path never exercised | 5 |
Exercise
Your wrapper passes the fake-agent tests in rung 1 and the clean-environment test in rung 2. The first run started by systemctl start fails with "No such file or directory" for prompts/nightly.md. Which setting in which file do you check first, and why did rung 2 not catch it?
Show answer
Check WorkingDirectory in the service unit. The wrapper does cd /srv/repos/my-project itself, so also confirm that path exists for the user the unit runs as, and that the unit's User= can read it. Rung 2 ran the script from your own account and home directory, so it never exercised the unit file's working directory, user, or environment file. Only a run through the real scheduler (rung 3) tests those settings.
Exercise
Why is "tell the agent in the prompt not to change anything" not enough for a dry run, and what should enforce it instead?
Show answer
A prompt is an instruction the model may misread or be steered away from, so it does not guarantee that nothing changes. Enforce the dry run with a read-only tool allowlist, credentials that cannot push or write, and a throwaway clone or scratch repository. Then confirm with git status --short and a check of the remote for new branches or pull requests.