createComment: anchor failures give no corrective hint — port editPageText's self-correction affordances (closest-block hint, markdown-strip, multi-block detection) #408
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
createCommentis the top real-error hotspot in agent dialog history: 96 failed invocations across 29 chats (2026-06-17…07-07 DB analysis, seedocs/reading-ai-logs.md; one chat shows 10 attempts / 0 successes — blind retry loops). The failure is always anchoring: the agent-suppliedselectionis not found (or ambiguous) in the document.Throwing is intentional (live retry — see the comment in
apps/server/src/core/ai-chat/tools/ai-chat-tools.service.ts:442), but the error text gives the model nothing to correct with, unlikeeditPageText, whose soft-failure design keeps its error count at 4/79:packages/mcp/src/lib/json-edit.ts)packages/mcp/src/client.ts,packages/mcp/src/lib/comment-anchor.ts)**bold**in the locator)warning)Fix sketch
client.ts createComment:The page JSON is already loaded in the pre-check (
getPageJson, client.ts:2545) andblockPlainTextexists innode-ops.ts— the hint is cheap. Consider extracting the hint builder from json-edit.ts into a shared helper instead of copying.comment-anchor.ts(canAnchorInDoc/countAnchorMatches/applyAnchorInDoc/getAnchoredText): when the raw selection does not match, retry with markdown syntax stripped (same rules as json-edit) and anchor on the raw doc text via the existingnormalizeForMatchindex map. Report a softwarninglike editPageText does. All four functions must stay consistent (count/can/apply/get must agree on what matches — the suggestion-uniqueness gate at client.ts:2551/2677 depends on it).packages/mcp/src/tool-specs.ts:768, shared speccreateComment): add "copy theselectionverbatim from getPage/searchInPage output — do not quote from memory" (stale-memory quoting is the root cause of most misses).Notes