Repository navigation
fix(api,mcp): validate comment anchors against version HTML at creati… - #327
Open
ashutoshb206 wants to merge 1 commit into
Open
ashutoshb206 wants to merge 1 commit into
ashutoshb206 wants to merge 1 commit into
Conversation
ashutoshb206
requested review from
anuragrao04 and
razvi-razorpay
as code owners
December 2, 2026 18:29
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.
What and why
POST /v1/documents/{id}/comments(and by extension CLIdocs comments createand MCPcreate_comment) previously accepted any non-emptyquotewithout checking it against the target version's HTML, returning201 Createdwithcurrent_placement.status: "attached"andconfidence: 1even when the quote did not exist in the document or appeared multiple times without disambiguation. Additionally,commentPlacementskipped checking whenplacementVersionID == cm.VersionIDand searched raw HTML withstrings.Containsinstead of rendered document text.This change:
extractRenderedTextinapi/internal/server/anchor.go) matching the frontend render bridge (renderedTextOf): walks<body>in document order, replaces<br>with a single space while tracking offsets for UTF-16start_offset/end_offsetalignment, and masks<script>,<style>, and<noscript>content with NUL bytes.validateAnchorForCreateinhandlers_comments.go):version_idbelongs todocument_id(404 Not Foundotherwise).400 Bad Request(error.code: "anchor_not_found") when the quote does not occur in the version's rendered text (or does not match the providedprefix/suffix).400 Bad Request(error.code: "anchor_ambiguous") when the quote occurs multiple times and is not uniquely disambiguated byprefix/suffixorstart_offset/end_offset.commentJSONfor the201response instead of an empty string.commentPlacementinapi/internal/server/json.goto resolve placement against extracted rendered text (including on the creation version when HTML is available), reportingorphaned(confidence: 0) for missing quotes andconfidence: 0.5for un-disambiguated repeated quotes.Closes #198
How it was tested
api/internal/server/anchor_test.gocoveringextractRenderedText(inline tags,<br>,<script>/<style>, UTF-16 offsets with emoji),validateAnchorForCreate(missing quote, unique quote, ambiguous quote, disambiguation viaprefixand offsets), andcommentPlacement.api/internal/server/server_comments_test.go(TestCreateCommentValidatesAnchorAgainstVersionHTML) andapi/internal/server/server_mcp_test.go.cd api && go test ./internal/server -run "TestExtractRenderedText|TestValidateAnchorForCreate|TestCommentPlacement" -v(13/13 subtests passed)cd api && go vet ./...(passed)internal/serverwere skipped locally because Docker /SLASHDOCS_TEST_DATABASE_URLis not running on this machine; CI will run the full suite.Checklist
make lintandmake testpass locallyREADME.md,docs/,.env.example, chartvalues.yaml) if behaviour or configuration changed