# Least-Privilege Tokens and Branch Protection — Safe Autonomous Code Fixing

Source: https://www.skillbyai.com/en/safe-autonomous-code-fixing/x-privilege

> The bot proposes; humans and CI decide.

## Push to branches, never to main

Give the fix bot a token that can **create branches and pull requests**, but cannot push to protected branches, approve its own pull requests, change repository settings or read unrelated repositories. Protect the main branch: required status checks, required human approval, no force pushes. Scope tokens per repository, make them short-lived where possible, and log every action the bot takes.

## Bot permissions

Allow the minimum.

```text
action                                    bot allowed?
read repository contents                  yes (only target repos)
create branch autofix/*                   yes
open / update pull request                yes
push to main                              no (branch protection)
approve or merge its own PR               no
edit workflows / settings / secrets       no
read organisation secrets                 no
```

## Use a dedicated bot identity

A separate account or app makes bot actions easy to audit and to revoke.

**Quiz:** Which permission should an auto-fix bot NOT have?

- [x] Merging its own pull requests into main
- [ ] Creating a branch
- [ ] Opening a pull request
- [ ] Reading the target repository

*Answer:* Merging its own pull requests into main. Humans and CI approve merges.
