Skip to content

fix(desktop): snap caret out of agent mention chips#2712

Open
marcoleejr wants to merge 1 commit into
block:mainfrom
marcoleejr:fix/agent-mention-caret-snap
Open

fix(desktop): snap caret out of agent mention chips#2712
marcoleejr wants to merge 1 commit into
block:mainfrom
marcoleejr:fix/agent-mention-caret-snap

Conversation

@marcoleejr

Copy link
Copy Markdown

Summary

Fixes #2707.

With Keep addressed agents active (persistent auto-tag), the composer re-inserts @AgentName after each send. Agent mentions are decorations on plain text, not atom nodes, and the leading @ is rendered at width: 0. A click that looks like the start of the chip often lands inside the display name; the next keystrokes mutate that name, the decoration dies, and the agent is no longer tagged.

This PR treats the interior of an agent mention as non-editable caret space:

  1. snapCaretOutOfAgentMention — if the empty caret is strictly inside @Name, move it just after the mention (consuming the trailing space hydration always inserts).
  2. appendTransaction — apply that snap after selection changes (clicks, arrows).
  3. handleTextInput — if input would land inside a chip, insert after the chip instead of corrupting the name.

Edge positions stay put (before @ for Backspace, after name so typing after a chip without a trailing space still works). Human-only mentions are unchanged.

How it was implemented

  • Pure helpers in mentionHighlightExtension.ts (findAgentMentionRanges, snapCaretOutOfAgentMention) so the policy is unit-testable without the full editor.
  • Wired into the existing TipTap plugin that already owns agent decoration ranges — single source of truth for “what is an agent chip”.

Test plan

  • Unit tests for range finding + caret snap (interior → after trailing space; edges untouched; multi-agent; empty agent list)
  • node --test src/features/messages/lib/mentionHighlightExtension.test.mjs — 29/29 pass
  • Manual (desktop): enable keep-addressed-agents, @ an agent in a thread, send a few messages, click the chip / place caret mid-name, type — text should land after the chip and the tag should remain intact

Notes

Out of scope: turning mentions into real atom nodes (larger rewrite). This is the minimal fix for the auto-tag breakage path reported in #2707.

Agent mentions in the composer are decorations over plain text, not atom
nodes, and the leading @ is width:0. With persistent auto-tag, clicks that
look like the start of a chip often land inside the display name; typing
then mutates the name and drops the decoration so the agent stops being
tagged.

Snap an empty caret out of the interior of agent mentions (after the
trailing space hydration inserts), and rewrite text input that would land
inside a chip so it inserts after the chip instead.

Fixes block#2707
@marcoleejr
marcoleejr requested a review from a team as a code owner July 24, 2026 12:00
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.

[Bug] when using auto-tag agent, the cursor often slips back into the agents name, and typing breaks it

1 participant