codex icon indicating copy to clipboard operation
codex copied to clipboard

Pasting is really slow on the CLI Windows

Open anlagbr opened this issue 8 months ago • 8 comments

What version of the Codex App are you using (From “About Codex” dialog)?

codex-cli 0.112.0

What subscription do you have?

Pro 200$

What platform is your computer?

Microsoft Windows NT 10.0.26100.0 x64

What issue are you seeing?

Pasting is really slow on the CLI

What steps can reproduce the bug?

Paste large text in the CLI

What is the expected behavior?

Should be fast

Additional information

No response

anlagbr avatar Mar 09 '26 17:03 anlagbr

Potential duplicates detected. Please review them and close your issue if it is a duplicate.

  • #13729

Powered by Codex Action

github-actions[bot] avatar Mar 09 '26 17:03 github-actions[bot]

Still reproducible on 0.151.0 with a 17,496-character paste

I observed the Windows CLI paste-latency problem on current stable codex-cli 0.151.0.

Environment

  • Windows 11 Pro for Workstations Insider Preview, x64, build 26220.9223
  • Windows Terminal 1.24.11911.0
  • PowerShell 7.6.5
  • Direct Windows Terminal session; WT_SESSION present, no multiplexer detected
  • disable_paste_burst is not set in the Codex config

Observed behavior

  1. Paste a long, mixed Chinese/English Markdown instruction into the active CLI composer.
  2. The final placeholder reports exactly [Pasted Content 17496 chars].
  3. Before reaching that final state, the TUI visibly stalls. Raw/partially handled text is visible first, and only after the delay does it collapse into the blue Pasted Content element.
  4. The paste eventually completes and the placeholder count is correct.

The final collapsed placeholder itself appears intentional for content over the large-paste threshold. The bug being reported here is the noticeable UI stall and late transition from visible text to the placeholder, not the existence of the placeholder.

This was one directly observed reproduction; the wall-clock duration was not timed, so I am not claiming a precise latency or deterministic threshold. No model response is involved in reproducing the composer-side delay.

Source-level observation (not a confirmed cause)

In 0.151.0, Windows terminals can use the non-bracketed paste-burst fallback, which receives characters as key events, buffers detected bursts, and flushes them through the large-paste path after the Windows-specific idle timeout. The relevant paste-burst and large-placeholder code is unchanged on current public main.

A per-character event/render backlog or delayed burst classification could plausibly explain the stall and late collapse, but I have not profiled or confirmed that mechanism.

A screenshot of the final [Pasted Content 17496 chars] state is available.

Water-Run avatar Aug 31 '26 04:08 Water-Run

Synthetic profiling narrows the latency to Windows input delivery

I ran two temporary, source-level probes with the same payload size as the reproduction (17,496 mixed Chinese/English characters). These numbers are from one unoptimized test build on the reporter's Windows machine, so they are directional rather than a stable benchmark:

  • driving ChatComposer::handle_key_event once per character: 25.1 ms
  • driving the full App::handle_tui_event path once per character: 130.9 ms

Both probes exercised the non-bracketed paste-burst state machine and preserved multiline input. The temporary profiling tests were removed afterward; no production change was made.

This does not reproduce the noticeable end-to-end pause by itself. Importantly, these probes exclude Windows console input acquisition and crossterm's event-source thread. That makes the placeholder insertion and composer state machine unlikely to be the dominant cost in this reproduction.

The remaining source boundary is notable:

  1. Codex explicitly clears ENABLE_VIRTUAL_TERMINAL_INPUT and uses Windows console input-record mode: https://github.com/openai/codex/blob/a9519cbcdd2d/codex-rs/tui/src/tui/windows_console.rs#L1-L65
  2. The pinned crossterm fork's Windows event source checks the queue and then calls read_single_input_event() for one record before returning one event: https://github.com/openai-oss-forks/crossterm/blob/45fecb9508105988f42fe6ff0441783ed3717f92/src/event/source/windows.rs#L43-L86

A large Windows paste can therefore require thousands of console-record reads and event handoffs before Codex receives the final character and the 60 ms Windows burst-idle timer can collapse the buffered payload into [Pasted Content N chars].

That is a source-supported hypothesis, not yet an end-to-end confirmed root cause. The next useful measurement is timing/count instrumentation around WindowsEventSource::try_read (queue depth, records drained, and time until the last paste character), ideally comparing the current single-record path with a batched ReadConsoleInputW path while preserving key, resize, focus, surrogate-pair, Enter, and IME semantics.

