What the agent does
The agent reads how the site logs today. In a site shaped like wagtail/bakerydemo, requirements/base.txt pins Django 6.0 and Wagtail 8.0 with whitenoise, django-csp and dj-database-url, .python-version says 3.12, and the settings are split into bakerydemo/settings/base.py, dev.py, production.py and test.py. base.py's LOGGING sends INFO and above to a console StreamHandler with a verbose formatter of [%(asctime)s][%(process)d][%(levelname)s][%(name)s] %(message)s and loggers for wagtail, django.request, django.security and xff; production.py replaces it with a plain console handler for the django logger at DJANGO_LOG_LEVEL. MIDDLEWARE starts with SecurityMiddleware and WhiteNoise and ends with wagtail's RedirectMiddleware, with the CSP middleware appended only when CSP_DEFAULT_SRC is set. Nothing names a request id anywhere, so two log lines from one request cannot be tied together, and a line from a uwsgi worker (etc/uwsgi.ini runs two workers with four threads each) cannot be tied to the request the router logged.
The deployment files say where the id can come from. Procfile and heroku.yml run the site on Heroku under uwsgi, and Heroku's router sends an X-Request-ID header with every request, so the middleware accepts that header when present and generates a UUID when it is not, which also covers docker-compose.yml and manage.py runserver. The agent writes bakerydemo/base/middleware.py with a RequestIdMiddleware that keeps the id in a contextvar, puts it on request.request_id and echoes it as X-Request-ID on the response, and a logging Filter that copies the contextvar onto every record. In base.py the filter is attached to the console handler and the verbose format gains a [%(request_id)s] field; production.py's handler gets the same filter and format so the two files stop diverging; the middleware goes right after SecurityMiddleware so the id exists before sessions, CSRF or the redirect middleware can log. etc/uwsgi.ini gains a log-format that prints the header's request variable, so uwsgi's own request lines carry the same id as the Django lines.
Tests go under bakerydemo/base/tests beside test_seo.py and test_home_page.py, which use WagtailPageTestCase and build a Site and a HomePage without the demo fixture, as AGENTS.md asks. The new test_request_id.py sends a request with HTTP_X_REQUEST_ID and asserts the same value comes back on the response, sends one without and asserts a UUID-shaped value is generated, sends a header with an over-long or odd value and asserts it is replaced, and uses assertLogs on the wagtail logger to show the id on a record emitted during the request. It runs with DJANGO_SETTINGS_MODULE=bakerydemo.settings.test ./manage.py test, the invocation AGENTS.md recommends and .github/workflows/ci.yml runs after ./manage.py check and makemigrations --check, inside a uv venv built from requirements/development.txt. Then make lint-server, which runs ruff format --check, ruff check with the B, BLE, C4, E, F, I, T20, UP and W rules from ruff.toml, curlylint and djhtml.
Finally the agent starts the site in its sandbox with the dev settings against Postgres and Redis from docker-compose.yml, loads the demo content with load_initial_data, requests the home page once with a header and once without, and reads back the response header and the console lines, so the pull request shows the format as it will look in a log aggregator.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "Add request id logging to this Wagtail site. Read requirements/base.txt, bakerydemo/settings/base.py, production.py, dev.py and test.py, bakerydemo/urls.py, etc/uwsgi.ini, Procfile, heroku.yml, docker-compose.yml, AGENTS.md, Makefile, ruff.toml, the tests under bakerydemo/base/tests and .github/workflows/ci.yml first. Add a middleware in bakerydemo/base that takes X-Request-ID from the request when it is present and well formed and generates a UUID otherwise, keeps it in a contextvar, sets it on the request and echoes it on the response, and a logging filter that adds it to every record. Put the filter and a request id field on the console handler and format in both base.py and production.py, place the middleware right after SecurityMiddleware, and add a uwsgi log-format that prints the header. Write tests beside the existing base tests for the echoed header, the generated id, a rejected malformed header and a log record carrying the id, using assertLogs. Run manage.py check, makemigrations --check, the test suite with the test settings and make lint-server as CI does, start the site against docker compose, request the home page with and without the header, and open a pull request that quotes the test output and two log lines.",
"resources": [
{
"url": "https://github.com/your-org/your-app"
}
]
}- 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.
- 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.
- 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
What comes back is a pull request on its own branch that adds bakerydemo/base/middleware.py and bakerydemo/base/tests/test_request_id.py and changes bakerydemo/settings/base.py, bakerydemo/settings/production.py and etc/uwsgi.ini. No model, template or migration changes, so makemigrations --check stays clean.
The description quotes the test run with the new cases among the base tests, the lint run, and two console lines from the sandbox, one for a request that arrived with an X-Request-ID and one for a request that did not, each followed by the response header the client received. It names the position of the middleware and says why.
Before merging, a reviewer decides whether an inbound header should be trusted outside Heroku at all (the middleware already caps its length and character set), whether the id should be sent to Sentry as a tag now that the LOGGING comment in base.py mentions it, and whether the uwsgi line format should include the response time. Each is a follow-up on the same session.
Run it on every Wagtail site logging without request ids
This is a change to start from the terminal and read later: one POST /o/{org}/sessions with this prompt and the repository in resources. The run reads the logging config and the deployment files, writes the middleware and the tests, runs the suite against Postgres and opens the pull request on its own. The sandbox parks with its disk when the turn ends, so tag Sentry events with the request id as a follow-up wakes it with the venv, the containers and the branch intact.
A team with several Django sites sends the same body once per repository, one run each, and each run reads that site's own LOGGING and middleware list. Give the API key a webhook_url and Rush posts run.completed or run.failed with the session_url when each turn ends. Send a request_id with each body so a retried POST returns the run it already started.
The use case this job follows, with its automation and trigger: Hand off a long task.
Why run it on Rush?
A request id is small to write and tedious to prove: the proof is a response header and a log line from a real server, with and without the inbound header, and a test that fails when the filter is missing from one of two settings files. The agent runs the site under docker compose in its own sandbox and the pull request carries the lines rather than a description of a middleware.
The record keeps every command, from the uv install through manage.py check to the curl calls, so a reviewer who wants to know whether the uwsgi format was exercised can read it. Nothing happens on the reviewer's laptop, and the branch runs the same CI steps the workflow already defines.
Frequently asked questions
Why not a package such as django-log-request-id?
Because the whole feature is a middleware, a contextvar and a logging filter, about as long as the configuration a package would need, and the repository keeps its dependencies short. If you would rather have the package, say so in a follow-up and the agent swaps it in.
Is the inbound X-Request-ID trusted?
Only when it looks like an id: the middleware caps the length and the character set and generates a UUID otherwise, and a test covers the rejected case. On Heroku the router sets the header, which is the value you want to search for in the router logs.
Do background tasks get the id?
A task that runs during the request, as the test settings' immediate backend does, sees the contextvar. A task picked up by a separate worker does not, unless the id is passed to it explicitly, which the pull request leaves for a follow-up.
Does it need a migration or touch templates?
No. The change is settings, one middleware file, the uwsgi config and tests, and makemigrations --check stays clean.
Will the dev server show the id?
Yes. The console handler and format in base.py are what dev.py inherits, so runserver lines carry the id, and the response header is there in every environment.