Automatic scrolling of window when setting cursor position
Windows Terminal version
1.16.230126001
Windows build number
10.0.19044.2486
Other Software
No response
Steps to reproduce
I mentioned this first in #14759.
You can reproduce the problem with this test program:
#include <windows.h>
int main()
{
HANDLE con = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(con, &csbi);
// use scrollback of 100 lines
COORD con_size = {
csbi.srWindow.Right - csbi.srWindow.Left + 1,
csbi.srWindow.Bottom - csbi.srWindow.Top + 1 + 100
};
SetConsoleScreenBufferSize(con, con_size);
// write 50 lines so the position is not at the top of the scrollback
for (int i = 0; i < 50; i++)
WriteConsoleA(con, "line\n", 5, NULL, NULL);
// stop at escape key
HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD ir;
DWORD didread;
while (ReadConsoleInput(in, &ir, 1, &didread))
{
if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
{
if (ir.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
break;
WriteConsoleA(con, &ir.Event.KeyEvent.uChar.AsciiChar, 1, NULL, NULL);
GetConsoleScreenBufferInfo(con, &csbi);
// re-set the cursor position where it already is, this is done
// so the cursor is visible at the new location after the character
// was printed
SetConsoleCursorPosition(con, csbi.dwCursorPosition);
// now the console window is scrolled so that the cursor is at the bottom
// of the window.
}
}
return 0;
}
There is a difference of behavior when tried with either the conhost.exe of the current Win10, or the OpenConsole.exe of Microsoft.WindowsTerminal_Win10_1.16.10261.0_8wekyb3d8bbwe.msixbundle.
Between entering some keys, scroll the window up or down with the mouse.
Expected Behavior
Once you enter a key, and the cursor position is re-set, it will scroll the window to the cursor, but only if the cursor was not already in the visible part of the window. Which is exactly how it works in conhost.exe.
Actual Behavior
But with OpenConsole.exe 1.16.230126001, the window is always scrolled so that the cursor is at the bottom of the screen, even if it was already visible before.
Thanks for the repro!
This change in behavior is almost certainly a result of PR #12972, and I suspect it could be difficult to resolve without reintroducing the virtual bottom bugs that PR was attempting to fix.
Thanks for putting this issue down. I have the same issue and it really sucks, as there's no other API that can perform the functions with GetConsoleScreenBufferInfo() in the meantime. CONSOLE_SCREEN_BUFFER_INFO::dwCursorPosition.Y is also affected by this, and it maxes out at the vertical console window size when you hit the bottom of the console window to the point where the scrollbar shows up.
Any updates on the bug? I was able to create a custom detection algorithm for the bug, maybe that helps:
// A function to check for a specific bug affecting CONSOLE_SCREEN_BUFFER_INFO::dwCursorPosition
// positioning reports, in the new Windows Terminal and OpenConsole.exe.
bool ConsoleWTBugCheck() {
// Get console size
HANDLE hTest = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbiTest;
GetConsoleScreenBufferInfo(hTest, &csbiTest);
int nConsoleHeight = csbiTest.srWindow.Bottom - csbiTest.srWindow.Top;
// Spam enters until a few more than console bottom
for (int i = 0; i <= (nConsoleHeight + 4); i++) {
std::cout << '\n';
}
// Get the console cursor position after that
GetConsoleScreenBufferInfo(hTest, &csbiTest);
int nVerticalCPosition = csbiTest.dwCursorPosition.Y;
cls();
if (nVerticalCPosition <= nConsoleHeight) {
return true;
}
else {
return false;
}
}
This code gets the height of the terminal and spams newlines after at least 1 above the terminal height (i used 4 for some reason). The number of newlines entered is recorded. It then checks the new reported height by the GetConsoleScreenBufferInfo() function. If that function reports anything smaller than the number of newlines entered, then the bug is there, else it isn't. A cls() function is there to clear the screen after the newlines so the user doesn't see them.
It is required that the test is done when the cursor position is at the highest row (0).
This bug is the root cause of scroll-jump issues in all terminal applications that use the Ink rendering framework (React-based terminal UI for Node.js), which calls SetConsoleCursorPosition on every render cycle.
The most prominent affected application is Anthropic's Claude Code CLI, which has a large and growing Windows user base. Claude Code streams long-form output (code generation, file diffs, build results) while users need to scroll up to read prior context. Every render cycle triggers the viewport-snap-to-bottom described here, making it impossible to read scrollback during active output.
The practical impact across the Claude Code community:
- 6+ duplicate issues filed on the Claude Code repo (#33367 with 61 upvotes, #33814 with 30, #10835 with 15, #34242 with 9, plus #34400 and #34052)
- Combined 100+ upvotes and 100+ comments across those issues
- Multiple community-built workarounds (stdout interceptors, PTY proxies, tmux wrappers,
snapOnOutput: false) — all attempting to work around this specificSetConsoleCursorPositionbehavior - One user (@doitallwrong on the Claude Code repo) decompiled the CLI, identified three specific functions emitting unclamped cursor-up sequences, and provided a
sedpatch — but the underlying Windows Terminal behavior described here is the aggravating factor that makes the cursor repositioning scroll the viewport even inside synchronized update blocks
This affects any Ink-based application on Windows Terminal, not just Claude Code. Given that Ink is the standard framework for Node.js terminal UIs, the blast radius extends to the entire ecosystem of interactive CLI tools built with it.
I would like to work on this.
I traced the current behavior to ApiRoutines::SetConsoleCursorPositionImpl in src/host/getset.cpp. After #12972, the function determines the snap position relative to GetVirtualViewport(), moves the real viewport, and calls UpdateBottom(), even when the cursor is already visible in the real viewport.
My proposed boundary is:
- If the rendered cursor cell is already inside
GetViewport(), leave both the real viewport and virtual bottom unchanged. - If the cursor is outside the real viewport, retain the existing virtual-viewport snapping and
UpdateBottom()behavior. - Add a regression case to
ScreenBufferTests::UpdateVirtualBottomWithSetConsoleCursorPositionwhere the real and virtual viewports differ but the real viewport already contains the cursor.
I would also manually validate the reproducer here, #1222, #9754, and cls behavior so this does not undo the cases protected by #12972.
@zadjii-msft, does that behavioral boundary look correct before I open a draft PR?
- If the rendered cursor cell is already inside
GetViewport(), leave both the real viewport and virtual bottom unchanged.
This doesn't sound right to me. The virtual bottom needs to be updated whenever the cursor is moved outside the virtual viewport boundaries. This can occur regardless of whether the cursor is inside the visible viewport or not.
It's also worth noting that this issue is specific to ConHost/OpenConsole as far as I'm aware. So if you're trying to address an issue in Windows Terminal, like the complaint from the Claude Code user above, I suspect that's unrelated to this code.
It's also worth noting that this issue is specific to ConHost/OpenConsole as far as I'm aware. So if you're trying to address an issue in Windows Terminal, like the complaint from the Claude Code user above, I suspect that's unrelated to this code.
Yes. The real problem here is not that the Windows Terminal is doing it wrong, but it's that ConHost (conio.h) has been doing it wrong for so long. GetConsoleScreenBufferInfo() traditionally returns the position of the cursor relative to the entire scrollbuffer. The current implementation in OpenConsole will result in the returned CONSOLE_SCREEN_BUFFER_INFO object to contain the cursor co-ordinates for the visible buffer/current viewport area only. This is correct, but if we were to get Microsoft to fix Get/SetConsoleCursorPosition() to properly address this, it would break other API functions (specifically functions in conio.h) and legacy applications which still use the logic of the old method.
Hence, I think this should be fixed as a profile-specific setting implementation. A simple toggle should do the trick. Enable the old method of cursor position logic (default, off), enable the new method (on). When off, the cursor position would be updated based on the position on the entire scrollbuffer. When on, the cursor position would be updated based on the viewport and restricted to that area, just like it is now.
I believe this could work because an installation of Claude could just create a new profile on Windows Terminal sticking to the old method of console cursor position data. Then, the devs for those API tools can adapt their logic to the old method.
All this is provided Microsoft isn't willing to fix their cursor functions AND conhost.
After #12972, it will return the position on the visible buffer/viewport only.
I believe that to be a misapprehension of what that pull request does.
After #12972, it will return the position on the visible buffer/viewport only.
I believe that to be a misapprehension of what that pull request does.
You are correct. It's been a while since i dug into this so I forgot all my PR numbers, LOL. Just axed it instead, edited.
Almost all AI agent CLIs have this issue: while working, the user is reading the output, and then the entire screen gets offset and scrolls to the top.
Almost all AI agent CLIs have this issue: while working, the user is reading the output, and then the entire screen gets offset and scrolls to the top.
Agentic CLIs by and large use the alternate screen buffer and implement their own scrolling. There is nothing we can do about that.