-
Notifications
You must be signed in to change notification settings - Fork 6.3k
refactor: simplify PasteBurst flush API and disable color requery #5683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
refactor: simplify PasteBurst flush API and disable color requery #5683
Conversation
|
Okay, I’ll add some conditional flag instead of commenting it out. I’ll set it to false for now, since I’m not sure whether you’ll want to completely remove this function or use it in some other way. |
- Replace FlushResult enum with Option<String> in paste_burst.rs - Update chat_composer to use Option<String> instead of FlushResult - Increase PASTE_BURST_CHAR_INTERVAL from 8ms to 16ms for better reliability - Disable terminal color requery on focus to prevent OSC responses bleeding into input stream - Add improved heuristics for paste detection (retro_chars >= 2)
0ad7ae6 to
d967912
Compare
How the flag and optimization workRuntime code: What the Rust compiler does:
// tui.rs calls: // Compiler replaces it with an empty function → the call disappears Final effect
💡 SummaryThis is better than commenting code – the compiler guarantees that it won’t run, Ah, now I get why I sent just the paste burst earlier :) — I thought you’d be handling the PR with fixes and adjusting the whole structure yourselves. I figured my part was just to get the credit and that it would later be merged into the main branch automatically, so I didn’t want to interfere or add anything on my own :) I was thinking I’d get some initial feedback on how to handle the commented-out line — I didn’t want to add any logic on my own to avoid introducing confusion. I’m hoping it finally goes through and you can be done with it once and for all :) |
|
Ah, the tests passed successfully earlier, but I updated the branch to check another issue — it needs to be run again now. |
|
Going to cc @joshka-oai here since he is coordinating a refactor of related logic here |
|
That's very good — so things don't get messy in the long run :) Though here it’s mainly about blocking this function. The paste burst itself behaved similarly in both cases — with bracketed paste disabled, there wasn’t any significant difference. Only the API. Practically speaking: without bracketed paste mode, reliable cross-platform handling of large paste operations is extremely difficult — especially when dealing with both Windows and Unix together. Reason: without bracketed paste, it’s nearly impossible to reliably distinguish a paste burst from normal user input across different systems (e.g., Unix uses In the mode where you have DisableBracketedPaste enabled and you’re reading raw bytes (which is required to make it work reliably and prevent the buffer from hanging), you take full responsibility for everything the terminal/OS normally handles: decoding, escape sequence parsing, event mapping, and signal processing. I’m writing this as a note for the future, in case anyone else decides to wrestle with this problem. |
|
There's an alternate fix for the color queries that addresses this at the crossterm level over in #5935. The goal there is to move this down a level with some other similar things. This might be complementary to that (as changing the timing of when these queries happen is a good idea), so it's likely not wasted effort. I probably won't get to looking at this PR until later next week (or the week after if other priorities take my time), but it seems at a high level to possibly be complementary. |
|
Ok, no need to approve it — that’s not the point. Maybe you have a different idea on how to approach this. I just wanted to show where the cause might be, so you can verify that it works. I also wanted to report as many observations as possible that I noticed during this process. I hope I didn’t cause more confusion than help :) I tried to do this without touching the code — just to point out the possible cause, so you could decide how to proceed yourselves :) |
Summary
This PR simplifies the paste burst detection API and fixes an issue with OSC color query responses.
Changes
Simplified PasteBurst API
FlushResultenum withOption<String>for cleaner APIchat_composer.rsto handle the simplified return typeImproved paste detection reliability
PASTE_BURST_CHAR_INTERVALfrom 8ms to 16msretro_chars >= 2as paste-likerecommended_flush_delay()with guard band for timer jitterFixed color query issue
requery_default_colors()on terminal focus gainbefore
Here you can see how it sends right after pasting.

after

Problem with EAGAIN and reading from stdin:
What is EAGAIN?
How it was supposed to work for paste burst:
Why it's tricky on Windows:
Windows doesn’t have EAGAIN
ERROR_IO_PENDING,ERROR_NO_DATAinstead ofEAGAIN.Different console API
read()on file descriptor0(stdin)ReadConsoleInput()orReadFile()— totally different mechanismsNon-blocking mode is handled differently
fcntl(fd, F_SETFL, O_NONBLOCK)SetNamedPipeHandleState()or overlapped async operationsCrossterm already handles this
crosstermcrate provides cross-platform event handlingcrossterm::event::EventStreamfor unified async inputWhy you can't drain stdin on Windows:
Console input is an event queue, not a byte stream
Unix: stdin is a file descriptor—you read bytes until EAGAIN
Windows: ReadConsoleInput() returns events (key, mouse, resize) from a queue
No simple "read everything available"
No non-blocking read for console
Unix: O_NONBLOCK + read() = returns EAGAIN immediately
Windows: ReadConsoleInput() blocks or returns 0 events (unclear if empty or waiting)
PeekConsoleInput() only peeks, doesn't consume
Raw stdin ≠ console on Windows
ReadFile(GetStdHandle(STD_INPUT_HANDLE)) works for pipes/files
For console you need ReadConsoleW()—different function!
Non-blocking ReadFile() requires overlapped I/O (async callbacks)
Timing problem
Can't tell if event queue is empty because NO input OR data still incoming
No equivalent of "EAGAIN = end of currently available data"
Conclusion: Windows requires async/event-based approach (IOCP), not sync drain loop. That's why they use crossterm.
Therefore, paste bursts on Windows won’t work when there’s no support for bracketed paste — and the same applies to some other systems as well.
That means you could try using async fd, but it's unclear how it would work.
However, bracketed paste is supported by most modern terminals, so this isn’t a significant issue.
If anyone’s interested, I can share an example snippet that handles EAGAIN on an async file descriptor.
In summary, at the current stage everything works correctly — no duplicate input, no unexpected sends, fully stable behavior.
The root cause of the issue was the
requery_default_colors()call.@dylan-hurd-oai
I’ve compiled and run it — everything works as expected.
I’m leaving the notes above in case someone finds them useful in the future.
Paste burst works analogously to before, and regarding EAGAIN I'm speaking generally—the problem existed earlier. At the moment as I was writing it doesn't pose a problem because bracketed paste works virtually everywhere. But educationally speaking, the system depending on load, without bracketed paste, in addition to sending byte-by-byte, could also send packets in 1, 2, 4, 8 KB sizes, etc. Event stream ini poll can't be drained that way to pure bytes.
So currently paste burst with bracketed paste disabled won't work correctly on any system, not just Windows, precisely because of the 1, 2, 4, 8 KB packet sizing. For short pastes it will work relatively fine, but for long ones it will depend on how much you paste at once—the buffer might catch the whole 8k characters at once and it will work, or it might break it into 1k chunks, and then only subsequent clicks will fill in the next packets. So if the terminal doesn't support bracketed paste, the only advice for now is don't paste long text :)
Using async fd will solve the problem on Unix systems, but how it will work on Windows is a mystery.
So now it will work on Windows and any other system, as long as the terminal supports bracketed paste. If it doesn’t, then on any system it might work sometimes and sometimes not.
Url