fix(terminal): deliver PTY input in order - #1509
Open
rwanglq-ctrl wants to merge 1 commit into
Open
rwanglq-ctrl wants to merge 1 commit into
rwanglq-ctrl wants to merge 1 commit into
Conversation
Each keystroke was sent as its own invoke("pty_write") without waiting for
the previous one, and pty_write runs on the async runtime and the blocking
pool. Concurrent writes could land in the PTY in any order, so fast typing
reached the shell scrambled.
Keep one pty_write in flight per session and batch input that arrives while it
completes. Keystrokes, IME commits and paste stay ordered; a failed write is
not retried (it may have written a prefix); closing the session drops pending
input.
Closes xiaolai#1507
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fast typing in the integrated terminal reached the shell out of order (#1507).
VMarkPty.write()sent each keystroke as its owninvoke("pty_write")without waiting for the previous one, andpty_writeis anasynccommand that writes throughspawn_blocking, so concurrent writes could land in the PTY in any order.This keeps one
pty_writein flight per session and batches input that arrives meanwhile.Policy Gates (Required)
Linked Issue
Type of Change
What Changed
src/lib/ptyInputQueue.ts: per-session ordered delivery — one write in flight, later input joined into the next batch. A failed write is reported and not retried (it may already have written a prefix).close()drops pending input without waiting for the current write.src/lib/pty.ts:write()goes through the queue;_cleanup()closes it, which replaces the per-write destroy guard.src/lib/pty.input.test.ts(5 tests): a rapid mixed-Unicode burst + Enter stays ordered with one write in flight; bracketed paste and control bytes keep their order with following input; empty writes are ignored; killing the session during a write drops queued input; a failed partial write is not replayed and later input is still accepted. The first three fail againstmain'spty.ts; the last two pin behaviourmainalready had.scripts/feature-map.jsonand.claude/feature-ledger.md: register the new module underpty-backend.Validation
pnpm check:alllocally.Ran locally:
pnpm vitest runonsrc/lib/pty*.test.ts,src/lib/__tests__/pty.test.ts,src/components/Terminal/spawnPty.test.ts(72 passed),pnpm typecheck, eslint on the changed files,lint:feature-map,lint:file-headers,lint:provenance-ids. Not run: the fullcheck:all. Manual check on Linux ARM64 (Arch / Hyprland, WebKitGTK 4.1 2.52.6): bursts typed withwtypearrive at a raw-mode recorder in order, ~40 ms after the key.UI Evidence (if applicable)
Before (from #1507): the typed string arrives scrambled, e.g.
>/tmp/vmark-input-qa/received.txtbecame>/tm/pvmark-input-qa/receivedt.xt. After: received bytes match what was typed.PR Checklist
中文说明
问题: 在内置终端快速输入时,字符会乱序到达 shell(#1507)。前端每个按键单独调用一次
pty_write,不等上一次完成;后端pty_write是异步命令,并在阻塞线程池里写入 PTY,并发写入的先后顺序无法保证。修复: 每个终端会话同一时间只保留一个在途写入请求,期间到达的输入合并成一批,按顺序发送。按键、输入法上屏和粘贴都保持顺序;写入失败不重试(可能已写入一部分);关闭会话时丢弃未发送的输入。
验证: 新增 5 个测试,其中顺序、粘贴、空写入 3 个在
main上失败、在本 PR 上通过,另外 2 个固定已有行为;相关终端测试 72 个全部通过。在 Linux ARM64(Arch + Hyprland)上实测,快速输入按顺序到达,约 40 ms。🤖 Generated with Claude Code