# What the Defense Agent does

The Defense Agent fixes the problems the Attack Agent finds, in a copy of your code, and sends each fix as a pull request or a download.

The Defense Agent fixes security problems in your website's code. It works on a copy of your code on a computer of its own, and it can never push to your repository: AgentProof turns its change into a pull request, or keeps it on the run page for you to download. It needs your code, connected in **Settings > Code**.

The Defense Agent runs in two ways. A **repair run** starts on its own after the Attack Agent finds problems. A **planned Defense run** reviews your code on a day you or Autopilot chose.

## Give the Defense Agent your code

The Defense Agent can only work when your website's code is connected. An owner or admin does this in **Settings > Code**, in one of two ways:

- **GitHub**: press **Connect GitHub**, install the AgentProof GitHub App, then choose the repository and press **Add repository**. Fixes can then arrive as pull requests.
- **Copy of your code**: upload a .zip or .tar.gz of your code. Fixes then stay on the run page for you to download.

See [Connect your code](/docs/connect-your-code/) for every step and limit.

## Repair runs start on their own

A repair run starts when all of these are true:

1. An Attack run on your website ended as **Completed**.
2. It confirmed at least one problem with a risk other than Info.
3. Your website's code is connected: a GitHub repository linked to the website, or an uploaded copy.

The repair run is labelled REPAIR. It receives the list of problems, each with its address, what is wrong, the evidence, the steps to show it again and the Attack Agent's suggested fix. It uses agent hours like any run.

Problems from retests, code audits, app audits and the free website test never start a repair run. See [Attack Agent](/docs/attack-agent/).

### When a repair run cannot start

If the Defense Agent is busy with another run, or too many runs are already going, AgentProof tries again every minute for up to an hour. Meanwhile the fix shows **PREPARING FIX** with a note such as "The agent stayed busy." or "Too many runs were already running."

If the run still cannot start, or cannot start for another reason, the fix shows **NOT STARTED** with the reason as its note:

- "No agent hours left."
- "The workspace has no active plan."
- "The website's domain is not verified."
- "Workspace storage is full."
- "Agent runs are not available."
- "The Defense run could not start."

## Planned Defense runs review your code

You can plan a Defense run like any run: drag the Defense Agent onto a day on the **Calendar**, press **+ Defense** in the dashboard panel, or let Autopilot plan it. See [Calendar and scheduling](/docs/calendar-and-scheduling/).

A planned Defense run:

1. reads the agents' notes about your website (see [Agent memory](/docs/agent-memory/))
2. fixes your website's confirmed problems that are not fixed yet, up to the 30 newest, leaving out Info notes
3. reviews the code for problems an attacker could use: who can read or change which data, sign-in and sessions, input that reaches database queries, files or commands, secrets in the code, and dependencies with known security flaws
4. fixes what it can confirm from the code, and lists what it is unsure of in its summary instead of changing it
5. saves a short report as `report.md` and updates its notes about your website

It may open your website to confirm a problem. When it does, it never places orders or payments, never changes or deletes real data, never sends enough traffic to slow the site and never signs in.

**Warning:**
A planned Defense run needs your code. Without it, the run is skipped when its time comes: the **Calendar** shows **SKIPPED / NO CODE ADDED**, and alerts say "Add your code in Settings." While no code is connected, **Auto schedule** and **Autopilot** plan only Attack runs.

## How a fix reaches you

When a Defense run finishes, AgentProof reads the change it prepared. The Defense Agent's computer has no way to push to your repository. AgentProof makes the pull request itself, through the AgentProof GitHub App.

### As a pull request

A fix becomes a pull request when your website has a connected GitHub repository and **Open pull requests for fixes** is on in **Settings > Code**. It is on unless an owner or admin turns it off.

- The pull request goes to the branch shown for the repository in **Settings > Code**, based on the commit the agent worked from.
- Its branch name starts with `agentproof/fix-`, followed by the Defense run's ID without its `run_` prefix.
- Its description is the agent's summary of what it changed and tested, followed by the problems it fixes, each with its risk.
- The run page's fix record shows **PULL REQUEST OPEN** with a link to the pull request and the branch name.
- The dashboard's **Needs you** list shows "Fix for 2 problems · pull request" with a **Review** button that opens it on GitHub.

A developer on your team reviews the pull request and merges it, or closes it.

### As a kept fix to download

When your code is an uploaded copy, or **Open pull requests for fixes** is off, the fix stays on the run page:

- The fix record shows **FIX READY** and lists the files it changes under **FILES**.
- **Download the fix** gives you a .zip file with each changed file at its path in your code. If the fix removes files, the .zip also holds `DELETED-FILES.txt`, which lists them.
- The dashboard's **Needs you** list shows "Fix for 2 problems · ready to download" with a **Download** button.

The fixed problems change to **FIX PROPOSED**. AgentProof does not retest a kept fix on its own. Apply it to your code and deploy it yourself.

