What the agent does
The agent starts in Package.swift and Sources/App/configure.swift. In a project generated from vapor/template the manifest is swift-tools-version:6.3 with vapor from 4.121.4, fluent 4.13.0 and a Fluent driver chosen at generation time, Postgres 2.12.0, MySQL 4.8.0 or SQLite 4.9.0 according to manifest.yml, plus swift-nio 2.101.0 and the VaporTesting product for the test target. The upcoming-feature flags in swiftSettings, ExistentialAny, InternalImportsByDefault and MemberImportVisibility among them, mean a new middleware has to use any for protocol types and import what it names. configure.swift sets up the database from DATABASE_HOST, DATABASE_USERNAME and the other Environment.get variables, adds CreateTodo() to app.migrations, and calls routes(app); the commented-out FileMiddleware line is the only middleware mention. CORS needs no new package, because CORSMiddleware ships in Vapor itself.
The edit in configure.swift reads the allowed origins from a new CORS_ALLOWED_ORIGINS environment variable, a comma-separated list, builds a CORSMiddleware.Configuration with allowedOrigin set to those origins, allowedMethods of GET, POST, PUT, PATCH, DELETE and OPTIONS, and allowedHeaders of accept, authorization, contentType, origin and xRequestedWith, and registers it with app.middleware.use(cors, at: .beginning). The position matters: Vapor's default ErrorMiddleware must run after CORS so that an error response still carries the Access-Control-Allow-Origin header, and at: .beginning is how the Vapor docs place it. With the variable unset the agent allows no cross-origin requests rather than every origin, and the pull request says so. docker-compose.yml gets the variable in its x-shared_environment block beside LOG_LEVEL, so docker compose up app behaves like production.
Tests go in Tests/AppTests/AppTests.swift, which already uses Swift Testing: a @Suite with a withApp helper that calls Application.make(.testing), configure(app), app.autoMigrate() and app.autoRevert(), and @Test functions that drive app.testing().test(.GET, "hello") and the todos routes with #expect. The agent adds three cases in the same style. A preflight OPTIONS /todos with Origin set to an allowed origin and Access-Control-Request-Method: POST answers with Access-Control-Allow-Origin equal to that origin and Access-Control-Allow-Methods listing POST. A GET /hello from an origin not in the list gets no Access-Control-Allow-Origin header and still returns the plain greeting the route sends. A POST /todos from an allowed origin creates the todo and carries the header. The allowed list for the test is set through the Environment the suite builds, so the tests do not depend on the shell.
Then it runs swift test from the package root, the command TEMPLATE_README.md documents and the one .github/workflows/test-template.yml runs against a freshly generated project for every Fluent and Leaf combination. It also runs docker compose build, as the workflow's container job does, to be sure the swift:6.3-noble builder stage in the Dockerfile still compiles the package with --static-swift-stdlib. It pushes a branch and opens a pull request with the diff, the environment variable's name and format, and the test output.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "Add CORS to this Vapor server. Read Package.swift, Sources/App/configure.swift, Sources/App/routes.swift, Tests/AppTests/AppTests.swift, docker-compose.yml and .github/workflows first. Use Vapor's built-in CORSMiddleware; add no package. Read the allowed origins from a CORS_ALLOWED_ORIGINS environment variable as a comma-separated list, allow GET, POST, PUT, PATCH, DELETE and OPTIONS and the accept, authorization, content-type, origin and x-requested-with headers, and register the middleware at .beginning so error responses carry the headers. With the variable unset, allow no cross-origin requests. Add the variable to docker-compose.yml's shared environment. In AppTests.swift, using the existing Swift Testing suite and withApp helper, add tests for a preflight OPTIONS on /todos from an allowed origin, a GET /hello from a refused origin, and a POST /todos from an allowed origin, setting the allowed list through the test Environment. Run swift test and docker compose build. Open a pull request with the diff, the variable's format and the test output. Change nothing else.",
"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. Sources/App/configure.swift gains the middleware configuration and the environment read, docker-compose.yml gains one line under x-shared_environment, and Tests/AppTests/AppTests.swift gains three @Test functions beside helloWorld, getAllTodos, createTodo and deleteTodo. Package.swift is unchanged. The description names CORS_ALLOWED_ORIGINS, shows an example value, and states that an unset variable means no cross-origin access.
The session transcript, at GET /o/{org}/sessions/{id}/messages, keeps the swift test run with every suite's result and the docker compose build output, so a reviewer can see the package compiled with the Swift 6.3 toolchain and the tests passed before trusting the description. If your repository has a workflow that runs swift test on pull requests, it runs on the branch as well.
Before merging, a reviewer checks that the production origin list is set wherever the app is deployed, since the default refuses everything; that allowedHeaders includes any custom header the frontend sends; and that at: .beginning did not displace a middleware the project added after generation. A follow-up to the same session adjusts any of these on the same branch.
Run it on every terminal hand-off
This is a change you start and walk away from. One POST /o/{org}/sessions with this body and your repository in resources starts the run in its own sandbox, where swift build of a fresh package and its dependencies takes the time it takes without holding your machine; GET /o/{org}/sessions/{id}/events streams it live if you want to watch. When the turn ends the sandbox parks with its .build directory intact, so a follow-up through POST /o/{org}/sessions/{id}/messages recompiles only what changed.
Give the key a webhook_url and Rush POSTs run.completed to your server when the turn finishes, with the result and the session_url. Send a request_id with the request so a retry returns the run already started.
The use case this job follows, with its automation and trigger: Hand off a long task.
Why run it on Rush?
On a laptop the slow part of this change is not the ten lines of configuration but the build: a Vapor package compiles its dependency graph from source the first time, and swift test repeats a large part of that after each edit. In a Rush sandbox that cost lands on a machine that is not yours, with the Swift 6.3 toolchain the Dockerfile names, while your checkout and your own .build stay as they were.
The pull request arrives with proof: the three new tests, the swift test output in the transcript, and the docker compose build that shows the release image still builds. One run per service means a team with several Vapor services can hand them all off at once, each in its own sandbox, and read the pull requests when they are ready.
Frequently asked questions
Why not allow any origin?
Because .any with credentials is what browsers refuse and what reviewers rightly block. The agent reads an explicit list from CORS_ALLOWED_ORIGINS and refuses cross-origin requests when it is unset. If your API is public and credential-free and you do want *, say so in the prompt and the agent configures .all and notes the trade-off in the pull request.
Does this need a new Swift package?
No. CORSMiddleware and its Configuration are part of Vapor itself, which Package.swift already depends on from 4.121.4. The manifest and Package.resolved do not change, so the Dockerfile's cached swift package resolve layer stays valid.
Why must the middleware be registered at .beginning?
Vapor adds ErrorMiddleware by default, and middleware runs in registration order. If CORS sits after it, a 404 or 500 reaches the browser without Access-Control-Allow-Origin and the frontend sees an opaque network error instead of the status. Registering with at: .beginning puts CORS first, which is the placement the Vapor documentation recommends.
How are the tests run in CI?
The template's own workflow, test-template.yml, generates a project for every Fluent driver and Leaf combination with the vapor toolbox and runs swift test --package-path against a MySQL or Postgres service. In your generated project, whatever workflow runs swift test on pull requests picks the three new tests up; the agent runs the same command in the sandbox either way and quotes the output.
More Vapor jobs
The same job on other stacks
- Add CORS to an Actix Web API (Actix Web)
- Add CORS to an aiohttp App (aiohttp)
- Add CORS to an Axum API (Axum)