fix(desktop): recover from a stale IME composition flag that wedged the composer

A missed compositionend (focus jump, input-source switch, programmatic DOM
swap mid-preedit) left composingRef stuck true, and the stuck flag silently
swallowed every Enter in handleEditorKeyDown and every Send-button submit via
the form onSubmit guard — no error, no RPC, until the composer remounted. For
CJK IME users (where even ASCII typing runs through composition) this read as
"Enter has no effect; messages cannot be sent", degrading composer instance
by composer instance.

Recover in two places, both grounded in invariants Chromium guarantees:

- keydown: every keydown during a genuine composition carries
  isComposing=true, so when the native flag says we're not composing, clear
  the stale ref before the guard reads it.
- blur: a composition never survives focus loss, so clear the flag
  unconditionally — this is what unblocks the Send button path, which has no
  native composition flag to consult.

The genuine-IME protection (#37483 class) is untouched: Enter with
isComposing=true is still swallowed.

Fixes #44135
This commit is contained in:
AIalliAI
2026-06-11 08:50:21 +00:00
committed by Teknium
parent 202e5b813a
commit 39e7607794
2 changed files with 150 additions and 1 deletions
@@ -0,0 +1,129 @@
import { act, cleanup, fireEvent, render } from '@testing-library/react'
import { useRef } from 'react'
import { afterEach, describe, expect, it, vi } from 'vitest'
afterEach(cleanup)
// Faithful mirror of index.tsx's IME wiring: the composition guard at the top
// of handleEditorKeyDown (self-heal + swallow), the compositionstart/end
// handlers, and the blur reset.
//
// Regression repro for #44135: compositionend can be missed (focus jumps,
// input-source switches, programmatic DOM swaps mid-preedit), leaving
// composingRef wedged true. Before the fix, a wedged flag silently swallowed
// every Enter — and, via the form onSubmit guard, the Send button — until the
// composer remounted, which read as "Enter has no effect, no error, nothing
// reaches the gateway". The fix trusts Chromium's per-keydown isComposing flag
// to clear a stale ref, and clears it on blur (a composition never survives
// focus loss).
function Harness({ onSubmit, wedgeComposing }: { onSubmit: (text: string) => void; wedgeComposing?: boolean }) {
const editorRef = useRef<HTMLDivElement>(null)
const composingRef = useRef(Boolean(wedgeComposing))
const submitDraft = () => {
onSubmit(editorRef.current?.textContent ?? '')
}
const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
if (composingRef.current && !event.nativeEvent.isComposing) {
composingRef.current = false
}
if (composingRef.current || event.nativeEvent.isComposing) {
return
}
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault()
submitDraft()
}
}
return (
<div>
<div
contentEditable
data-testid="editor"
onBlur={() => {
composingRef.current = false
}}
onCompositionEnd={() => {
composingRef.current = false
}}
onCompositionStart={() => {
composingRef.current = true
}}
onKeyDown={handleKeyDown}
ref={editorRef}
suppressContentEditableWarning
/>
<button
data-testid="send"
onClick={() => {
// Mirrors the form onSubmit guard in index.tsx.
if (composingRef.current) {
return
}
submitDraft()
}}
type="button"
/>
</div>
)
}
describe('composer Enter — stale IME composition flag recovery (#44135)', () => {
it('sends on Enter despite a wedged composing flag when the native event says not composing', async () => {
const onSubmit = vi.fn()
const { getByTestId } = render(<Harness onSubmit={onSubmit} wedgeComposing />)
const editor = getByTestId('editor')
await act(async () => {
editor.textContent = 'hello after wedge'
fireEvent.keyDown(editor, { key: 'Enter', isComposing: false })
})
expect(onSubmit).toHaveBeenCalledWith('hello after wedge')
})
it('still swallows Enter during a genuine composition (isComposing keydown)', async () => {
const onSubmit = vi.fn()
const { getByTestId } = render(<Harness onSubmit={onSubmit} />)
const editor = getByTestId('editor')
await act(async () => {
fireEvent.compositionStart(editor)
editor.textContent = '你好'
// The Enter that confirms the preedit: Chromium stamps isComposing=true.
fireEvent.keyDown(editor, { key: 'Enter', isComposing: true })
})
expect(onSubmit).not.toHaveBeenCalled()
// After compositionend, the next Enter sends normally.
await act(async () => {
fireEvent.compositionEnd(editor)
fireEvent.keyDown(editor, { key: 'Enter', isComposing: false })
})
expect(onSubmit).toHaveBeenCalledWith('你好')
})
it('unblocks the Send button after blur even when compositionend was missed', async () => {
const onSubmit = vi.fn()
const { getByTestId } = render(<Harness onSubmit={onSubmit} />)
const editor = getByTestId('editor')
await act(async () => {
fireEvent.compositionStart(editor)
editor.textContent = '发送'
// compositionend never fires (the wedge) — the user mouses to Send,
// blurring the editor.
fireEvent.blur(editor)
fireEvent.click(getByTestId('send'))
})
expect(onSubmit).toHaveBeenCalledWith('发送')
})
})
+21 -1
View File
@@ -551,6 +551,18 @@ export function ChatBar({
}
const handleEditorKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
// Self-heal a stale composition flag before the guard below reads it.
// compositionend can be missed (focus jumps, input-source switches, or a
// programmatic DOM swap mid-preedit abort the composition without the
// event reaching us), and a wedged composingRef silently swallows every
// Enter — and, via the form onSubmit guard, the Send button — until the
// component remounts (#44135). Chromium stamps isComposing on every
// keydown of a genuine composition, so when the native flag says we're
// not composing, trust it and recover.
if (composingRef.current && !event.nativeEvent.isComposing) {
composingRef.current = false
}
// IME composition: Enter confirms composed text, not a message submission.
// We check both composingRef (set by compositionstart/compositionend, robust
// across browsers) and nativeEvent.isComposing (Chromium fallback). Without
@@ -1012,7 +1024,15 @@ export function ChatBar({
data-placeholder={placeholder}
data-slot={RICH_INPUT_SLOT}
onBeforeInput={handleEditorBeforeInput}
onBlur={() => window.setTimeout(closeTrigger, 80)}
onBlur={() => {
// A composition never survives focus loss (Chromium commits the
// preedit and fires compositionend on blur) — but if that event is
// missed, the wedged flag would block the Send button's form-submit
// guard forever (#44135). Clear unconditionally: by the time blur
// runs there is nothing left composing in this editor.
composingRef.current = false
window.setTimeout(closeTrigger, 80)
}}
onCompositionEnd={event => {
composingRef.current = false