Hard rails for an autonomous code-audit agent
Prompt rules guide an audit agent. Permissions, repository rules, draft pull requests, and human review are what stop it from merging its own work.
I run an audit agent across my repositories on a recurring schedule. It looks for bugs, security issues, dead code, abandoned scaffolding, and the small debris that accumulates between human reviews. It can propose a narrow class of fixes in a draft pull request. I do not want it deciding that its own work deserves to land.
The important part is not the audit prompt. It is the authority the agent does not have.
These rails protect repository and merge authority. They do not protect the host, network, credentials, or data from hostile repository code; that needs separate controls.
A prompt is not a guardrail
My first version put a large NEVER list at the top of the prompt. That was useful, but I was calling the wrong thing a hard rail.
An instruction tells the model what it should not do. A permission boundary makes the action unavailable. If the same credential can push to the default branch, change repository rules, approve a pull request, and merge it, then a stern paragraph is the last line of defence. That is not where I want it.
The setup I would trust as a hard boundary has three layers:
| Layer | What it does |
|---|---|
| Repository policy | Requires a pull request and human approval, blocks force-pushes, and gives the audit identity no bypass |
| Agent instructions | Define forbidden actions, the small remediation scope, and when to stop |
| Postcondition checks | Confirm that the pull request is open, still a draft, targets the expected branch, and has no auto-merge request |
The first layer matters most, and it exposes an uncomfortable distinction. If the agent runs through my own broadly privileged session, prompt rules and postcondition checks are operational discipline - not hard rails. The platform cannot reliably enforce a human boundary between the agent and me if it sees us as the same actor.
The hard-rail version therefore needs a dedicated automation identity with no administrative or bypass privileges, plus repository rules on the default branch. That is a deployment requirement, not something a prompt can simulate.
Repository commands run in an isolated environment without production secrets. A suspected security finding or exposed secret goes through a private disclosure path, never a public branch or draft pull request.
GitHub rulesets can require pull requests, reviews, status checks, and restrictions on updates to selected branches. They can also grant bypasses. The audit identity gets none. The point is not to make the agent promise that it will avoid main; the point is to make main reject it.
The five absolute rules
The platform boundary comes first. The prompt still gets an explicit operating policy:
ABSOLUTE RULES
1. NEVER merge, approve, or mark a pull request ready for review.
2. NEVER push to the default branch or any protected branch. Work only
on a fresh audit/auto-fixes-<DATE> branch. Never force-push.
3. NEVER change repository settings, rulesets, branch protection,
secrets, access controls, or CI workflows. Report concerns instead.
4. NEVER expand the remediation scope because a fix looks easy. If a
change is not explicitly eligible, report it and stop there.
5. After creating the draft PR, verify every required postcondition.
If one fails, contain the failure, report it, and stop the run.
I also name forbidden command families in the real prompt: merge, approval, auto-merge, ruleset, secret, administrative, force-push, and branch-deletion operations. This is not a security boundary. It is a way to make the desired behaviour unambiguous and the logs easier to audit.
There is one awkward but necessary exception. GitHub CLI disables auto-merge through gh pr merge --disable-auto. If the verification step finds auto-merge enabled, the agent may run that exact recovery command, with no merge method or --auto flag, verify the result, and terminate the audit. A blanket ban on every gh pr merge invocation would contradict the recovery procedure.
Draft is a state, not a mood
GitHub documents draft pull requests as unmergeable. I treat that state as a fail-safe and status signal, not the enforced boundary: identity permissions and repository rules provide that boundary. An identity with enough authority may be able to change the pull request state first.
Auto-merge also needs precise language. Allowing auto-merge at repository level does not schedule every pull request for automatic merging. Someone with write permission must enable it on a particular pull request; that pull request then merges when its configured requirements are satisfied.
After opening the pull request, the agent queries its state rather than assuming the creation command did what I meant:
gh pr view <PR> --json state,isDraft,baseRefName,headRefName,autoMergeRequest
Assert:
- state is OPEN
- isDraft is true
- baseRefName is the expected default branch
- headRefName is the fresh audit branch
- autoMergeRequest is null
If auto-merge is not null, the recovery path is exact:
gh pr merge <PR> --disable-auto
Then the agent re-queries the pull request, reports the invariant failure, and stops. It does not continue because it successfully repaired the dangerous state. A safety check that fails is information about the system, not a minor detour.
Scope before cleverness
An audit agent without scope reads like a horoscope: your code has issues. True. Useless.
I group findings by action because they do not all imply the same response:
| Priority | Buckets | Default action |
|---|---|---|
| Human first | Correctness, security | Report only |
| Candidate cleanup | Stubs, TODOs, dead or unreachable code | Propose only when repository policy explicitly allows it |
| Engineering judgement | Maintainability, performance, testing | Report with evidence |
| Low-risk upkeep | Documentation and developer experience | Narrow fixes may be eligible |
The report starts with the findings most likely to hurt now. It also records the evidence, affected paths, confidence, and whether the item was changed or merely reported. “Potential issue” is not a useful unit of work without a reason and a location.
There are no universally safe fixes
My original allow-list was too generous. Removing an unused import can remove a side effect. Deleting an empty file can break packaging. Adding a null guard can change behaviour. Removing .skip can reactivate a deliberately quarantined test. “The linter suggested it” is evidence, not absolution.
The list is now called eligible for proposal, not safe to auto-fix:
Eligible only when the repository context explicitly permits it:
- Apply the repository's configured formatter to a tightly bounded diff
- Fix unambiguous typos in comments and documentation
- Repair a verified broken internal documentation link
- Remove a focused-test marker such as .only
- Remove an obvious debugger statement when repository policy forbids it
- Apply a configured linter fix whose behavioural effect is understood
Everything remains reviewable in a draft pull request. Passing tests is necessary, not proof that the change is harmless.
The report-only list is deliberately broader:
Never change automatically:
- Business logic, authentication, payments, or data mutation paths
- Security findings
- Public APIs, exported signatures, schemas, or migrations
- Test assertions or quarantined tests
- Dependencies or lockfiles
- CI workflows, repository policy, or access configuration
- File, function, or symbol names
- Generated, vendored, or intentionally scaffolded paths
- Anything whose intended behaviour is unclear
The useful behaviour is not bravely fixing more. It is stopping without turning uncertainty into a diff.
Project context is policy
Each repository has a small audit-context.md that tells the agent what kind of project it has entered. I treat that file as human-owned policy: the agent may read it but never edit it.
| Field | Purpose |
|---|---|
| Stack | Establish what belongs in the repository |
| Status | Set a different bar for a prototype and a maintained product |
| Auto-fix posture | Choose report-only, conservative, or standard |
| Intentional scaffolding | Suppress findings for deliberate placeholders |
| Out of scope | Exclude paths from inspection or modification |
| Known issues | Avoid reopening work already tracked elsewhere |
| Conventions | Record non-obvious local rules |
| Priority areas | Direct attention without expanding authority |
report-only is the default for a new repository. I watch a complete run before enabling even the conservative proposal list. Most bad findings are not acts of model rebellion; they are the model confidently misunderstanding something intentionally strange.
The workflow, end to end
1. Verify the automation identity, active repository rules, and default branch.
If the required protections are absent, run report-only.
2. Fetch the default branch fresh. If an earlier audit PR is still open,
report it and stop rather than stacking another one on top.
3. Create a fresh audit/auto-fixes-<YYYY-MM-DD> branch.
4. Write audits/AUDIT_REPORT_<YYYY-MM-DD>.md with findings and evidence.
5. Apply only eligible proposals, in one commit per finding category.
If a category would touch more than 20 files, report it instead.
6. Run the repository's documented checks and record the exact results.
7. Review the complete diff for scope violations, then push the branch.
8. Open a draft pull request.
9. Verify PR state, draft state, base, head, and auto-merge state.
10. Print the report and PR links. Stop.
The 20-file ceiling is not a universal safety constant. It is my review-budget tripwire. A sweeping cleanup may be correct and still be impossible to review responsibly in the format this agent produces.
One commit per category serves the same purpose. audit: remove focused-test markers and audit: repair documentation links can be inspected or reverted independently. One mega-commit called audit fixes turns nominal human review into archaeology.
What the agent is actually for
This is not a substitute for code review, a security audit, or engineering judgement. It is a recurring janitorial pass with unusually strict employment conditions.
It finds the focused test that escaped CI, the documentation link that now goes nowhere, the abandoned debug statement, and the scaffold that is either dead or insufficiently explained. Sometimes it finds a real bug. When it does, its job is to present the evidence - not to promote itself from observer to release manager.
The best autonomous agents are good at not doing things. This one inspects, proposes, verifies, opens a draft, and stops. The prompt helps it remember that boundary. The repository makes the boundary real.