What the agent does
The agent reads the one file that talks to the database. In Automattic/wp-telegram-bot, a Telegram bot that forwards new WordPress.com posts to chats, database.js is 76 lines. It connects lazily in dbP, which calls MongoClient.connect(DB_URL), picks client.db(process.env.MONDODB_DATABSE), and on line 21 calls db.collection('blogChats').ensureIndex({chatId: 1, blogPath: 1}, {unique: true}). followBlog inserts with .insert(document) on line 36, unfollowBlog calls .remove(criteria, {justOne: true}) on line 44 and returns response.result && response.result.n, and clearFollowedBlogs does the same with .remove(criteria) on line 66. index.js calls these five functions from the bot's command handlers. package.json pins mongodb at ^3.6.3, package-lock.json holds 3.7.4, and .nvmrc says Node 8.9.3.
A bump to the current driver, 6.22.0, installs without complaint, and the first database call crashes the process. Run against a MongoDB with the driver at 6, followBlog ends with TypeError: db.collection(...).ensureIndex is not a function from line 21, and because dbP wraps the connection in a new Promise that never passes the inner rejection to its reject, the error surfaces as an unhandled rejection that stops Node rather than as a rejected call the handler could catch. The removals happened in two steps: driver 4.0.0, published 2021-07-13, dropped ensureIndex and kept insert and remove; driver 5.0.0, published 2023-01-31, dropped insert and remove as well. On 3.7.4 all three exist, and remove returns a result whose result.n is the deleted count, which is what two of the functions read.
The replacements are one each: createIndex for ensureIndex with the same keys and {unique: true}, insertOne for insert of one document, deleteOne for remove with justOne, and deleteMany for remove without it. The return shapes change with them, and that is where the agent spends its care. deleteOne and deleteMany return {acknowledged, deletedCount} with no result, so the two functions that returned response.result.n return response.deletedCount instead; insertOne returns {acknowledged, insertedId}. The database name needs no change: with MONDODB_DATABSE unset, as the misspelled variable usually is, client.db(undefined) on driver 6 resolves to the database named in the connection string, wp-telegram-bot.
The proof is a MongoDB started inside the sandbox and the ported module driven against it: createIndex builds the unique index, insertOne stores a document, a second insert of the same chat and blog is refused with error code 11000, deleteOne on a filter that matches two documents removes one and reports deletedCount: 1, and deleteMany removes the rest. The agent also writes down the promise wrapper's flaw and fixes it in passing, by returning the connection promise directly so a failure inside it rejects dbP instead of killing the process.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "Our bot crashes on its first database call with \"TypeError: db.collection(...).ensureIndex is not a function\" after the mongodb package was upgraded. Find every call to a removed driver method in the project (ensureIndex, insert, update, remove, save, findAndModify, count) with file and line, and every place that reads the old result shape such as result.n or result.ok. Read package.json and the lock to say which driver was locked and which is installed now, and confirm by installing releases which major removed each method. Replace each call with its named replacement (createIndex, insertOne or insertMany, updateOne or updateMany, deleteOne or deleteMany, findOneAndUpdate, countDocuments), choosing one-document or many-document forms from how each call was used, and change each caller that read the old result to read deletedCount, insertedId or the matching field. Keep the index keys and options identical. Start a MongoDB in the sandbox and drive each changed function against it, including a duplicate insert against the unique index. If a promise wrapper swallows rejections from the connection step, say so and fix it. Open a pull request with the diff, the version findings and the output of the database checks.",
"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 rewrites database.js in six places: the ensureIndex on line 21, the insert on line 36, the two remove calls on lines 44 and 66, and the two returns that read response.result.n, which become response.deletedCount. The dbP wrapper returns the connection promise rather than hiding it inside new Promise. package.json and the lock move the driver to 6. The description names the locked 3.7.4, the two majors that removed the methods and their dates, and the old and new return shapes side by side.
Below that is the output of the sandbox MongoDB session: the index creation, the stored document, the refused duplicate with code 11000, and the two delete results with their counts. The reviewer checks that each remove became the right form, deleteOne only where justOne was passed, that the unique index still covers chatId and blogPath, and that index.js reads the values the changed functions now return. The reviewer also confirms .nvmrc, which names Node 8.9.3: driver 6 does not run there, so the deploy target's Node version moves with this change or the change waits.
What the run does not cover is Telegram and WordPress.com. The bot's handlers in index.js call these functions in response to chat commands and XMPP notifications, and neither service is reachable from the sandbox; the first /follow after deploy is the reviewer's check.
Run it on every dependency bump that breaks on the first database call
This error does not appear at install or at start-up; it appears at the first command a user sends, as a crash in the process log. A process manager or hosting platform that reports crashes usually cannot add the Authorization: Bearer header a Rush webhook trigger needs, so a handler on your server receives its notification and forwards the log text to the trigger's URL with the trigger's secret. The text lands after the saved prompt as the event, so the prompt tells the agent to read the removed method's name from it. Send an Idempotency-Key header to the trigger's URL built from the crash id, so a bot that restarts and crashes again starts one run.
A repository with a dependency bot can start earlier: add a CI step that runs the database functions against a MongoDB service container, and have it call the same trigger on failure with the pull request's head commit as the key. The bump then starts the port before anything deploys.
The use case this job follows, with its automation and trigger: Fix a bug from an alert.
Why run it on Rush?
The driver removed these methods in two majors two years apart, and a laptop with the old lock shows neither change. A Rush sandbox installs the bumped driver, starts a MongoDB beside the code, and makes the first call fail the way it fails in production, including the unhandled rejection that takes the process down. The agent then installs 3.7.4, 4.0.0, 4.17.2, 5.0.0 and 6.0.0 to say which major removed what, with each check kept in the session.
A port like this is judged on return shapes, not method names. deleteOne returning deletedCount where remove returned result.n is the kind of difference that compiles, lints and passes a review, and only shows when a caller reads a value that is no longer there. The pull request carries the real results of each call against a server, so the reviewer compares fields rather than trusting a rename.
Frequently asked questions
Which driver release removed ensureIndex, insert and remove?
Two of them. mongodb 4.0.0, published 2021-07-13, removed ensureIndex and kept insert and remove; mongodb 5.0.0, published 2023-01-31, removed insert and remove. On 3.7.4 all three exist. A bump straight from 3 to 6 meets both removals at once.
Why does the bot crash instead of logging the error?
Because of the connection wrapper. dbP creates a new Promise with only resolve and returns the connection chain inside it, so when ensureIndex throws in that chain nothing rejects the outer promise, and Node treats it as an unhandled rejection and exits. Returning the chain directly makes the failure a rejected call the handler can catch.
What replaces remove with justOne?
deleteOne with the same filter; remove without justOne becomes deleteMany. Both return {acknowledged, deletedCount}, so a caller that read result.n reads deletedCount instead. Driven against a MongoDB in the sandbox, deleteOne on a filter matching two documents removes one and reports a count of 1.
Does the misspelled database variable matter?
Not for the port. With MONDODB_DATABSE unset, client.db(undefined) on driver 6 resolves to the database named in the connection string, wp-telegram-bot, which is what the default DB_URL carries. The pull request notes the spelling so whoever sets it in production knows which name is read.
Can I stay on driver 3 instead?
You can pin mongodb at ^3, and the code works as it is. The project's .nvmrc names Node 8.9.3, which is where that driver was tested, so the pin and the Node version tend to move together; the pull request names both so the decision is one decision.
More jobs like this: Fix a bug from an alert
- Fix ERR_PNPM_OUTDATED_LOCKFILE When CI Runs pnpm install (pnpm)
- Fix "ESLint couldn't find an eslint.config.* file" after upgrading to ESLint 9 (ESLint)
- Fix "TrainingArguments.__init__() got an unexpected keyword argument 'evaluation_strategy'" (Transformers)
- Fix "expected exactly 5 fields, found 6" After Upgrading robfig/cron to v3 (robfig/cron)
- Fix "error: externally-managed-environment" in a Docker Build (Docker)
- Fix the Manifest Behind a Failed Argo CD Sync (Argo CD)