Skip to content

fix(session-ui): preserve caret after prompt editor rebuild - #42297

Open
gold-beyond wants to merge 1 commit into
anomalyco:devfrom
gold-beyond:caret-after-mention
Open

fix(session-ui): preserve caret after prompt editor rebuild#42297
gold-beyond wants to merge 1 commit into
anomalyco:devfrom
gold-beyond:caret-after-mention

Conversation

@gold-beyond

@gold-beyond gold-beyond commented Aug 13, 2026

Copy link
Copy Markdown

Issue for this PR

Fixes #42335

Related to #37037 / #36210 (caret jumps around in the prompt input while the
session updates). This PR fixes the variant I could reproduce and verify on the
current v2 composer: inserting a @mention mid-text on the session page
collapses the caret to the END of the prompt instead of keeping it right after
the mention. I could not reproduce the streaming-output case from #37037 on
current dev, so I only claim the mention-insertion scenario here.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

renderPromptInputV2Editor rebuilds the editor DOM (replaceChildren) whenever
the prompt store changes outside a local input (mention insertion, history
navigation, followup edit). After the rebuild it always collapsed the caret to
the end of the editor, ignoring the cursor position stored in the prompt store.
Reproduced on the session page: after inserting @src/index.ts mid-text, the
caret lands at offset 32 (end of a 32-char prompt) instead of 21 (right after
the mention).

Changes in packages/session-ui/src/v2/components/prompt-input:

  • interaction.ts: expose cursor() on the controller so the editor can read
    the authoritative stored caret position.
  • index.tsx: restore the caret from the stored cursor after a rebuild, falling
    back to the pre-rebuild DOM caret when no cursor is stored. Mentions are
    uneditable spans that still occupy their text length, so
    setPromptInputV2Cursor places the caret before/after a mention, never inside
    it.

How did you verify your code works?

  • New e2e regression test packages/app/e2e/regression/prompt-caret-after-mention.spec.ts:
    types "please fix the bug", moves the caret to offset 7, inserts
    @src/index.ts mid-text on a session page, and asserts the caret lands at
    offset 21. It failed before the fix (caret at 32) and passes after.
    Run with: bunx playwright test e2e/regression/prompt-caret-after-mention.spec.ts
  • bun run typecheck in packages/session-ui passes.
  • bun test src in packages/session-ui: 83 tests pass.
  • Existing prompt e2e (prompt-input-v2-command-draft) still passes.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

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.

Caret jumps to end of prompt after inserting an @mention on the session page

1 participant