# Run a Large Refactor on a Rails Codebase

An agent extracts rubygems.org's gem push flow into GemPushService in two commits with 8 new tests; one push test failed before and after, intermittently.

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

## What the agent does

The agent read `app/controllers/api/v1/rubygems_controller.rb`, `app/models/pusher.rb` and the repository's `AGENTS.md`. The controller's `create` action parsed the multipart upload and its attestations inline, built a `Pusher`, called `process` and handed the pusher to `track_gem_push`.

Commit one adds `app/services/gem_push_service.rb`: `call` runs `Pusher` and returns an immutable `Result` (message, code, rubygem, version), which the controller and its tracking now read. Commit two moves request parsing into `GemPushService.from_request`, which raises `InvalidRequest` for a non-file `gem`, missing attestations or a non-array; the controller renders that as 400 with the same messages. `test/services/` gains 3 service tests and 5 parsing tests.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Refactor this codebase: extract the gem push flow into a service object with its own tests. Work in small commits, run the build and tests after each, and stop at needs_review with a summary if a step cannot keep the tests green. Open a pull request listing each commit.",
  "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

Tried on rubygems/rubygems.org at 96a4bd1f7: 4 files changed, 169 insertions(+), 20 deletions(-) across two commits. The clean baseline ran `5627 runs, 17208 assertions, 2 failures`; after the change, `5635 runs, 17242 assertions, 1 failures`. The one failure, `PusherTest#test_rejects_a_push_to_an_ownerless_fully_yanked_gem_when_the_api_key_has_an_unexpected_owner_type`, failed in the baseline too and in two of the run's later runs, passing in two others, so the run calls it flaky. `bin/rubocop app test` found `no offenses detected`. No pull request was opened.

For the reviewer: `track_gem_push` stays in the controller, and `app/services/` is a new directory with no prior convention. Setup took Ruby 4.0.9 built from source, Postgres 15 where the repository pins 18, memcached and OpenSearch from a tarball, plus `bin/rails tailwindcss:build` and `config/database.yml` copied from the gitignored sample. Rubocop ran only after the change, not on the baseline.

## Run it on every refactor a teammate starts from the console

A teammate starts this prompt from the console in the Project that holds the repository. Anyone in the org can read the run while the suite runs after each commit; its starter or an org admin answers the run's open question as a follow-up, here whether `track_gem_push` should move in a third commit.

## Why run it on Rush?

On a suite of 5,600 tests, a failure is only useful with its baseline. This run took the baseline on a pristine worktree, threw out an earlier run it had contaminated by editing mid-suite, and named the one push test that fails with or without the refactor instead of hiding it.

## Frequently asked questions

### Did the stop at needs_review trigger?

No. The push tests ended `285 runs, 1182 assertions, 0 failures` after commit two; after commit one they had one failure, the same `PusherTest` case that failed on the untouched code. A second baseline failure, in `StoreVersionContentsJobTest`, passed after the change.

### Does the API behave differently?

The 400 bodies are unchanged. Malformed multipart uploads now raise `InvalidRequest`, rescued before the generic `StandardError` rescue, and a missing `gem` parameter still reaches the generic rescue as a 500, as before.

## More Rails jobs

- [Turn Linear Issues Into Pull Requests for a Rails App](https://getrush.ai/run/linear-issue-to-pr/rails/)
- [Make a Database Schema Change in a Rails App as a Team](https://getrush.ai/run/pair-on-schema-change/rails/)
- [Plan a Linear Issue Against a Ruby on Rails Codebase](https://getrush.ai/run/plan-linear-issues/rails/)

## The same job on other stacks

- [Run a Large Refactor on an Elixir Codebase](https://getrush.ai/run/large-refactor/elixir/)
- [Run a Large Refactor on a Go Codebase](https://getrush.ai/run/large-refactor/go/)

## More jobs like this: Steer a run as a team

- [Migrate a Drizzle Schema from SQLite to PostgreSQL](https://getrush.ai/run/migrate-sqlite-to-postgres/drizzle/)

## 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/)
