Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(chat): drop a deferred composer write once the next composition begins

M3gA-Mindd191ceed3ac6
diff --git a/app/src/components/assistant-ui/thread.tsx b/app/src/components/assistant-ui/thread.tsx
index 0f55c3ffeb..3611f37a68 100644
--- a/app/src/components/assistant-ui/thread.tsx
+++ b/app/src/components/assistant-ui/thread.tsx
@@ -350,12 +350,30 @@ const Composer: FC<{
};
}, []);
- // DOM text -> composer store. Deferred by a microtask so the editor has
- // finished applying the event before the DOM is read.
+ // Set for as long as an IME composition is open. The gate is a ref rather
+ // than state because it is read from a microtask, not from a render.
+ //
+ // Adopted from #5764 (@ligjn), which identified the hazard this closes: the
+ // store write below is deferred, and a fast CJK typist can open the next
+ // composition before it runs. That stale write would rebuild the editor
+ // mid-composition and cancel it -- #5763 again, one composition later.
+ const isComposingTextRef = useRef(false);
+
+ // DOM text -> composer store. The text is read at event time; only the write
+ // is deferred by a microtask, so the editor has finished applying the event
+ // before the store changes under it.
+ //
+ // The gate is re-checked INSIDE the microtask, not just at event time: a
+ // composition that started in between makes this write stale, and dropping it
+ // loses nothing, because the DOM is the source of truth and that
+ // composition's own commit reads the whole of it.
const syncComposerFromDom = (target: EventTarget | null) => {
if (!(target instanceof HTMLElement)) return;
const text = target.textContent ?? '';
- globalThis.queueMicrotask(() => aui.composer.setText(text));
+ globalThis.queueMicrotask(() => {
+ if (isComposingTextRef.current) return;
+ aui.composer.setText(text);
+ });
};
return (
@@ -380,6 +398,9 @@ const Composer: FC<{
<LexicalComposerInput
ref={inputWrapperRef}
placeholder="Send a message..."
+ onCompositionStartCapture={() => {
+ isComposingTextRef.current = true;
+ }}
onInputCapture={event => {
// An IME fires `input` per keystroke while the candidate window is
// still open, and the text on the DOM then is the pre-edit, not the
@@ -387,16 +408,25 @@ const Composer: FC<{
// cancels the composition, so `nihao` + Enter committed as
// `n ni nihao 你好` (#5763). The keydown guard below already refuses
// to act mid-composition; this bridge was the one that did not.
+ //
+ // Two checks, because they catch different things: the ref covers
+ // the whole composition from `compositionstart`, and the native flag
+ // covers an `input` that arrives without one.
+ if (isComposingTextRef.current) return;
if ('isComposing' in event.nativeEvent && event.nativeEvent.isComposing) {
return;
}
syncComposerFromDom(event.target);
}}
onCompositionEndCapture={event => {
+ // Re-open the gate before syncing: what the DOM holds now is what the
+ // user committed, and it is the store's turn to catch up.
+ //
// Chromium emits a trailing `input` with `isComposing === false` that
// the handler above picks up; WebKit does not, so on Safari the
// committed text exists only here. Running in both is harmless -- the
// second write carries the same string.
+ isComposingTextRef.current = false;
syncComposerFromDom(event.target);
}}
onKeyDownCapture={event => {
diff --git a/app/src/pages/__tests__/Conversations.render.test.tsx b/app/src/pages/__tests__/Conversations.render.test.tsx
index 91189a06c0..79db6e27bc 100644
--- a/app/src/pages/__tests__/Conversations.render.test.tsx
+++ b/app/src/pages/__tests__/Conversations.render.test.tsx
@@ -1030,6 +1030,70 @@ describe('Conversations — smoke render (#1123 welcome-lock removal)', () => {
);
});
+ // The store write is deferred by a microtask, so a fast typist can open the
+ // NEXT composition before it runs. #5764 (@ligjn) named that hazard: the stale
+ // write rebuilds the editor mid-composition and cancels it, which is #5763 one
+ // composition later. The gate is therefore re-checked inside the microtask.
+ it('drops a deferred store write once the next composition has begun', async () => {
+ const { textarea, thread } = await renderSelectedConversation();
+
+ await act(async () => {
+ fireEvent.compositionStart(textarea);
+ typeImePreEdits(textarea, ['n', 'ni', 'nihao']);
+ commitIme(textarea, '你好');
+ // Still inside the same task, so the write queued by `commitIme` has not
+ // run yet — and the user has already started composing the next word.
+ fireEvent.compositionStart(textarea);
+ });
+
+ // The stale write was dropped: nothing reached the store while a
+ // composition is open.
+ expect(screen.queryByRole('button', { name: 'Send message' })).toBeNull();
+
+ // Nothing was lost either — the next commit reads the whole DOM.
+ await act(async () => {
+ typeImePreEdits(textarea, ['你好sh', '你好shi']);
+ commitIme(textarea, '你好世界');
+ });
+ await waitFor(() => {
+ expect(screen.getByRole('button', { name: 'Send message' })).not.toBeDisabled();
+ });
+ await act(async () => {
+ fireEvent.click(screen.getByRole('button', { name: 'Send message' }));
+ });
+ await waitFor(() => {
+ expect(chatSend).toHaveBeenCalledTimes(1);
+ });
+ expect(chatSend).toHaveBeenCalledWith(
+ expect.objectContaining({ threadId: thread.id, message: '你好世界' })
+ );
+ });
+
+ // A cancelled composition (Escape, or clicking away) still fires
+ // `compositionend`, but the finalized DOM is legitimately empty. The store
+ // must end up empty too rather than holding the last pre-edit.
+ it('does not resurrect the pre-edit when a composition is cancelled', async () => {
+ const { textarea } = await renderSelectedConversation();
+
+ await act(async () => {
+ fireEvent.compositionStart(textarea);
+ typeImePreEdits(textarea, ['n', 'ni', 'nihao']);
+ commitIme(textarea, '');
+ });
+
+ await waitFor(() => {
+ expect(screen.queryByRole('button', { name: 'Send message' })).toBeNull();
+ });
+
+ // The gate reopened, so ordinary typing after the cancellation still syncs.
+ await act(async () => {
+ setComposerText(textarea, 'after cancel');
+ });
+ await waitFor(() => {
+ expect(screen.getByRole('button', { name: 'Send message' })).not.toBeDisabled();
+ });
+ });
+
// The gate keys on `isComposing`, so ordinary typing must be untouched. This is
// the property the 22 existing composer tests depend on: in jsdom the bridge is
// the only path from a synthetic `input` to the store.