# Let a coding agent fix failing tests overnight

A scheduled GitHub Actions run lets Claude Code try to fix failing tests on a branch and open a pull request. A person reviews every change in the morning.

- Category: Coding
- Runs on: Claude Code
- Level: Advanced
- Audience: agent
- Setup time: about 90 minutes
- Evidence: Documented (each step backed by official docs; not tested by us)
- Last updated: 2026-10-31
- Named sources: 8
- Canonical URL: https://crimsonbot.com/use-cases/fix-failing-tests-overnight

## You will need

- A GitHub repository with a test suite that runs in CI
- Claude Code GitHub Actions, with an Anthropic API key or a Claude subscription token stored as a GitHub secret (usage is billed)
- GitHub Actions minutes (included amounts depend on your GitHub plan)
- Branch protection on your main branch

## You will end up with

- One pull request per fix, each on its own branch
- Nothing merged without a person's approval

## Steps

### 1. Protect the main branch first

Turn on branch protection for main: require pull request reviews before merging, and restrict who can push. This is what actually stops an agent from merging, not the token alone.

Sources:
- [GitHub Docs: About protected branches](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches)

### 2. Give the workflow the least access it needs

Install Claude Code GitHub Actions (the action is open source, MIT). Store the API key as a GitHub secret, never in the repo. In the workflow, set GITHUB_TOKEN permissions to the minimum (contents and pull requests write only). GitHub recommends fine-grained tokens over classic ones if you use a personal token.

Sources:
- [Claude Code docs: GitHub Actions](https://code.claude.com/docs/en/github-actions)
- [anthropics/claude-code-action on GitHub](https://github.com/anthropics/claude-code-action)
- [GitHub Docs: Use GITHUB_TOKEN for authentication](https://docs.github.com/en/actions/tutorials/authenticate-with-github_token)
- [GitHub Docs: Managing your personal access tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)

### 3. Schedule the run

Add a schedule trigger with cron syntax (it runs in UTC unless you set a time zone). With a prompt, the Claude Code action runs in automation mode on a cron schedule. It has no shell access until you grant tools with --allowedTools in claude_args; allow only what running tests and editing code needs. You can also set allow/deny permission rules in Claude Code settings. Set --max-turns to cap cost. Test output and code comments can carry prompt injection, so keep tool access narrow.

```text
Run the test suite with [TEST COMMAND].
For each failing test, up to [N]: create a branch named fix/<test-name>, make the smallest change to the code under test that makes it pass, run the full suite again, and open a pull request explaining the cause.
Never edit, skip or delete a test to make it pass. Never change CI config, dependencies or secrets. If you are not confident, open an issue instead of a pull request.
```

Sources:
- [GitHub Docs: Events that trigger workflows (schedule)](https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows)
- [Claude Code docs: GitHub Actions — Run on a schedule](https://code.claude.com/docs/en/github-actions)
- [Claude Code docs: Settings (permissions)](https://code.claude.com/docs/en/settings)
- [Claude Code docs: Security](https://code.claude.com/docs/en/security)

### 4. Review the pull requests in the morning (human checkpoint)

A person reviews and merges. Close any pull request that changes a test to make it pass, or touches files outside the fix.

Sources:
- [GitHub Docs: About protected branches](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches)

## Tools used

- [Claude Code](https://claude.com/product/claude-code): Coding agent for the terminal, IDE and desktop.

## Next

- [Our tool: Scrub a PDF locally before you paste it into AI →](https://crimsonbot.com/go/p/pdf-scrubber)
- [Get an email when the newsletter launches →](https://crimsonbot.com/#newsletter)
