What the agent does
The agent starts by listing the service methods that write more than once, because a transaction changes nothing anywhere else. In a NestJS app like lujakob/nestjs-realworld-example-app (typeorm 0.2.24 in both lockfiles, @nestjs/typeorm 7.0.0, MySQL through the mysql package) they are all in src/article/article.service.ts. create saves the article, then loads the author with relations: ['articles'], pushes the article onto that list and saves the author, so a failure in the second half leaves an article that belongs to nobody. favorite and unFavorite save the user's favorites and then save the article with a favoriteCount they changed in JavaScript. addComment saves the comment and then the article, and deleteComment deletes the comment and then saves the article.
Next it reads how those methods reach the database, since that decides whether a transaction would hold. The service receives four repositories through @InjectRepository, and findAll and findFeed call the global getRepository. Every one of them works on the connection's default manager, which borrows whichever pooled connection is free for each statement. In TypeORM 0.2 a transaction is connection.transaction(async manager => { ... }), and only statements issued through that manager are inside it. A this.articleRepository.save left in the callback commits by itself on another connection, and the method reads as transactional while being nothing of the kind. So the agent injects Connection, which ApplicationModule in src/app.module.ts already takes in its constructor, and rewrites each multi-write method to use manager.getRepository(...) for every read and write inside the callback. It does not use the @Transaction() and @TransactionManager() decorators that 0.2.24 ships: TypeORM's changelog lists them as removed in 0.3.0, and code written with them would have to be rewritten at the next upgrade.
The counter needs a second fix that a transaction does not provide. article.favoriteCount++ followed by save writes an absolute number, so two requests that load the same article both write the same value and one favorite is lost, transaction or not. The agent replaces the pair with manager.increment(ArticleEntity, { id }, 'favoriteCount', 1) and its decrement twin, which make the database do the arithmetic in one UPDATE. It also checks something the code leaves open: favorite loads the user with findOne(id) and no relations, and favorites on UserEntity is a @ManyToMany that is not eager, so before changing anything the agent calls the endpoint and records whether user.favorites is populated or undefined at that point. A rollback cannot be demonstrated on a path that throws before its first write.
Then it proves the behaviour against a real server. The repository's single spec, src/tag/tag.controller.spec.ts, builds its module with TypeOrmModule.forRoot(), so even today npm test needs a database and an ormconfig.json, which .gitignore excludes. The agent installs a MySQL-compatible server from system packages, copies ormconfig.json.example and src/config.ts.example into place, and lets synchronize: true create the tables. New specs make the second write of each method fail and assert that the first one is gone: no article without an author, no row in user_favorites_article beside an unchanged count, no comment missing from its article. One more spec sends favorites for the same article from several users at once and compares favoriteCount with the number of join rows. Each spec is run against the old code first, and the failures are kept.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "Make the multi-step writes in this NestJS and TypeORM application atomic, staying on the installed TypeORM version. Read package.json, both lockfiles, ormconfig.json.example, src/app.module.ts, every entity and every service first. List each service method that issues more than one write and, for each, what is left in the database today if a later write fails. Install a database server of the kind the TypeORM config names from system packages, say which server and version you installed, create the config files from their committed examples and run the existing tests. Before changing a method, call its endpoint once and record what it does today, including any error. Write a failing integration test for each method first: force the later write to fail and assert that the earlier write did not persist. Then run each method inside connection.transaction and use the manager passed to the callback for every read and write in it; no injected repository and no global getRepository may be used inside a transaction. Do not use the @Transaction or @TransactionManager decorators. Replace any counter that is read, changed in JavaScript and saved with an atomic increment or decrement, and add a test that sends concurrent requests and compares the counter with the rows it counts. Keep response bodies and status codes as they are, leave single-write methods alone and list them. Open a pull request that lists each method with its statements before and after, the tests failing on the old code and passing on the new, and anything you found broken that you did not fix.",
"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
A pull request that touches src/article/article.service.ts, its module if the provider list changes, and new spec files. The description has one entry per method: the statements it issued before, in order, what a failure after each one used to leave behind, and the statements it issues now inside one START TRANSACTION and COMMIT. The counter change is called out separately, with the concurrent test that fails on the read-then-save version.
A second section lists what was not wrapped and why. update and delete issue one write each. UserService.create checks for an existing username or email with a query and then inserts, which is a race that a transaction at the default isolation level does not close; the description says it needs a unique index and leaves it for that change. Anything the agent found broken while calling the endpoints, such as a relation that is never loaded, is reported with the request and the response it got, not silently repaired.
The reviewer's check is mechanical. Inside every transaction callback there should be no this. repository and no getRepository call, only the manager. The description names the functions it traced, and the test output from the sandbox's database server, with its name and version, is pasted in full.
Run it on every half-applied write found in the data
This job usually starts with a row that should not exist: an article with a null author, a count that disagrees with its join table. The developer who finds it sends this body to POST /o/{org}/sessions from a terminal or an editor's MCP client, adds one sentence naming the endpoint, and goes back to what they were doing. A request_id, a UUID generated once for the job, makes a retried call return the run it already started.
A team with several services can send one request per repository from a script, each with its own UUID as request_id. A key runs as many at once as its max_concurrent allows and the rest wait as queued runs. Each pull request stands alone, because each service has its own entities and its own failure paths.
The use case this job follows, with its automation and trigger: Hand off a long task.
Why run it on Rush?
Whether a write path is atomic is only known by breaking it halfway and reading the tables. That takes a database server, a schema, and the freedom to make statements fail on purpose. The agent has all three in its own sandbox, and every command it runs is kept with its output in the transcript at GET /o/{org}/sessions/{id}/messages. The lost favorite is the same: it shows up only when several requests really run at once against a real server.
The change itself is repetitive and easy to get subtly wrong, one forgotten repository call at a time. Done as a run, it comes back as a branch where each method has a test that failed before and passes after, while the developer's own checkout and local database stay as they were.
Frequently asked questions
Why not a QueryRunner with startTransaction and commitTransaction?
It works and gives finer control, and it makes you responsible for rollbackTransaction and release on every path. connection.transaction commits when the callback resolves, rolls back when it rejects, and releases the connection either way. The agent reaches for a query runner only when a transaction has to outlive one function.
Does this still work after upgrading to TypeORM 0.3?
Yes, with a rename. In 0.3 the injected object is a DataSource and the call is dataSource.transaction(async manager => ...); the callback body is unchanged. The transaction decorators are the part that does not survive, which is why the agent avoids them.
Will a transaction stop two users from registering the same email?
No. A check followed by an insert races even inside a transaction at MySQL's default isolation level, because both transactions can pass the check before either inserts. A unique index is what refuses the second insert, and the agent reports that gap instead of pretending the transaction covers it.
Can it use a library that propagates transactions for me?
It can if you ask. Packages that carry the active transaction through async context remove the need to pass a manager around, and they add a dependency that patches repository behaviour. On a service this size the explicit manager is a few lines per method and keeps the boundary visible in review.