fix(cli): wrap terminal capability queries in hidden sequence (#19080)

Co-authored-by: Srinath Padmanabhan <srithreepo@google.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
Srinath Padmanabhan
2026-02-14 20:08:13 -08:00
committed by GitHub
parent bcd547baf6
commit 78130d4bb7
2 changed files with 25 additions and 2 deletions

View File

@@ -11,6 +11,7 @@ import {
enableKittyKeyboardProtocol,
enableModifyOtherKeys,
} from '@google/gemini-cli-core';
import * as fs from 'node:fs';
// Mock fs
vi.mock('node:fs', () => ({
@@ -289,5 +290,16 @@ describe('TerminalCapabilityManager', () => {
expect(manager.isKittyProtocolEnabled()).toBe(false);
expect(enableModifyOtherKeys).not.toHaveBeenCalled();
});
it('should wrap queries in hidden/clear sequence', async () => {
const manager = TerminalCapabilityManager.getInstance();
void manager.detectCapabilities();
expect(fs.writeSync).toHaveBeenCalledWith(
expect.anything(),
// eslint-disable-next-line no-control-regex
expect.stringMatching(/^\x1b\[8m.*\x1b\[2K\r\x1b\[0m$/s),
);
});
});
});

View File

@@ -43,6 +43,9 @@ export class TerminalCapabilityManager {
private static readonly TERMINAL_NAME_QUERY = '\x1b[>q';
private static readonly DEVICE_ATTRIBUTES_QUERY = '\x1b[c';
private static readonly MODIFY_OTHER_KEYS_QUERY = '\x1b[>4;?m';
private static readonly HIDDEN_MODE = '\x1b[8m';
private static readonly CLEAR_LINE_AND_RETURN = '\x1b[2K\r';
private static readonly RESET_ATTRIBUTES = '\x1b[0m';
/**
* Triggers a terminal background color query.
@@ -219,11 +222,19 @@ export class TerminalCapabilityManager {
try {
fs.writeSync(
process.stdout.fd,
TerminalCapabilityManager.KITTY_QUERY +
// Use hidden mode to prevent potential "m" character from being printed
// to the terminal during startup when querying for modifyOtherKeys.
// This can happen on some terminals that might echo the query or
// malform the response. We hide the output, send queries, then
// immediately clear the line and reset attributes.
TerminalCapabilityManager.HIDDEN_MODE +
TerminalCapabilityManager.KITTY_QUERY +
TerminalCapabilityManager.OSC_11_QUERY +
TerminalCapabilityManager.TERMINAL_NAME_QUERY +
TerminalCapabilityManager.MODIFY_OTHER_KEYS_QUERY +
TerminalCapabilityManager.DEVICE_ATTRIBUTES_QUERY,
TerminalCapabilityManager.DEVICE_ATTRIBUTES_QUERY +
TerminalCapabilityManager.CLEAR_LINE_AND_RETURN +
TerminalCapabilityManager.RESET_ATTRIBUTES,
);
} catch (e) {
debugLogger.warn('Failed to write terminal capability queries:', e);