# Turn Sentry Errors Into Fix Pull Requests for an aiohttp App

Yes. A Sentry alert on your aiohttp app starts a run; the agent reproduces the traceback with pytest-aiohttp and a real Postgres, fixes the cause and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/fix-sentry-errors/aiohttp/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads how the app is built before it reads the traceback. In an app laid out like the polls demo in aio-libs/aiohttp-demos (`demos/polls`), `aiohttpdemo_polls/main.py` assembles everything in `init_app`: the config from `settings.get_config`, which parses `config/polls.yaml` through a trafaret schema in `utils.py`; Jinja2 through `aiohttp_jinja2.setup`; `pg_context` on `app.cleanup_ctx`, which opens an SQLAlchemy async engine over asyncpg and stores an `async_sessionmaker` under the `db_key` `AppKey` from `typedefs.py`; then `setup_routes`, `setup_static_routes` and `setup_middlewares`. `routes.py` registers four handlers: `GET /`, `GET /poll/{question_id}`, `GET /poll/{question_id}/results` and `POST /poll/{question_id}/vote`. `middlewares.py` wraps all of them: an `HTTPException` whose status has an override renders `404.html` or `500.html`, and any other exception is logged with `request.protocol.logger.exception("Error handling request")` and answered with the 500 page. That logging call is what Sentry's logging integration captures, so the issue's event carries the full traceback and the request's path.

The event in this app points at `db.vote`. In `db.py` the function does `result = await sess.get(Choice, choice_id)` and then `result.votes += 1` on the very next line, and only after that checks `if not result:` to raise `RecordNotFound`. A form post whose `choice` names a choice id that does not exist, or that was deleted while the page was open, raises `AttributeError: 'NoneType' object has no attribute 'votes'` from the increment, so the `RecordNotFound` branch that `views.vote` catches to answer `HTTPNotFound` never runs and the voter gets the 500 page. The agent reads the frames (`views.py` `vote` into `db.py` `vote`), the `choice` value in the request data of the event, and the release, and checks out that commit before matching line numbers. It also notes what the message of the unused exception promises and the code never checks: that the choice belongs to the question in the URL, since `sess.get(Choice, choice_id)` loads any choice id.

It reproduces the error as a test before touching the fix. `tests/conftest.py` builds the app from `config/polls_test.yaml` and creates the `test_aiohttpdemo_polls` database through `init_db.setup_db` with an admin engine on `localhost:5432`, so the suite needs a live Postgres; the agent starts one in its sandbox the way `.github/workflows/ci.yml` does, with the `postgres` image and the password `postgres`, and installs `requirements-dev.txt`, which pins aiohttp 3.13.5, SQLAlchemy 2.0.54, asyncpg 0.32.0, pytest 9.1.1 and pytest-aiohttp 1.1.1. The new test in `tests/test_integration.py` posts `choice=9999` to `/poll/1/vote` with the `tables_and_data` fixture and expects 404; before the fix it gets 500, and because `pytest.ini` sets `filterwarnings = error` and `asyncio_mode = auto`, the captured log shows the `AttributeError` under the `Error handling request` line. The fix moves the `None` check above the increment, raises `RecordNotFound` there, and selects the choice with `Choice.question_id == question_id` so a choice from another poll is also a 404. `views.vote` already turns `RecordNotFound` into `web.HTTPNotFound(text=str(e))`, and the error middleware renders `404.html` for it.

Then it runs what the repository runs: `make flake` (flake8 over the package) and `make test` (`pytest tests`), plus the root `Makefile`'s `lint` target when the app is one demo among several. The pull request links the Sentry issue, quotes the traceback and the two test runs, and explains why the answer is a 404 and not a 400: `views.vote` already answers 400 with "You have not specified choice value" when the `choice` field is missing or not an integer, so an integer that names nothing is the record-not-found case. It leaves the templates and the middleware alone. If the event cannot be reproduced, for example an asyncpg connection error from a restart rather than a code path, it opens no pull request and says what was missing.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "A Sentry alert fired for this aiohttp app; the alert payload follows this prompt. Use the alert payload, and the Sentry connection when the run names one, to read the issue's latest event: the release, the request path and form data, the in-app frames and the logging breadcrumb from the error middleware. Read aiohttpdemo_polls/main.py, routes.py, views.py, db.py and middlewares.py first so you know how a request reaches a handler and how an exception becomes the 500 page, then tests/conftest.py, pytest.ini and .github/workflows/ci.yml for how the suite runs. Check out the release's commit before matching frames to lines. Start Postgres in your sandbox as the workflow does and install requirements-dev.txt. Write a test in tests/test_integration.py that reproduces the exception through the HTTP route and fails first; then fix the cause in the module the top in-app frame names, keeping the app's existing error handling: a missing record is RecordNotFound and a 404, a bad field is a 400, nothing new goes in the middleware. Run make flake and make test. Open a pull request that links the Sentry issue, quotes the traceback and shows the test failing before the fix and passing after. Change nothing unrelated. If the event is not enough to reproduce the error, open no pull request and say what was missing.\n\nAlert payload:\n{payload}",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that changes four lines of `db.py` and adds one test. Its description quotes the Sentry event's traceback, the test output before the fix with the `AttributeError` in the captured log, and the passing run after it, together with the aiohttp, SQLAlchemy and asyncpg versions the sandbox installed from `requirements-dev.txt` and the Postgres image it started. The check on the pull request runs the same `pytest tests` with the same service.

