Skip to content

fix(desktop): bypass Chromium editing pipeline for large paste & select-delete - #45812

Merged
OutThisLife merged 1 commit into
mainfrom
bb/composer-large-paste-lag
Jun 13, 2026
Merged

fix(desktop): bypass Chromium editing pipeline for large paste & select-delete#45812
OutThisLife merged 1 commit into
mainfrom
bb/composer-large-paste-lag

Conversation

@OutThisLife

@OutThisLife OutThisLife commented Jun 13, 2026

Copy link
Copy Markdown
Collaborator

Summary

Large paste and Ctrl+A → Delete froze the composer for seconds. Both hit Chromium's contenteditable editing pipeline (~O(n²) on multiline DOM) — a browser cost, same in prod.

Fix lives in rich-editor.ts:

  • insertPlainTextAtCaret — Range + text/<br> fragment (paste)
  • deleteSelectionInEditorrange.deleteContents() for non-collapsed Backspace/Delete
  • Shared composerSelectionRange; composer flushes via existing flushEditorToDraft

Collapsed-caret deletes (incl. Opt/Cmd word/line delete) still native.

Profiled live (47 KB / 122 paragraphs)

Op Before After
Paste (onPaste) ~4474 ms ~13 ms
Ctrl+A → Delete ~1304 ms ~4 ms

Test plan

  • vitest run --environment jsdom rich-editor.test.ts — 7/7
  • tsc --noEmit clean
  • Paste large doc; Ctrl+A → Delete; single Backspace / word-delete unchanged

@github-actions

github-actions Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

🔎 Lint report: bb/composer-large-paste-lag vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 10896 on HEAD, 10896 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 5726 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

@OutThisLife OutThisLife changed the title fix(desktop): drop execCommand from paste — fixes multi-second composer freeze fix(desktop): stop routing large paste & selection-delete through Chromium's editing pipeline Jun 13, 2026
@OutThisLife
OutThisLife force-pushed the bb/composer-large-paste-lag branch from 91a1885 to d040972 Compare June 13, 2026 20:44
…ct-delete

Large paste and Ctrl+A → Delete froze the composer for seconds — both routed
through Chromium's contenteditable editing pipeline (~O(n²) on multiline DOM).

- insertPlainTextAtCaret: Range + text/<br> fragment (paste path)
- deleteSelectionInEditor: range.deleteContents for non-collapsed Backspace/Delete
- Shared composerSelectionRange helper; both flush via flushEditorToDraft

