fix: report per-index write errors and inserted count when insert-many partially fails - #1386
Open
innolope-dev wants to merge 1 commit into
Open
Conversation
…y partially fails A MongoBulkWriteError from insert-many was flattened to a single generic message, losing which documents were inserted and which failed. With the driver's default ordered inserts a failure is usually partial: everything before the first failing index is inserted, everything after it is skipped — but the caller could not tell. InsertManyTool now overrides handleError for MongoBulkWriteError and reports the inserted count, each failing index with its error code and (redacted) message via formatUntrustedData, the ordered-skip semantics, and how to retry with only the documents that were not inserted. The per-index list is capped at 10 entries to bound the response size.
innolope-dev
requested review from
himanshusinghs
and removed request for
a team
July 23, 2026 20:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
When
insert-manyhits aMongoBulkWriteError, the generic error handler flattens it to a single message. That loses the two things an agent needs to recover: how many documents actually made it in, and which indices failed. With the driver's default ordered inserts a failure is usually partial — everything before the first failing index was inserted, everything after it was silently skipped — so the LLM can neither tell the user what state the collection is in nor retry with only the missing documents (retrying the whole batch would duplicate the already-inserted ones).This PR adds a
handleErroroverride toInsertManyToolthat, forMongoBulkWriteError, reports:formatUntrustedDatasinceerrmsgcan contain document values,The per-index list is capped at 10 entries so a batch with many failures can't blow up the response. All other errors fall through to
super.handleErrorunchanged.Example response for a 3-document batch where index 1 collides with an existing
_id:Notes:
api-extractor/reports/tools.public.api.mdregenerated viapnpm run update:api(the protected override shows up in the tools report).Checklist