Based on these measurements, changing the large-paste threshold or merely shortening the idle timeout would not address the likely dominant cost and could regress paste/IME correctness. I did not prepare that speculative patch.

Water-Run avatar Aug 31 '26 07:08 Water-Run

Candidate fix: batch the Windows console input queue

I was able to reproduce the multi-second cost at the exact source boundary identified above and prepared a tested dependency PR:

https://github.com/openai-oss-forks/crossterm/pull/4

The change replaces one-record-at-a-time ReadConsoleInputW calls with a single read of all currently available console records into a local FIFO. Crossterm still parses and emits one public event at a time, preserving ordering, surrogate-pair state, mouse-button state, resize/focus handling, and timeout behavior.

Isolated A/B measurement

A disposable child process allocated its own hidden Win32 console, enqueued exactly 17,496 synthetic KEY_EVENT_RECORDs, used one warm-up, and measured five release-build rounds with fully separate Cargo target directories:

Path Pinned baseline median Batched median
synchronous event::read 3,476,437 us 1,835 us
asynchronous EventStream used by Codex 3,139,261 us 4,040 us

A direct batch read of all 17,496 records took about 0.55-0.62 ms.

This confirms that the single-record crossterm boundary is independently sufficient to create a multi-second backlog of the observed size. It is still an isolated Win32 queue benchmark, not an end-to-end Windows Terminal clipboard measurement, so terminal-side record production and Codex rendering may contribute additional latency.

Validation

  • crossterm cargo test --all-features: 86 passed, 10 ignored; 53 doctests passed
  • crossterm clippy across all targets/features: passed with existing warnings only
  • Codex current main (d58d0e5841e0) compiled against the patched local dependency
  • 92 Codex TUI tests matching paste passed
  • one unrelated goal-draft test stack-overflowed on Windows and reproduced identically against the unmodified pinned crossterm baseline

This means changing Codex's paste threshold or idle timer is unnecessary for this bottleneck; the final [Pasted Content N chars] behavior can remain unchanged while the underlying input records are delivered much faster.

Water-Run avatar Aug 31 '26 11:08 Water-Run

Adding a bounded Windows transport reproduction focused on paste framing under pauses, complementing the earlier batching analysis and crossterm PR #4. Batching addresses the reported input-read bottleneck; the observations below concern preserving paste boundaries when delays do occur. This is not a claim that a complete CLI fix has passed integration testing.

Environment: native Windows x64, OS build 10.0.26200; installed Codex CLI 0.153.4. Source checked at rust-v0.153.4 (3d2ee51ca2d5db578f328aa75e20aa22c0197c9a) and a subsequent main snapshot (3d3df0a0cad5d3d8d3340b633787e9dd304ea463). The original user-visible symptom occurred in an Orca embedded terminal; the transport experiments below used a separate ConPTY process, without Orca.

Why increasing the paste timeout is insufficient

Codex enables bracketed paste output mode, but explicitly clears ENABLE_VIRTUAL_TERMINAL_INPUT on Windows and reasserts input-record mode while polling. The pinned crossterm Windows backend reads individual console input records. The composer then uses timing-based PasteBurst classification, including a 60 ms active-buffer idle timeout on Windows.

An unchanged PasteBurst module can emit multiple paste chunks when UI ticks flush it between bursts separated by 61 ms. This shows that chunk boundaries can depend on scheduling; it does not establish the precise timing of the original incident.

Controlled ConPTY observations

