From 83ffeb9f0a510e7fa8712b0aeea0c506860e846a Mon Sep 17 00:00:00 2001 From: Janghoon Lee <44862514+savagemanage@users.noreply.github.com> Date: Wed, 28 Oct 2026 09:45:26 +0000 Subject: [PATCH] fix(tui): stop mouse codes leaking into Git Bash after exit In Git Bash (mintty, no pseudo console) Ctrl+C ends a native program at once, so no exit handler runs and the shell is left with mouse reporting on. Every mouse move then prints "35;x;yM". - Mouse reporting is off by default in Git Bash (REDROB_FORCE_MOUSE=1 keeps it). - The npm launcher resets terminal modes when the TUI ends abnormally on Windows. --- packages/core/src/flag/flag.ts | 2 ++ packages/redrob/bin/redrob | 17 +++++++++++++++++ packages/tui/src/app.tsx | 5 +++-- packages/tui/src/terminal-win32.ts | 10 ++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/core/src/flag/flag.ts b/packages/core/src/flag/flag.ts index 46032cd75a..8cb5071491 100644 --- a/packages/core/src/flag/flag.ts +++ b/packages/core/src/flag/flag.ts @@ -43,6 +43,8 @@ export const Flag = { /** Never read or write the saved console /models listing (tests, which mock the fetch). */ REDROB_DISABLE_MODELS_CACHE: truthy("REDROB_DISABLE_MODELS_CACHE"), REDROB_DISABLE_MOUSE: truthy("REDROB_DISABLE_MOUSE"), + /** Keep mouse reporting on in Git Bash (mintty), where it is off by default. */ + REDROB_FORCE_MOUSE: truthy("REDROB_FORCE_MOUSE"), REDROB_FAKE_VCS: process.env["REDROB_FAKE_VCS"], REDROB_SERVER_PASSWORD: process.env["REDROB_SERVER_PASSWORD"], REDROB_SERVER_USERNAME: process.env["REDROB_SERVER_USERNAME"], diff --git a/packages/redrob/bin/redrob b/packages/redrob/bin/redrob index 18e4789758..3227763bf8 100755 --- a/packages/redrob/bin/redrob +++ b/packages/redrob/bin/redrob @@ -7,6 +7,22 @@ const os = require("os") const forwardedSignals = ["SIGINT", "SIGTERM", "SIGHUP"] +// Mouse reporting, bracketed paste, kitty keys, alternate screen, hidden cursor. +const TERMINAL_RESET = "\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?1015l\x1b[?2004l\x1b[ { + restoreTerminal(code, signal) for (const forwardedSignal of forwardedSignals) { process.removeListener(forwardedSignal, forwarders[forwardedSignal]) } diff --git a/packages/tui/src/app.tsx b/packages/tui/src/app.tsx index dca6f02e4a..f7085cf907 100644 --- a/packages/tui/src/app.tsx +++ b/packages/tui/src/app.tsx @@ -86,7 +86,7 @@ import type { EventSource } from "./context/sdk" import { DialogVariant } from "./component/dialog-variant" import { createTuiAttention } from "./attention" import * as TuiAudio from "./audio" -import { win32DisableProcessedInput, win32FlushInputBuffer } from "./terminal-win32" +import { win32DisableProcessedInput, win32FlushInputBuffer, win32GitBash } from "./terminal-win32" import { destroyRenderer } from "./util/renderer" import { cliErrorMessage, errorFormat } from "./util/error" @@ -203,7 +203,8 @@ export const run = Effect.fn("Tui.run")(function* (input: TuiInput) { useKittyKeyboard: {}, autoFocus: false, openConsoleOnError: false, - useMouse: !Flag.REDROB_DISABLE_MOUSE && input.config.mouse, + useMouse: + !Flag.REDROB_DISABLE_MOUSE && input.config.mouse && (Flag.REDROB_FORCE_MOUSE || !win32GitBash()), consoleOptions: { keyBindings: [{ name: "y", ctrl: true, action: "copy-selection" }], }, diff --git a/packages/tui/src/terminal-win32.ts b/packages/tui/src/terminal-win32.ts index 1aaa80aecd..deba3666ad 100644 --- a/packages/tui/src/terminal-win32.ts +++ b/packages/tui/src/terminal-win32.ts @@ -128,3 +128,13 @@ export function win32InstallCtrlCGuard() { return unhook } + +/** + * Git Bash (mintty) without a pseudo console. Ctrl+C there ends a native program + * at once, so no exit handler runs and mouse reporting stays on in the shell. + */ +export function win32GitBash() { + if (process.platform !== "win32") return false + if (!process.env["MSYSTEM"]) return false + return process.env["TERM_PROGRAM"] === "mintty" || !process.stdin.isTTY +}