## What a fix may change

The Defense Agent is told to make the smallest change that fixes each problem, in the style of the code around it, to add a dependency only when the fix needs one, and to run your project's tests and linters when they exist.

AgentProof refuses a fix that:

- changes more than 50 files
- has a file larger than 1 MB, or is larger than 4 MB in total
- has a file that is not text
- changes any of these, because they could run with your repository's secrets: `.git/`, `.github/`, `.gitlab-ci.yml`, `.circleci/`, `.buildkite/`, `azure-pipelines.yml`, `bitbucket-pipelines.yml` or `Jenkinsfile`

A refused fix shows **NO FIX** with a note that starts "The fix was refused:" and names the reason.

## Retest after you merge a fix

When you merge a fix's pull request, its record shows **MERGED / WAITING FOR DEPLOY** and its problems show **FIX MERGED**.

The Attack Agent then retests the problems on your live website. The retest starts 10 minutes after the merge, or sooner, as soon as GitHub reports a successful deployment of the merge commit. It is an Attack run labelled RETEST, and it uses agent hours like any run.

The retest repeats each problem's recorded steps and marks each one:

- **FIXED**: the same attack no longer works.
- **STILL OPEN**: the attack still works.
- **NOT CHECKED**: the check could not run, for example because the site was down or still showed the old version.

When every problem is fixed, the fix record shows **FIXED AND VERIFIED**. Otherwise it shows **NOT VERIFIED**.

If you close the pull request without merging it, the record shows **PULL REQUEST CLOSED** and its problems go back to **OPEN**.

If the retest cannot start, the note says why and when AgentProof tries again, for example: "The retest could not start: no agent hours left. AgentProof tries again on Dec 1." When hours ran out, it tries again when they reset. For other reasons, it tries again a day later.

## The Protected by AgentProof link

Unless you turn it off, a fix also adds a small **Protected by AgentProof** link to your website. The Defense Agent adds it once, in the footer or layout every page shares, and only as part of a change that also fixes a problem. It never makes a change just for the link.

Owners and admins turn this off in **Settings > Website** by clearing **Defense Agent fixes add a "Protected by AgentProof" link**. See [Protected by AgentProof](/docs/protected-by-agentproof/).

## Statuses of fixes and problems

The run page shows a fix record under **The Defense Agent's fix.** on the Attack run, the Defense run and the retest. Its **RUNS** line links to all three.

### Fix statuses

| Status | What it means |
|---|---|
| PREPARING FIX | The Defense run is waiting to start, running, or its pull request is being opened. |
| NOT STARTED | The Defense run could not start. The note says why. |
| NO FIX | The run ended without a fix AgentProof could use. The note says why. |
| FIX READY | A kept fix is ready to download. |
| PULL REQUEST OPEN | The pull request is waiting for your team. |
| PULL REQUEST CLOSED | The pull request was closed without merging. |
| MERGED / WAITING FOR DEPLOY | The pull request was merged. The retest waits for the deploy. |
| RETESTING | The Attack Agent is retesting the problems. |
| FIXED AND VERIFIED | The retest found every problem fixed. |
| NOT VERIFIED | At least one problem was still open or could not be checked. |

### Problem statuses

| Status | What it means |
|---|---|
| OPEN | Found and not fixed yet. |
| FIX PROPOSED | A fix for it is in an open pull request or ready to download. |
| FIX MERGED | Its fix was merged and is waiting for the retest. |
| FIXED | The retest showed it is fixed. |
| STILL OPEN | The retest showed it still works. |
| NOT CHECKED | The retest could not check it. |

AgentProof can also send these changes to Slack, Discord or a webhook: **Fix pull request opened**, **Fix ready**, **Fix verified** and **Fix not verified**. See [Notifications](/docs/notifications/).

## Notes on a fix that did not work

These are the notes a fix record can show under **NOTE**, and what to do.

- **The Defense Agent did not prepare a fix.** The run ended without saving a fix. Check what the agent reported at the end on the Defense run's page.
- **The Defense Agent made no change.** The agent found nothing it could fix safely. The note may show its own summary instead, which explains why.
- **The Defense run did not finish.** The run failed, was stopped or reached its time limit.
- **The fix was refused: …** The fix broke one of the rules in [What a fix may change](#what-a-fix-may-change).
- **The repository is no longer connected.** Connect the repository again in **Settings > Code**.
- **GitHub refused the pull request: …** GitHub's own reason follows. Check that the AgentProof GitHub App can still write to the repository.
- **The pull request could not be opened.** AgentProof kept getting errors from GitHub for an hour.

- [Attack Agent](/docs/attack-agent/) What it tests and how it records problems.
- [Connect your code](/docs/connect-your-code/) Connect GitHub or upload a copy of your code.
- [Runs and reports](/docs/runs-and-reports/) Read run pages and reports.
- [Protected by AgentProof](/docs/protected-by-agentproof/) The link fixes add to your website.
