Skip to content

fix(doc-api): use textRanges[0] instead of highlightRange for comment anchoring#3224

Open
bjohas wants to merge 1 commit into
superdoc-dev:mainfrom
OpenDevEd:fix/doc-api-highlightrange-comment-anchor
Open

fix(doc-api): use textRanges[0] instead of highlightRange for comment anchoring#3224
bjohas wants to merge 1 commit into
superdoc-dev:mainfrom
OpenDevEd:fix/doc-api-highlightrange-comment-anchor

Conversation

@bjohas
Copy link
Copy Markdown
Contributor

@bjohas bjohas commented May 11, 2026

Summary

The superdoc_comment tool description instructs LLMs to build target.range from result.highlightRange. But highlightRange is computed relative to the snippet preview string, not the block — it is offset by up to SNIPPET_PADDING = 30 characters:

// super-editor/.../find/common.ts
const snippetFrom = Math.max(0, matchFrom - SNIPPET_PADDING);
highlightRange: { start: prefix.length, end: prefix.length + matchNormalized.length }

Following the description literally produces silently mis-anchored comments (off by ~30 chars) or TARGET_NOT_FOUND errors when the offset lands past the end of the block.

The correct field is result.items[0].context.textRanges[0] — a complete block-relative TextAddress ({kind:"text", blockId, range:{start,end}}) that can be passed directly as target. The document-api's own integration test (overview-examples.test.ts:537) already uses this pattern.

Changes

  • packages/document-api/src/contract/operation-definitions.ts — one-line description fix: replace the highlightRange instruction with context.textRanges[0] and add an explicit warning not to use highlightRange.

The generated apps/mcp/src/generated/catalog.ts and mcp-prompt.ts will pick up the corrected wording when pnpm run generate:all is run.

Test plan

  • pnpm run generate:all — catalog and prompt regenerate cleanly.
  • Manual: superdoc_search → use result.items[0].context.textRanges[0] as target in superdoc_comment action:"create" — comment anchors correctly.
  • Manual (regression): verify that using highlightRange still mis-anchors (so the fix is needed and the warning is accurate).

🤖 Generated with Claude Code

… anchoring

highlightRange is snippet-relative (offset by up to SNIPPET_PADDING=30
chars from the start of the block). Telling LLMs to build target.range
from it produces silently mis-anchored comments or TARGET_NOT_FOUND
errors.

The correct field is context.textRanges[0], which is a complete
block-relative TextAddress ({kind:"text", blockId, range:{start,end}})
and can be passed straight through as the comment target.

Update the superdoc_comment tool description accordingly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@bjohas bjohas requested a review from a team as a code owner May 11, 2026 13:44
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 16fca2d306

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

description:
'Manage document comment threads: create, read, update, and delete. ' +
'To create a comment, first use superdoc_search to find the target text, then pass action "create" with the comment text and a target: {kind:"text", blockId:"<blockId>", range:{start:<N>, end:<N>}} using the blockId and highlightRange from the search result. ' +
'To create a comment, first use superdoc_search to find the target text, then pass action "create" with the comment text and a target built from the search result: use result.items[0].context.textRanges[0] directly as the target — it is already a complete {kind:"text", blockId, range:{start,end}} block-relative address. Do NOT use result.highlightRange — it is offset relative to the snippet preview (up to 30 chars off) and will mis-anchor the comment or trigger TARGET_NOT_FOUND. ' +
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reference an actual search result field

When an agent follows this new superdoc_comment guidance after superdoc_search, result.items[0].context is undefined: I checked packages/sdk/langs/browser/src/intent-dispatch.ts, where superdoc_search dispatches to doc.query.match, and the query.match schema defines text match items with target, highlightRange, and blocks, but no context or textRanges. That makes the instructed target undefined or invalid, so comment creation fails for the exact search-then-comment workflow this description documents; point to a real query.match field such as the item’s blocks range data or otherwise expose the desired text address.

Useful? React with 👍 / 👎.

@caio-pizzol caio-pizzol self-assigned this May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants