What the agent does
The agent reads the app and how it is configured before it adds state. In the polls demo under aio-libs/aiohttp-demos, aiohttpdemo_polls/main.py builds the app in init_app: a web.Application, the config from get_config stored under config_key, aiohttp_jinja2 with a package loader, pg_context appended to cleanup_ctx to open and dispose an async SQLAlchemy engine stored under db_key, then setup_routes, setup_static_routes and setup_middlewares, which installs an error middleware rendering 404.html and 500.html. Keys are typed web.AppKey values in typedefs.py. settings.py reads config/polls.yaml through trafaret_config, and utils.py holds the TRAFARET schema that validates postgres, host and port and nothing else, so a new setting has to be declared there or the app refuses to start. views.py has four handlers: index, poll, results and vote, where vote reads choice from the form, calls db.vote and redirects to results; nothing stops a browser from voting on the same question twice. requirements-dev.txt pins aiohttp==3.13.5, aiohttp-jinja2==1.6, asyncpg==0.32.0, SQLalchemy==2.0.54, pytest and pytest-aiohttp; tests/conftest.py builds a real test database from config/polls_test.yaml and tests/test_integration.py posts a vote and checks the count went up by one; the repository's ci.yml runs every demo's tests against a Postgres service with PYTHONASYNCIODEBUG=1, and flake8 with flake8-quotes and flake8-bugbear lints the tree.
Sessions come from aiohttp-session with its secure extra, which brings cryptography for EncryptedCookieStorage. The agent adds aiohttp-session[secure] to requirements-dev.txt and to install_requires in setup.py, and adds a session_secret key to the config: a string in config/polls.yaml and config/polls_test.yaml, declared as T.String() in TRAFARET, decoded with base64.urlsafe_b64decode into the 32 bytes the storage needs, with a note in README.rst that cryptography.fernet.Fernet.generate_key() makes one. In init_app, before setup_middlewares, aiohttp_session.setup(app, EncryptedCookieStorage(key, cookie_name='polls_session', max_age=30 * 24 * 3600, httponly=True, samesite='Lax')) installs the session middleware, so the error middleware still wraps the handlers the way it did.
The session carries one fact: which questions this browser has voted on. In vote, session = await get_session(request) and voted = session.setdefault('voted', []); if the question is already there the handler redirects to results without touching the database, otherwise it records the vote, appends the id and the storage writes the cookie on the response. poll reads the same list and passes already_voted to detail.html, which shows the choices without the form and a line saying this browser has voted; results.html keeps its "Vote again?" link only when the session does not hold the id. While in db.vote, the agent moves the if not result check above result.votes += 1, since the current order raises on a missing choice before the RecordNotFound it means to raise, and says so in the pull request.
Tests use the existing fixtures. tests/test_integration.py gains a test that posts a vote, posts the same vote again from the same client, which keeps its cookie jar, and asserts the count rose by exactly one and the second response redirected to results; a second test uses a fresh client to show another browser can still vote; a third asserts detail.html no longer contains the submit button after voting; and a fourth checks the response sets a cookie named polls_session with HttpOnly. The agent starts Postgres in its sandbox, runs python init_db.py for the sample data, pytest tests with filterwarnings = error as pytest.ini demands, and flake8 aiohttpdemo_polls through the demo's Makefile, and opens the pull request when both are clean.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "Add cookie-based sessions to this aiohttp app and use them to prevent a browser from voting twice on the same poll, then open a pull request. Read aiohttpdemo_polls/main.py, views.py, routes.py, middlewares.py, settings.py, utils.py, typedefs.py, db.py, the templates, config/polls.yaml, config/polls_test.yaml, requirements-dev.txt, setup.py, pytest.ini, tests/conftest.py and tests/test_integration.py first. Add aiohttp-session[secure] to the requirements and setup.py. Add a session_secret setting to both config files and the trafaret schema, decode it to the 32-byte key EncryptedCookieStorage needs, and document how to generate one. Call aiohttp_session.setup in init_app before the middlewares with a named cookie, a max_age, httponly and samesite Lax. In vote, read the session, redirect to results without writing when the question id is already recorded, and record it after a successful vote; pass the state to the detail and results templates so the form and the vote-again link hide. Fix the order of the missing-choice check in db.vote. Add integration tests for a repeated vote from the same client, a vote from a fresh client, the hidden form and the cookie flags. Run init_db.py against a local Postgres, pytest tests and flake8 until clean, then open a pull request listing the setting, the cookie attributes and what each test proves.",
"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
The pull request changes main.py, views.py, db.py, utils.py, detail.html, results.html, both config files, requirements-dev.txt, setup.py, README.rst and tests/test_integration.py. Its description shows the setup call with its cookie attributes, names the new session_secret setting and the command that generates a value, and quotes the pytest and flake8 output from the sandbox run against Postgres.
It says what the session does not do: it is a per-browser record in an encrypted cookie, so clearing cookies or using another browser allows another vote, and the description names RedisStorage from the same package as the move for a server-side record if that matters. It also calls out the db.vote reordering as a bug fix separate from the feature, so a reviewer can judge it on its own.
Before merging, a reviewer confirms the secret is read from configuration and not committed for production, checks that max_age and samesite match how the site is served, and decides whether the test config's secret should move to an environment variable in the CI service definition.
Run it on every Linear issue labelled session
An automation on the Project with a linear trigger on linear.issue.labeled, session in config.labels, and the prompt "this issue names something the app should remember for one browser between requests; store it in the session, read it in the handlers the issue names, extend the integration tests, and open a pull request" starts one run per labelled issue once the storage is configured. The trigger adds the issue after the prompt and fills in nothing else, so an issue about remembering a chosen language gets a key in the session and a lookup in the template context.
A team with several aiohttp services sends this body once per repository from its own server, each POST /o/{org}/sessions with that repository in resources and a request_id that is a UUID v5 of the repository's full name, so a retried call returns the run already started. Each run reads its own app: a service with Redis in its config gets RedisStorage instead of the cookie, and one whose settings come from environment variables gets the secret there rather than in a YAML file.
The use case this job follows, with its automation and trigger: Hand off a long task.
Why run it on Rush?
Session middleware is a few lines, and the care is in the edges: a secret that has to be exactly 32 bytes and has to come from configuration the schema validates, a middleware that must sit in the right place relative to the error handler, a cookie whose flags decide whether a script can read it, and a feature that only makes sense if the second request from the same browser is tested as such. The agent reads those constraints out of utils.py, main.py and conftest.py and proves the behaviour with a real Postgres and a client that keeps its cookies, in a sandbox where it can start both.
The test run, the lint run and the diff stay with the run, so a reviewer sees the repeated vote refused and the fresh client accepted without setting up a database. A team with several aiohttp services sends the same request to all of them at once and reads one pull request per service.
Frequently asked questions
Where does the secret come from?
From a session_secret entry in the YAML config, validated by the trafaret schema like every other setting, and decoded to 32 bytes before it reaches EncryptedCookieStorage. README.rst shows cryptography.fernet.Fernet.generate_key() as the way to make one; the value in config/polls.yaml is for local runs only.
Why an encrypted cookie rather than Redis?
The polls app has Postgres and nothing else in its config, and the data is a short list of question ids per browser, so a cookie keeps the deployment as it is. aiohttp-session ships RedisStorage with the same setup call, and the handlers would not change if the storage did.
Can a determined user still vote twice?
Yes, by clearing cookies or using another browser; the session records this browser's votes, nothing more. Stopping that would need an account or a server-side record keyed on something the client cannot discard, which is a different feature and the pull request says so.
Does the test suite need anything new?
Only the package. The existing cli fixture gives a client with a cookie jar, so the repeated-vote test is two posts from the same client, and the fresh-client test asks the aiohttp_client fixture for a second one. The database fixtures in conftest.py are reused as they are.
More aiohttp jobs
The same job on other stacks
- Add Cookie Sessions to a React Router App (React Router)
More jobs like this: Hand off a long task
- Add a Copy Button to Sphinx Code Blocks (Sphinx)
- Add CORS to an Actix Web API (Actix Web)