Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

fix(api,mcp): validate comment anchors against version HTML at creati… - #327

Open
ashutoshb206 wants to merge 1 commit into
razorpay:masterfrom
ashutoshb206:fix/comment-anchor-validation
Open

ashutoshb206 wants to merge 1 commit into
razorpay:masterfrom
ashutoshb206:fix/comment-anchor-validation

Conversation

@ashutoshb206

Copy link
Copy Markdown
Contributor

What and why

POST /v1/documents/{id}/comments (and by extension CLI docs comments create and MCP create_comment) previously accepted any non-empty quote without checking it against the target version's HTML, returning 201 Created with current_placement.status: "attached" and confidence: 1 even when the quote did not exist in the document or appeared multiple times without disambiguation. Additionally, commentPlacement skipped checking when placementVersionID == cm.VersionID and searched raw HTML with strings.Contains instead of rendered document text.

This change:

  • Adds rendered-text extraction (extractRenderedText in api/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-16 start_offset/end_offset alignment, and masks <script>, <style>, and <noscript> content with NUL bytes.
  • Validates anchors on comment creation (validateAnchorForCreate in handlers_comments.go):
    • Verifies version_id belongs to document_id (404 Not Found otherwise).
    • Returns 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 provided prefix/suffix).
    • Returns 400 Bad Request (error.code: "anchor_ambiguous") when the quote occurs multiple times and is not uniquely disambiguated by prefix/suffix or start_offset/end_offset.
    • Passes the extracted rendered text into commentJSON for the 201 response instead of an empty string.
  • Updates commentPlacement in api/internal/server/json.go to resolve placement against extracted rendered text (including on the creation version when HTML is available), reporting orphaned (confidence: 0) for missing quotes and confidence: 0.5 for un-disambiguated repeated quotes.

Closes #198

How it was tested

  • Added unit tests in api/internal/server/anchor_test.go covering extractRenderedText (inline tags, <br>, <script>/<style>, UTF-16 offsets with emoji), validateAnchorForCreate (missing quote, unique quote, ambiguous quote, disambiguation via prefix and offsets), and commentPlacement.
  • Added HTTP and MCP integration tests in api/internal/server/server_comments_test.go (TestCreateCommentValidatesAnchorAgainstVersionHTML) and api/internal/server/server_mcp_test.go.
  • Ran locally:
    • cd api && go test ./internal/server -run "TestExtractRenderedText|TestValidateAnchorForCreate|TestCommentPlacement" -v (13/13 subtests passed)
    • cd api && go vet ./... (passed)
    • Note: Postgres-backed integration tests in internal/server were skipped locally because Docker / SLASHDOCS_TEST_DATABASE_URL is not running on this machine; CI will run the full suite.

Checklist

  • make lint and make test pass locally
  • Tests added or updated for the change
  • Docs updated (README.md, docs/, .env.example, chart values.yaml) if behaviour or configuration changed
  • No breaking change, or the migration path is described below

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Comment anchors are not validated at creation: a quote missing from the document (or repeated) is reported attached with confidence 1

1 participant