Nothing changes for a voter who picks a real choice: the increment, the commit through `app[db_key].begin()` and the redirect to the results page in `views.vote` are untouched. A voter who posts an id that no longer exists now sees the app's own 404 page, rendered by the existing middleware from `404.html`, instead of the 500 page.

Before merging, a reviewer decides whether a choice that exists but belongs to another question should be a 404, as the pull request makes it, or should keep counting, which was the behaviour before; and whether the test database the suite creates through `init_db.py` is acceptable in their CI, since the sandbox needed the same admin access the workflow grants.

## Run it on every Sentry issue alert

Give the Sentry project an alert rule whose action is a webhook to a handler on your server. The handler checks Sentry's signature on the delivery, then sends this request with the alert JSON after the prompt and a `request_id` that is a UUID v5 of the Sentry issue id, so a redelivered alert returns the run already started rather than a second one. Each distinct issue becomes one run and, when it reproduces, one pull request.

Set a webhook URL on the API key and Rush POSTs `run.completed` or `run.failed` to it with the session's URL. To keep the prompt in Rush instead of in the handler, create an automation in the Project with a `webhook` trigger and have the handler forward the alert body to the trigger's URL with the trigger's secret as `Authorization: Bearer`; Sentry signs its own requests and cannot add that header itself. `max_runs_per_hour` on the automation keeps one noisy issue from starting a run per event.

## Why run it on Rush?

An aiohttp handler's traceback is only half the story: the other half is the middleware that turned it into a 500 page and the session factory the handler used, and reproducing it means the same Postgres, the same pinned aiohttp and the same pytest plugin the suite expects. The agent runs all of that in its own sandbox, starts the database the workflow starts, and writes the failing test through the HTTP route rather than by calling the function, so the fix is proved where the user hit it.

The pull request carries the two test runs and the transcript keeps every command, so a reviewer who has never read `db.py` can see the exception, the one-line cause and the behaviour after. Several Sentry issues from one release each get their own sandbox and branch at once, and an event that cannot be reproduced ends as a note rather than a guess.

## Frequently asked questions

### How does the agent reproduce an error that needs Postgres?

It reads `.github/workflows/ci.yml` and starts the same `postgres` service in its sandbox, with the admin password the workflow sets, so `tests/conftest.py` can create the test database through `init_db.setup_db` exactly as CI does. The test then posts the form the user posted and asserts the status, and the captured log shows the same `Error handling request` line Sentry recorded.

### Why is a vote for an unknown choice a 404 and not a 400?

Because `views.vote` already answers 400 for a `choice` field that is missing or not an integer, with the message "You have not specified choice value". An integer that names no row is the case `db.RecordNotFound` exists for, and the view already maps that exception to `web.HTTPNotFound`; the fix makes `db.vote` raise it before dereferencing a missing row instead of after.

### Will it change the middleware or the 500 page?

No. `middlewares.py` is doing its job: it logged the exception and answered with `500.html`, which is how Sentry saw the error at all. The prompt keeps the change in the module the top in-app frame names, so the diff is `db.py` and one test.

### What if the Sentry event has no request data?

The frames and the release are usually enough to find a code path that fails on a value the handler can receive, and the agent tries that first. When the event is a connection error, a timeout or something a code change cannot cause, it opens no pull request and says why, so a reviewer is not handed a guess.

### Does the agent need a Sentry connection in the Project?

The alert payload after the prompt already carries the issue and the latest event, which is enough for most fixes. With a Sentry connection the agent can also read earlier events of the same issue and the tags, which helps when the first event's request data was scrubbed.

## More aiohttp jobs

- [Add Cookie Sessions to an aiohttp App](https://getrush.ai/run/add-cookie-sessions/aiohttp/)
- [Add CORS to an aiohttp App](https://getrush.ai/run/add-cors/aiohttp/)
- [Add Test Coverage to CI for an aiohttp Project](https://getrush.ai/run/add-coverage-to-ci/aiohttp/)

## The same job on other stacks

- [Turn Sentry Errors Into Fix Pull Requests for an Angular App](https://getrush.ai/run/fix-sentry-errors/angular/)
- [Fix Ansible Errors from Sentry Alerts](https://getrush.ai/run/fix-sentry-errors/ansible/)
- [Turn Sentry Errors Into Fix Pull Requests for an ASP.NET Core App](https://getrush.ai/run/fix-sentry-errors/aspnet-core/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