A small native probe set raw console input and compared VT input disabled/enabled. Sending ESC[200~, alpha\n, beta, and ESC[201~ in separate writes with 300 ms delays produced:

Mode Character data observed
Input-record mode, VT input disabled alpha\nbeta (paste delimiters absent)
VT input enabled ESC[200~alpha\nbetaESC[201~

A second, experimental crossterm adapter decoded the VT character records. The source payload was "prefix\n" + "한글 😀\r\n" * 350 + "suffix" (2,113 Unicode scalar values), sent in three chunks with an additional two-second pause before the closing paste delimiter.

For the experimental VT decoder, this one case produced exactly one Paste event and zero Enter key events, and the emitted payload exactly matched the source (including CRLF and non-BMP Unicode). No paste was emitted before the closing delimiter, and a following Up-arrow sequence was decoded as Up.

The input-record baseline log contained individual key events, but that capture was not demonstrated to have drained the complete input. Its event count is therefore omitted: this is not a complete A/B event-count or throughput comparison, and it does not establish dropped input. These are event-source observations, not counts of CLI submissions or a complete keyboard/IME/resize compatibility test.

Proposed direction and limitations

Please investigate preserving terminal-issued paste framing through the Windows backend and emitting one Event::Paste only after the closing delimiter, rather than treating idle time as the boundary. Input mode and decoder changes need to ship together; simply enabling VT input against the currently pinned backend is insufficient. Terminals that do not send paste boundaries still need a clearly separate fallback.

The experimental decoder is not ready to submit: independent review found that its leading-ESC timeout could split an opener if the consumer was descheduled before draining queued records. A robust implementation must cover that case too, along with incomplete/unknown sequences, UTF-16 surrogate handling, native key compatibility, and terminal handoff. Increasing the existing timeout would only change the delay at which failures occur.

The full CLI build was stopped due to local resource pressure. No full CLI runtime, user acceptance test, or complete performance fix is claimed. Related multiline-input symptom: #31416.

MeroZemory avatar Sep 09 '26 06:09 MeroZemory

Adding a tested follow-up to the existing input-queue and paste-framing investigations in this issue. This is shared here in accordance with the repository's issue-based contribution policy; it is not an upstream Codex PR or a claim that the fix has been accepted.

Code and reproduction

  • Codex integration: https://github.com/barto-sh/codex/commit/3c66589bc87b4793333eb7a1b1e28b70792b2aff
  • Reproduction and validation: https://github.com/barto-sh/codex/tree/fix/windows-long-paste/scripts/windows-paste-probe
  • Public pinned backend: https://github.com/barto-sh/crossterm/commit/c37391e44b41fab9b2178a174e93539e699ea4ff

The batching work in https://github.com/openai-oss-forks/crossterm/pull/4 and the earlier framing analysis in this thread are relevant prior work. This follow-up additionally preserves terminal-issued paste boundaries through the native Windows record backend, rather than relying on the paste-burst idle timer to recover them.

Mechanism

Codex currently clears ENABLE_VIRTUAL_TERMINAL_INPUT. Controlled native ConPTY traces show that this removes the bracketed paste delimiters before the editor can consume them. The integration keeps VT input enabled and restores its original state at terminal handoff. The mode change and pinned decoder must be applied together. The paired backend batches ready records, decodes VT characters with the existing ANSI parser, and emits a single Event::Paste after the closing delimiter. Native key-up, mouse, focus and resize handling remain available. UTF-16 Press/Release assembly and committed Alt-code characters have separate regression coverage, including non-BMP text.

The standalone-Escape ambiguity is resolved only after draining both buffered records and the ready OS input queue. In particular, descheduling the consumer must not expire an opener that is already queued. A test mutation that restores eager expiry returned Esc instead of Paste and failed; the final implementation passes that regression. Fragmented delimiters, a delayed close and leading/trailing empty lines are also exercised in the full composer.

Full native Windows CLI measurements

Windows-11-10.0.26100-SP0. The production CLI was built in release mode (thin LTO), separately from the unoptimized TUI test binary. This is installed release codex-cli 0.163.0 versus patched main codex-cli 0.0.0 based on e4568a3538c9cab6627d64ff8cdfc7d98b79b7db, not a same-base full-CLI A/B. The harness uses native OS ConPTY, the source-matched Windows Terminal 1.24.12741.0 clipboard-to-wire normalization and a headless xterm observer. It measures a rendered follow-up editing marker, then independently exports the complete prompt through Ctrl+G and checks SHA-256. Export time is excluded from marker latency.

Binary / screen Characters / shape Median marker latency Range Exact
baseline (inline) 65,536 / single line 18.647 s 18.358-19.167 s 3/3
baseline (inline) 65,536 / multiline 18.370 s 17.649-18.729 s 3/3
candidate (inline) 65,536 / single line 0.187 s 0.185-0.188 s 3/3
candidate (inline) 65,536 / multiline 0.186 s 0.185-0.186 s 3/3
candidate (inline) 262,144 / single line 0.343 s 0.341-0.358 s 3/3
candidate (inline) 262,144 / multiline 0.339 s 0.337-0.342 s 3/3
default-screen 65,536 / multiline 0.187 s 0.186-0.202 s 3/3
default-screen 262,144 / multiline 0.341 s 0.341-0.353 s 3/3

The before/after input and wire hashes match. Any baseline content mismatch is retained in the raw results. The final candidate requires exact full text throughout. Three fresh-process first-composer trials have median 0.145 s, all exact; they do not warm the composer with Escape or Ctrl+G before input.

Validation and limits

  • 155 selected TUI tests passed across paste, event-stream and input-mode groups. Test-only optimization overrides are separate from the measured production CLI.
  • 19 functional cases passed, including Unicode/emoji, CRLF/LF, tabs/empty lines, cursor-middle insertion, unframed fallback, repeated large pastes in one draft, resize, subsequent editing, cancellation, literal command-like input and explicit Enter. No prompt is submitted automatically; only the last synthetic prompt is explicitly sent to an unauthenticated loopback receiver.
  • Native WinAPI mode restoration passed for both original VT states, including reassertion after another console client clears VT.
  • Library CI: Windows 142 passed / 10 ignored, Ubuntu 154 passed / 7 ignored, 53 doctests each; formatting and Clippy -D warnings passed on both platforms. https://github.com/barto-sh/crossterm/actions/runs/39281925280 (the CI-only commit has identical library sources to the pinned revision).
  • The generated lockfile also refreshes existing Windows dependency references without adding registry package versions. Public reproduction uses a disposable profile/workspace and publicly resolvable pinned dependency, not a local Cargo path. It needs no model login and does not read or overwrite the clipboard.
  • Physical Ctrl+V in Windows Terminal, physical IME/layouts, image clipboard and legacy-conhost end-to-end behavior have not been observed. No independent Claude/pi benchmark is claimed.

The linked branch includes the harness and raw synthetic results so the behavior and scope can be reproduced and reviewed without private session data.

Fresh-public-clone check: branch commit 07229411c32b78b4799ece54eeb76f7ed874ed5b was cloned from GitHub and built with --locked, using the shared compiler cache after checking all 9,321 tracked files against the tested tree. The resulting binary SHA-256 remained ac72fa3413ff9efde8e7ea0311fad7339c9e1ba1d0866def71181f85792a2e9b. A fresh npm ci of the published harness followed by six more default-screen trials preserved the complete text in all six cases.

barto-sh avatar Oct 19 '26 17:10 barto-sh

still same issue on v0.165.0

NeoStarkX avatar Oct 29 '26 10:10 NeoStarkX

(response from codex)

Reproduced on Codex CLI 0.172.0 with Windows 11 and Windows Terminal 1.18. The earlier typing-delay fix didn’t resolve this. crossterm PR #4 roughly halved the delay, but large pastes still took several seconds.

Workaround

For a local Windows session, remove Ctrl+V from Windows Terminal’s Paste action so Codex receives the shortcut and reads the clipboard directly. You can leave Ctrl+Shift+V bound to terminal Paste for other applications.

On the official 0.172.0 release, all six trials with Ctrl+V passed through to Codex preserved the full text and accepted the next keystroke in under 200 ms.

Where the time goes

#18914 reduced the wait for a single typed character from 30 ms to 8 ms. Once Codex detects a paste, it still waits 60 ms after the last character before flushing the buffer.

In one 8,000-character paste on the unmodified CLI, nearly all 16,000 key records were queued within 62 ms. The queue was still draining at 10.54 seconds, and the placeholder appeared at 10.66 seconds. Shortening the final timeout would save very little.

Full CLI comparison

I applied PR #4’s one-file batching change to the crossterm revision pinned by 0.172.0. All builds used the same compiler settings and paste timers. With Windows Terminal handling Ctrl+V, these were the median times until a follow-up keystroke appeared, over three trials per case on one VM.

Build 8,000 ASCII characters 17,496 Chinese/English characters, multiline
Baseline 14.5 s 32.7 s
PR #4 7.1 s 16.0 s
PR #4 with per-poll console-mode checks disabled 3.1 s 6.3 s

Multiline correctness matters here. Codex submitted the first line before I pressed Enter in 2 of 3 baseline trials and 3 of 3 PR trials. Local history contained the first line, and the exported draft matched the remainder. All ASCII drafts matched. The sample is too small to conclude that batching made premature submission more likely.

Notes for future investigation

I’d leave the paste timers alone and investigate preserving the terminal’s bracketed-paste markers through the Windows input backend. That would let Codex receive one paste event and treat embedded newlines as text. The prototype described above is a useful starting point.

Disabling the console-mode checks was diagnostic only. They protect keyboard handling when another process changes the input mode. Any fix should preserve that protection and retain a fallback for terminals that don’t send paste markers.

etraut-openai avatar Nov 24 '26 03:11 etraut-openai