The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui): keys after a CSI-u Enter in the same read edit the next draft
Tekniumf511fbaba39b
diff --git a/ui-tui/src/__tests__/textInputSubmitClear.test.tsx b/ui-tui/src/__tests__/textInputSubmitClear.test.tsxindex 81b0fde1c34..8d0d6bd9ca8 100644--- a/ui-tui/src/__tests__/textInputSubmitClear.test.tsx+++ b/ui-tui/src/__tests__/textInputSubmitClear.test.tsx@@ -3,7 +3,7 @@ import { PassThrough } from 'node:stream'import { renderSync } from '@hermes/ink'import React, { useState } from 'react'-import { describe, expect, it, vi } from 'vitest'+import { afterEach, describe, expect, it, vi } from 'vitest'import { TextInput } from '../components/textInput.js'@@ -153,4 +153,108 @@ describe('TextInput submit clearing', () => {expect(changes.at(-1)).toBe('x')expect(changes).not.toContain('hellox')})++ describe('keys after a CSI-u Enter in the same read', () => {+ afterEach(() => {+ vi.unstubAllEnvs()+ vi.restoreAllMocks()+ ;(process.stdout as { isTTY?: boolean }).isTTY = undefined+ })++ // One read carries text, a kitty Enter and the first key of the next draft, so all three+ // land in one React batch before the parent's submit response renders.+ async function burst(clearOnSubmit: boolean, fastEcho: boolean, read = 'hello\x1b[13ux') {+ const echoed: string[] = []++ if (fastEcho) {+ // useStdout() resolves to process.stdout, so the fast-echo bypass is armed there.+ vi.stubEnv('TERM_PROGRAM', 'iTerm.app')+ vi.stubEnv('TMUX', '')+ ;(process.stdout as { isTTY?: boolean }).isTTY = true+ vi.spyOn(process.stdout, 'write').mockImplementation(chunk => {+ echoed.push(String(chunk))++ return true+ })+ }++ const streams = makeStreams()+ const changes: string[] = []+ const submits: string[] = []+ let current = ''++ function Harness() {+ const [value, setValue] = useState('')+ current = value++ return (+ <TextInput+ columns={80}+ onChange={next => {+ changes.push(next)+ setValue(next)+ }}+ onSubmit={text => {+ submits.push(text)++ if (clearOnSubmit) {+ setValue('')+ }+ }}+ value={value}+ />+ )+ }++ const instance = renderSync(React.createElement(Harness), {+ patchConsole: false,+ stderr: streams.stderr as NodeJS.WriteStream,+ stdin: streams.stdin as unknown as NodeJS.ReadStream,+ stdout: streams.stdout as NodeJS.WriteStream+ })++ await settle()+ streams.stdin.send(read)+ await settle(50)+ streams.stdin.send('y')+ await settle(50)++ instance.unmount()+ instance.cleanup()++ return { changes, current, echoed: echoed.join(''), submits }+ }++ for (const fastEcho of [true, false]) {+ const mode = fastEcho ? 'fast echo' : 'Ink render'++ it(`starts the cleared draft with the key typed after Enter (${mode})`, async () => {+ const { changes, current, echoed, submits } = await burst(true, fastEcho)++ expect(submits).toEqual(['hello'])+ expect(current).toBe('xy')+ expect(changes.at(-1)).toBe('xy')+ expect(changes).not.toContain('hellox')++ if (fastEcho) {+ // The bypass is really armed: the next draft's later keystroke is written directly.+ expect(echoed).toContain('y')+ }+ })++ it(`keeps the draft and the key typed after Enter when the parent does not clear (${mode})`, async () => {+ const { current, submits } = await burst(false, fastEcho)++ expect(submits).toEqual(['hello'])+ expect(current).toBe('helloxy')+ })+ }++ it('submits each line and keeps the tail when one read holds two CSI-u Enters', async () => {+ const { current, submits } = await burst(true, true, 'a\x1b[13ub\x1b[13uc')++ expect(submits).toEqual(['a', 'b'])+ expect(current).toBe('cy')+ })+ })})diff --git a/ui-tui/src/components/textInput.tsx b/ui-tui/src/components/textInput.tsxindex f650e926fba..fbe55554b8c 100644--- a/ui-tui/src/components/textInput.tsx+++ b/ui-tui/src/components/textInput.tsx@@ -768,6 +768,8 @@ function useFwdDelete(active: boolean) {return ref}+type InputHandler = (inp: string, k: Key, event: InputEvent) => void+type PasteResult = { cursor: number; value: string } | nullconst isPasteResultPromise = (@@ -801,6 +803,11 @@ export function TextInput({// the text and the submit's clear in the same React batch, so `value` never visibly changes and// the submitted text would stay in the composer.const [submitSeq, setSubmitSeq] = useState(0)+ // Keys that arrive after a submit, before the parent's response has rendered (a kitty Enter+ // and the next keystroke share one read). They replay once the value-sync effect has applied+ // the parent's answer, so they edit the next draft instead of the submitted one.+ const postSubmitKeys = useRef<null | Parameters<InputHandler>[]>(null)+ const handleInputRef = useRef<InputHandler>(() => {})const fwdDel = useFwdDelete(focus)const termFocus = useTerminalFocus()const { stdout } = useStdout()@@ -965,6 +972,13 @@ export function TextInput({redo.current = []}, [value, submitSeq])+ // Declared after the value sync so it runs once the parent's answer to a submit is applied.+ useEffect(() => {+ const queued = postSubmitKeys.current+ postSubmitKeys.current = null+ queued?.forEach(([inp, k, event]) => handleInputRef.current(inp, k, event))+ }, [submitSeq])+// The composer unmounts while full-screen monitors own input. Keep its// insertion point with the shell, not with transient steer/secret inputs.useEffect(@@ -1383,8 +1397,20 @@ export function TextInput({}}+ const submit = (text: string) => {+ postSubmitKeys.current = []+ setSubmitSeq(n => n + 1)+ cbSubmit.current?.(text)+ }++ // Holds keys while a submit is pending. The replay goes through this gate too, so a replayed+ // Enter queues the keys after it until its own submit has been answered.+ const queueAfterSubmit = (handler: InputHandler): InputHandler =>+ (handleInputRef.current = (inp, k, event) =>+ postSubmitKeys.current ? void postSubmitKeys.current.push([inp, k, event]) : handler(inp, k, event))+useInput(- (inp: string, k: Key, event: InputEvent) => {+ queueAfterSubmit((inp: string, k: Key, event: InputEvent) => {const eventRaw = event.keypress.raw// Configured voice shortcut wins over composer-level defaults like@@ -1460,8 +1486,7 @@ export function TextInput({if (insertNewline) {commit(ins(pending.value, pending.cursor, '\n'), pending.cursor + 1)} else {- setSubmitSeq(n => n + 1)- cbSubmit.current?.(pending.value)+ submit(pending.value)}return@@ -1726,7 +1751,7 @@ export function TextInput({}commit(v, c)- },+ }),{ isActive: focus })