Profiled live (47 KB / 122 paragraphs): paste 4474 ms → 13 ms; select-delete
1304 ms → 4 ms. Collapsed-caret deletes still native.
@OutThisLife
OutThisLife force-pushed the bb/composer-large-paste-lag branch from d040972 to 1801559 Compare June 13, 2026 20:44
@OutThisLife OutThisLife changed the title fix(desktop): stop routing large paste & selection-delete through Chromium's editing pipeline fix(desktop): bypass Chromium editing pipeline for large paste & select-delete Jun 13, 2026
@OutThisLife
OutThisLife enabled auto-merge (squash) June 13, 2026 20:45
@OutThisLife
OutThisLife merged commit 0a865e5 into main Jun 13, 2026
25 checks passed
@OutThisLife
OutThisLife deleted the bb/composer-large-paste-lag branch June 13, 2026 20:50
AIalliAI pushed a commit to AIalliAI/Hermes that referenced this pull request Jun 14, 2026
…ct-delete (NousResearch#45812)

Large paste and Ctrl+A → Delete froze the composer for seconds — both routed
through Chromium's contenteditable editing pipeline (~O(n²) on multiline DOM).

- insertPlainTextAtCaret: Range + text/<br> fragment (paste path)
- deleteSelectionInEditor: range.deleteContents for non-collapsed Backspace/Delete
- Shared composerSelectionRange helper; both flush via flushEditorToDraft

Profiled live (47 KB / 122 paragraphs): paste 4474 ms → 13 ms; select-delete
1304 ms → 4 ms. Collapsed-caret deletes still native.
T02200059 pushed a commit to T02200059/hermes-agent that referenced this pull request Jun 18, 2026
…ct-delete (NousResearch#45812)

Large paste and Ctrl+A → Delete froze the composer for seconds — both routed
through Chromium's contenteditable editing pipeline (~O(n²) on multiline DOM).

- insertPlainTextAtCaret: Range + text/<br> fragment (paste path)
- deleteSelectionInEditor: range.deleteContents for non-collapsed Backspace/Delete
- Shared composerSelectionRange helper; both flush via flushEditorToDraft

Profiled live (47 KB / 122 paragraphs): paste 4474 ms → 13 ms; select-delete
1304 ms → 4 ms. Collapsed-caret deletes still native.
waefrebeorn pushed a commit to waefrebeorn/slermes that referenced this pull request Jul 2, 2026
…ct-delete (NousResearch#45812)

Large paste and Ctrl+A → Delete froze the composer for seconds — both routed
through Chromium's contenteditable editing pipeline (~O(n²) on multiline DOM).

- insertPlainTextAtCaret: Range + text/<br> fragment (paste path)
- deleteSelectionInEditor: range.deleteContents for non-collapsed Backspace/Delete
- Shared composerSelectionRange helper; both flush via flushEditorToDraft

Profiled live (47 KB / 122 paragraphs): paste 4474 ms → 13 ms; select-delete
1304 ms → 4 ms. Collapsed-caret deletes still native.
habarmc1223-sudo pushed a commit to habarmc1223-sudo/hermes-agent-fluxmem that referenced this pull request Jul 8, 2026
…ct-delete (NousResearch#45812)

Large paste and Ctrl+A → Delete froze the composer for seconds — both routed
through Chromium's contenteditable editing pipeline (~O(n²) on multiline DOM).

- insertPlainTextAtCaret: Range + text/<br> fragment (paste path)
- deleteSelectionInEditor: range.deleteContents for non-collapsed Backspace/Delete
- Shared composerSelectionRange helper; both flush via flushEditorToDraft

Profiled live (47 KB / 122 paragraphs): paste 4474 ms → 13 ms; select-delete
1304 ms → 4 ms. Collapsed-caret deletes still native.
santhreal pushed a commit to santhreal/hermes-agent that referenced this pull request Jul 13, 2026
…ct-delete (NousResearch#45812)

Large paste and Ctrl+A → Delete froze the composer for seconds — both routed
through Chromium's contenteditable editing pipeline (~O(n²) on multiline DOM).

- insertPlainTextAtCaret: Range + text/<br> fragment (paste path)
- deleteSelectionInEditor: range.deleteContents for non-collapsed Backspace/Delete
- Shared composerSelectionRange helper; both flush via flushEditorToDraft

Profiled live (47 KB / 122 paragraphs): paste 4474 ms → 13 ms; select-delete
1304 ms → 4 ms. Collapsed-caret deletes still native.
DavidMetcalfe pushed a commit to DavidMetcalfe/hermes-agent that referenced this pull request Jul 15, 2026
After typing into the composer and pasting (Cmd+V), pressing Cmd+Z
unwound the typed characters instead of the paste — the paste had
no entry in the contentEditable undo stack.

Root cause: `handlePaste` (`app/chat/composer/index.tsx:625`) called
`event.preventDefault()` then mutated the DOM via `insertPlainTextAtCaret`
(`app/chat/composer/rich-editor.ts:150`), which uses `range.insertNode`
directly. Chromium's contentEditable undo manager only records changes
that flow through Blink's editing pipeline (`Editor::InsertText` et al.) —
i.e. native keystrokes, native paste, and `document.execCommand('insertText')`.
Direct DOM mutation bypasses that pipeline entirely, so no undo entry
was pushed for the paste and Cmd+Z stepped back through the keystrokes
typed before it.

Why the custom path existed: PR NousResearch#45812 replaced `execCommand('insertText')`
because the editing pipeline is ~O(n²) on multiline blobs (profiled
4474 ms on a 47 KB / 122-paragraph paste vs 13 ms through the bypass).
We keep that win for pathological pastes and only restore the slow
path for typical sizes.

Fix: new `pastePlainTextIntoEditor` helper in `rich-editor.ts` branches
on `text.length <= LARGE_PASTE_THRESHOLD_CHARS` (4096) and on the editor
being focused. Small focused pastes go through `execCommand('insertText')`,
which restores the undo entry. Everything else falls back to
`insertPlainTextAtCaret` exactly as before.

Tests: three new cases in `rich-editor.test.ts` covering the focused /
unfocused / oversized branches by stubbing `document.execCommand` (jsdom
doesn't define it natively).

TypeScript + ESLint clean. All 32 composer-area tests pass; full
vitest run shows no new failures (3 added passing tests; the 6
pre-existing failures are unrelated to this change).
DavidMetcalfe added a commit to DavidMetcalfe/hermes-agent that referenced this pull request Jul 15, 2026
Route small pastes through document.execCommand('insertText') so
Chromium records an undo entry — Cmd+Z then reverts the paste instead
of skipping over it. Large pastes (>4096 chars) fall back to the
direct DOM-mutation path to avoid the O(n²) freeze (PR NousResearch#45812).

Also fixes two edge cases caught by cross-vendor review:
- Focus check relaxed to editor.contains(document.activeElement) so
  pastes work when a chip/descendant has focus.
- execCommand failure handled: fall back to insertPlainTextAtCaret
  on false return or exception.

Tests: pastePlainTextIntoEditor (5 tests: focused, unfocused, large,
descendant focus, execCommand failure fallback).
Gravezzz pushed a commit to Gravezzz/hermes-agent that referenced this pull request Jul 21, 2026
…ct-delete (NousResearch#45812)

Large paste and Ctrl+A → Delete froze the composer for seconds — both routed
through Chromium's contenteditable editing pipeline (~O(n²) on multiline DOM).

- insertPlainTextAtCaret: Range + text/<br> fragment (paste path)
- deleteSelectionInEditor: range.deleteContents for non-collapsed Backspace/Delete
- Shared composerSelectionRange helper; both flush via flushEditorToDraft

Profiled live (47 KB / 122 paragraphs): paste 4474 ms → 13 ms; select-delete
1304 ms → 4 ms. Collapsed-caret deletes still native.
OutThisLife added a commit that referenced this pull request Jul 26, 2026
The rich editor mutates its DOM through `Range` rather than the browser's
editing commands, because `execCommand('insertText')` is ~O(n²) on large
multiline blobs and froze the composer for seconds on a big paste (#45812).
Those mutations never reach Chromium's undo stack, so a paste was invisible to
it — Cmd+Z skipped straight past the pasted text and undid whatever edit came
before it, leaving the paste stranded and the earlier edit destroyed.

Owning the stack outright is the only coherent fix; a half-owned one interleaves
our snapshots with Chromium's own typing entries and undoes them out of order.
Every edit path now banks its pre-edit state, and the editor claims Cmd+Z /
Cmd+Shift+Z (plus Ctrl+Y) instead of letting the native command run. Snapshots
are plain text + a caret offset rather than DOM, since the editor already
round-trips losslessly through composerPlainText/renderComposerContents.

Consecutive keystrokes coalesce inside a 600ms window, so undo steps back by a
typing burst the way a native editor does rather than one character at a time.
Electron's Edit menu `{ role: 'undo' }` fires the native command without a
keystroke the renderer can see, so a capture-phase `beforeinput` listener claims
historyUndo/historyRedo too and keeps the menu item and the shortcut in
agreement. Switching drafts resets the history — undoing into another
conversation's text is worse than having none.

Co-authored-by: David Metcalfe <80915+DavidMetcalfe@users.noreply.github.com>
leewenjie pushed a commit to leewenjie/hermes-agent that referenced this pull request Aug 7, 2026
…ct-delete (NousResearch#45812)

Large paste and Ctrl+A → Delete froze the composer for seconds — both routed
through Chromium's contenteditable editing pipeline (~O(n²) on multiline DOM).

- insertPlainTextAtCaret: Range + text/<br> fragment (paste path)
- deleteSelectionInEditor: range.deleteContents for non-collapsed Backspace/Delete
- Shared composerSelectionRange helper; both flush via flushEditorToDraft

Profiled live (47 KB / 122 paragraphs): paste 4474 ms → 13 ms; select-delete
1304 ms → 4 ms. Collapsed-caret deletes still native.
randlee pushed a commit to randlee/hermes-agent that referenced this pull request Aug 11, 2026
The rich editor mutates its DOM through `Range` rather than the browser's
editing commands, because `execCommand('insertText')` is ~O(n²) on large
multiline blobs and froze the composer for seconds on a big paste (NousResearch#45812).
Those mutations never reach Chromium's undo stack, so a paste was invisible to
it — Cmd+Z skipped straight past the pasted text and undid whatever edit came
before it, leaving the paste stranded and the earlier edit destroyed.

Owning the stack outright is the only coherent fix; a half-owned one interleaves
our snapshots with Chromium's own typing entries and undoes them out of order.
Every edit path now banks its pre-edit state, and the editor claims Cmd+Z /
Cmd+Shift+Z (plus Ctrl+Y) instead of letting the native command run. Snapshots
are plain text + a caret offset rather than DOM, since the editor already
round-trips losslessly through composerPlainText/renderComposerContents.

Consecutive keystrokes coalesce inside a 600ms window, so undo steps back by a
typing burst the way a native editor does rather than one character at a time.
Electron's Edit menu `{ role: 'undo' }` fires the native command without a
keystroke the renderer can see, so a capture-phase `beforeinput` listener claims
historyUndo/historyRedo too and keeps the menu item and the shortcut in
agreement. Switching drafts resets the history — undoing into another
conversation's text is worse than having none.

Co-authored-by: David Metcalfe <80915+DavidMetcalfe@users.noreply.github.com>
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.

1 participant