Skip to content

fix(terminal): deliver PTY input in order - #1509

Open
rwanglq-ctrl wants to merge 1 commit into
xiaolai:mainfrom
rwanglq-ctrl:fix/pty-input-order
Open

rwanglq-ctrl wants to merge 1 commit into
xiaolai:mainfrom
rwanglq-ctrl:fix/pty-input-order

Conversation

@rwanglq-ctrl

@rwanglq-ctrl rwanglq-ctrl commented Oct 5, 2026 •

Copy link
Copy Markdown

Summary

Fast typing in the integrated terminal reached the shell out of order (#1507). VMarkPty.write() sent each keystroke as its own invoke("pty_write") without waiting for the previous one, and pty_write is an async command that writes through spawn_blocking, so concurrent writes could land in the PTY in any order.

This keeps one pty_write in flight per session and batches input that arrives meanwhile.

Policy Gates (Required)

  • This PR is single-focus (one issue, one problem, one objective).
  • This PR includes 100% test coverage for changed behavior and changed code paths.
  • If this is a bug fix, the linked issue contains detailed reproduction context.

Linked Issue

Type of Change

  • Bug fix
  • Feature
  • Docs
  • Refactor
  • Test-only
  • Other

What Changed

  • New 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 against main's pty.ts; the last two pin behaviour main already had.
  • scripts/feature-map.json and .claude/feature-ledger.md: register the new module under pty-backend.

Validation

  • I ran pnpm check:all locally.
  • I added or updated tests to fully cover behavior changes.
  • I manually verified critical flows.

Ran locally: pnpm vitest run on src/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 full check:all. Manual check on Linux ARM64 (Arch / Hyprland, WebKitGTK 4.1 2.52.6): bursts typed with wtype arrive 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.txt became >/tm/pvmark-input-qa/receivedt.xt. After: received bytes match what was typed.

PR Checklist

  • The PR avoids unrelated refactors or cleanup.
  • The issue context is clear (linked issue or explanation above).
  • Docs/changelog were updated if behavior or usage changed. (Bug fix only; no user-facing behavior or docs change.)
  • I am ready to address review feedback.

中文说明

问题: 在内置终端快速输入时,字符会乱序到达 shell(#1507)。前端每个按键单独调用一次 pty_write,不等上一次完成;后端 pty_write 是异步命令,并在阻塞线程池里写入 PTY,并发写入的先后顺序无法保证。

修复: 每个终端会话同一时间只保留一个在途写入请求,期间到达的输入合并成一批,按顺序发送。按键、输入法上屏和粘贴都保持顺序;写入失败不重试(可能已写入一部分);关闭会话时丢弃未发送的输入。

验证: 新增 5 个测试,其中顺序、粘贴、空写入 3 个在 main 上失败、在本 PR 上通过,另外 2 个固定已有行为;相关终端测试 72 个全部通过。在 Linux ARM64(Arch + Hyprland)上实测,快速输入按顺序到达,约 40 ms。

🤖 Generated with Claude Code

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Terminal: fast keyboard input reaches the shell out of order (Linux)

1 participant