From 81805a97ef977b4e1918c0d9945cf720df9952d9 Mon Sep 17 00:00:00 2001 From: teknium1 <127238744+teknium1@users.noreply.github.com> Date: Tue, 15 Sep 2026 11:51:02 -0700 Subject: [PATCH] fix(tui): drop a pending key-burst flush when an external value replaces the draft The composer hands key bursts to the parent on a 16ms timer. When history navigation, a slash completion or a submit clears/replaces the draft while such a flush is still armed, the [value] effect reset the local buffer correctly but left the timer running, so 16ms later the stale burst was handed to the parent and overwrote the external value (second hole in #111934). Disarm the pending flush in the external branch of the [value] effect: once the parent has replaced the draft, a burst typed against the old draft can never be the newer value. Second invariant test covers it alongside the stale own-echo case. --- .../textInputStaleParentEcho.test.tsx | 61 +++++++++++++++++++ ui-tui/src/components/textInput.tsx | 9 +++ 2 files changed, 70 insertions(+) diff --git a/ui-tui/src/__tests__/textInputStaleParentEcho.test.tsx b/ui-tui/src/__tests__/textInputStaleParentEcho.test.tsx index 6b39d88cda..e811f2329d 100644 --- a/ui-tui/src/__tests__/textInputStaleParentEcho.test.tsx +++ b/ui-tui/src/__tests__/textInputStaleParentEcho.test.tsx @@ -53,13 +53,20 @@ const tick = () => new Promise(resolve => setImmediate(resolve)) function Harness({ onValue, + setValueRef, snapshotRef }: { onValue: (value: string) => void + setValueRef?: React.MutableRefObject<((next: string) => void) | null> snapshotRef: React.RefObject }) { const [value, setValue] = useState('') + if (setValueRef) { + // Stands in for history navigation / a slash completion replacing the draft. + setValueRef.current = setValue + } + return React.createElement(TextInput, { cursorSnapshotRef: snapshotRef, onChange: (next: string) => { @@ -140,6 +147,60 @@ describe('stale parent own-echo during deferred key-burst flush', () => { expect(values.at(-1)).toBe('abcde') expect(snapshotRef.current).toEqual({ cursor: 5, value: 'abcde' }) }) + + it('drops a pending key-burst flush when an external value replaces the draft', async () => { + vi.stubEnv('TERM_PROGRAM', 'iTerm.app') + vi.stubEnv('TMUX', '') + + const stdout = new FakeTty() + const stdin = new FakeTty() + const stderr = new FakeTty() + const values: string[] = [] + const snapshotRef = useRefBridge() + const setValueRef: React.MutableRefObject<((next: string) => void) | null> = { current: null } + + const instance = renderSync( + React.createElement(Harness, { onValue: v => values.push(v), setValueRef, snapshotRef }), + { + patchConsole: false, + stderr: stderr as unknown as NodeJS.WriteStream, + stdin: stdin as unknown as NodeJS.ReadStream, + stdout: stdout as unknown as NodeJS.WriteStream + } + ) + + try { + await tick() + + for (const ch of ['a', 'b', 'c']) { + stdin.send(ch) + await tick() + } + + vi.advanceTimersByTime(16) + await tick() + await tick() + + // "d" arms a new deferred flush; history navigation then replaces the + // draft before that flush fires. + stdin.send('d') + await tick() + setValueRef.current?.('history draft') + await tick() + await tick() + + // The stale burst must not be handed to the parent 16ms later. + vi.advanceTimersByTime(16) + await tick() + await tick() + } finally { + instance.unmount() + instance.cleanup() + } + + expect(values).toEqual(['a', 'abc']) + expect(snapshotRef.current).toEqual({ cursor: 'history draft'.length, value: 'history draft' }) + }) }) // The snapshot ref is read after unmount in the assertion above; the helper diff --git a/ui-tui/src/components/textInput.tsx b/ui-tui/src/components/textInput.tsx index dcc49e366a..aa9d7860d2 100644 --- a/ui-tui/src/components/textInput.tsx +++ b/ui-tui/src/components/textInput.tsx @@ -940,6 +940,15 @@ export function TextInput({ return } + // An external value replaced the draft. A key burst still waiting on its + // 16ms flush is now stale; letting it fire would hand the parent the old + // draft on top of the value it just set. + if (parentChangeTimer.current) { + clearTimeout(parentChangeTimer.current) + parentChangeTimer.current = null + } + + pendingParentValue.current = null setCur(value.length) setSel(null) curRef.current = value.length