hermes-agent/tests/hermes_cli/test_modify_other_keys_aliases.py
ethernet 890bbbda1f Merge remote-tracking branch 'origin/main' into ethie/pm-clean
# Conflicts:
#	apps/desktop/e2e/archived-hidden-session-recoverable.spec.ts
#	apps/desktop/e2e/bot-chat-message-agent-friendly-name.spec.ts
#	apps/desktop/e2e/bot-mailbox-unreadable-ticket.spec.ts
#	apps/desktop/e2e/bot-mode-roster-localized.spec.ts
#	apps/desktop/e2e/bot-mode-row-click-mirrors-registry.spec.ts
#	apps/desktop/e2e/bot-mode-tab-shows-bot-name.spec.ts
#	apps/desktop/e2e/bot-roster-group-row-organisation.spec.ts
#	apps/desktop/e2e/bot-roster-ignores-infra-dirs.spec.ts
#	apps/desktop/e2e/bot-roster-timestamp-meta.spec.ts
#	apps/desktop/e2e/bot-roster-user-sections.spec.ts
#	apps/desktop/e2e/bot-routines-pane-narrow.spec.ts
#	apps/desktop/e2e/bot-row-open-recent-session.spec.ts
#	apps/desktop/e2e/bot-tile-ignores-ambient-composer-model.spec.ts
#	apps/desktop/e2e/group-composer-auto-grow.spec.ts
#	apps/desktop/e2e/group-create-gate-remote-roster.spec.ts
#	apps/desktop/e2e/group-prompt-renamed-primary-handle.spec.ts
#	apps/desktop/e2e/hosted-room-backend-continuity.spec.ts
#	apps/desktop/e2e/hosted-room-legacy-store-migration.spec.ts
#	apps/desktop/e2e/settings-scope-chips-bot-title.spec.ts
#	apps/desktop/e2e/worktree-branch-status.spec.ts
#	apps/desktop/electron/backend-probes.test.ts
#	apps/desktop/electron/connection-apply.test.ts
#	apps/desktop/electron/desktop-electron-pin.test.ts
#	apps/desktop/electron/desktop-uninstall.test.ts
#	apps/desktop/electron/gateway-file-download-transport.test.ts
#	apps/desktop/electron/gateway-stop-before-update.test.ts
#	apps/desktop/electron/github-api-auth.test.ts
#	apps/desktop/electron/registry-primary-profile-scope.test.ts
#	apps/desktop/electron/update-api-check.test.ts
#	apps/desktop/electron/update-handoff-marker.test.ts
#	apps/desktop/electron/venv-blocker-scan.test.ts
#	apps/desktop/scripts/after-extract.test.mjs
#	apps/desktop/scripts/local-pack-publish.test.mjs
#	apps/desktop/scripts/tasks-scroll.test.mjs
#	apps/desktop/src/app/settings/model-settings.test.tsx
#	apps/desktop/src/app/updates-overlay.blockers.test.tsx
#	apps/desktop/src/components/desktop-install-overlay.test.tsx
#	apps/desktop/src/lib/update-copy.test.ts
#	scripts/ci/check_os_marker_fakes.py
#	tests-js/desktop-mac-usage-descriptions.test.ts
#	tests-js/node-engine-alignment.test.ts
#	tests/agent/lsp/test_install_and_lint_fixes.py
#	tests/agent/test_command_token_source.py
#	tests/agent/test_compression_boundary_hook.py
#	tests/agent/test_create_openai_client_ssl_verify.py
#	tests/agent/test_custom_provider_ca_probes.py
#	tests/agent/test_endpoint_blackhole.py
#	tests/agent/test_estimator_parity.py
#	tests/agent/test_in_place_compaction.py
#	tests/agent/test_moa_loop_mode.py
#	tests/agent/test_model_metadata.py
#	tests/agent/test_skill_session_platform_gate.py
#	tests/agent/test_skill_utils.py
#	tests/agent/test_ssl_ca_guard.py
#	tests/computer_use/test_doctor.py
#	tests/cron/test_codex_execution_paths.py
#	tests/cron/test_cron_bot_chat_delivery.py
#	tests/cron/test_cron_script.py
#	tests/cron/test_media_delivery_parity.py
#	tests/cron/test_misfire_catchup.py
#	tests/cron/test_parallel_pool.py
#	tests/cron/test_recurring_eagain_redispatch.py
#	tests/gateway/test_choice_picker.py
#	tests/gateway/test_control_socket_windows_live.py
#	tests/gateway/test_dingtalk.py
#	tests/gateway/test_feishu.py
#	tests/gateway/test_feishu_onboard.py
#	tests/gateway/test_gateway_shutdown.py
#	tests/gateway/test_matrix.py
#	tests/gateway/test_model_command_custom_providers.py
#	tests/gateway/test_reasoning_command.py
#	tests/gateway/test_runtime_footer.py
#	tests/gateway/test_session.py
#	tests/gateway/test_session_hygiene.py
#	tests/gateway/test_status.py
#	tests/gateway/test_teams.py
#	tests/gateway/test_turn_lease.py
#	tests/gateway/test_whatsapp_connect.py
#	tests/hermes_cli/test_approvals_command.py
#	tests/hermes_cli/test_auth_store_lock_concurrent.py
#	tests/hermes_cli/test_backup.py
#	tests/hermes_cli/test_banner_git_state.py
#	tests/hermes_cli/test_certifi_repair.py
#	tests/hermes_cli/test_cmd_update.py
#	tests/hermes_cli/test_compat_manifest_targets.py
#	tests/hermes_cli/test_computer_use_cli.py
#	tests/hermes_cli/test_cpr_local_leak.py
#	tests/hermes_cli/test_dashboard_auth_gate.py
#	tests/hermes_cli/test_dashboard_procs_kill_grace.py
#	tests/hermes_cli/test_desktop_lifecycle_windows_live.py
#	tests/hermes_cli/test_doctor.py
#	tests/hermes_cli/test_doctor_command_install.py
#	tests/hermes_cli/test_fleet_config_migration_windows_live.py
#	tests/hermes_cli/test_gateway.py
#	tests/hermes_cli/test_gateway_platform_gating.py
#	tests/hermes_cli/test_gateway_restart_loop.py
#	tests/hermes_cli/test_gateway_task_probe.py
#	tests/hermes_cli/test_gateway_wsl.py
#	tests/hermes_cli/test_gui_command.py
#	tests/hermes_cli/test_install_cua_driver.py
#	tests/hermes_cli/test_kanban_db.py
#	tests/hermes_cli/test_lazy_command_exports.py
#	tests/hermes_cli/test_lazy_refresh_venv_repair.py
#	tests/hermes_cli/test_linux_desktop_entry.py
#	tests/hermes_cli/test_local_runtime.py
#	tests/hermes_cli/test_local_runtime_updates.py
#	tests/hermes_cli/test_managed_uv.py
#	tests/hermes_cli/test_mcp_reload_confirm_gate.py
#	tests/hermes_cli/test_nous_subscription.py
#	tests/hermes_cli/test_npm_engine.py
#	tests/hermes_cli/test_personality_none.py
#	tests/hermes_cli/test_pet_toggle.py
#	tests/hermes_cli/test_plan_reconciliation_windows_live.py
#	tests/hermes_cli/test_plugin_event_bus.py
#	tests/hermes_cli/test_plugin_manifest_v2.py
#	tests/hermes_cli/test_plugin_packs.py
#	tests/hermes_cli/test_plugins_cmd.py
#	tests/hermes_cli/test_plugins_cmd_enable_disable_nested.py
#	tests/hermes_cli/test_process_identity.py
#	tests/hermes_cli/test_profiles.py
#	tests/hermes_cli/test_profiles_sidebar_cache.py
#	tests/hermes_cli/test_pty_bridge.py
#	tests/hermes_cli/test_resolve_turn_limit.py
#	tests/hermes_cli/test_serve_runtime_inventory.py
#	tests/hermes_cli/test_session_vacuum_config.py
#	tests/hermes_cli/test_set_config_value.py
#	tests/hermes_cli/test_signal_handler_kanban_worker.py
#	tests/hermes_cli/test_slash_confirm_windows.py
#	tests/hermes_cli/test_stale_pid_guard.py
#	tests/hermes_cli/test_startup_fast_guards.py
#	tests/hermes_cli/test_status.py
#	tests/hermes_cli/test_telegram_managed_bot.py
#	tests/hermes_cli/test_tools_config.py
#	tests/hermes_cli/test_update_apply_shallow_count.py
#	tests/hermes_cli/test_update_autostash.py
#	tests/hermes_cli/test_update_concurrent_quarantine.py
#	tests/hermes_cli/test_update_fetch_failure_classifier.py
#	tests/hermes_cli/test_update_fleet_probe_resume_token.py
#	tests/hermes_cli/test_update_handoff_backend_reap.py
#	tests/hermes_cli/test_update_handoff_desktop_rebuild.py
#	tests/hermes_cli/test_update_head_moved_gate.py
#	tests/hermes_cli/test_update_host_obligation.py
#	tests/hermes_cli/test_update_import_guard.py
#	tests/hermes_cli/test_update_interrupted_recovery.py
#	tests/hermes_cli/test_update_inventory.py
#	tests/hermes_cli/test_update_launchd_unloaded_gateway.py
#	tests/hermes_cli/test_update_missing_configured_deps.py
#	tests/hermes_cli/test_update_modified_notice.py
#	tests/hermes_cli/test_update_multiplex_migration_hook.py
#	tests/hermes_cli/test_update_no_gateway_restart.py
#	tests/hermes_cli/test_update_orphan_backend_reap.py
#	tests/hermes_cli/test_update_parked_branch_guard.py
#	tests/hermes_cli/test_update_post_pull_syntax_guard.py
#	tests/hermes_cli/test_update_receipt.py
#	tests/hermes_cli/test_update_self_lock.py
#	tests/hermes_cli/test_update_shim_fail_closed.py
#	tests/hermes_cli/test_update_shim_self_lock.py
#	tests/hermes_cli/test_update_sqlite_remediation.py
#	tests/hermes_cli/test_update_stale_dashboard.py
#	tests/hermes_cli/test_update_stale_virtualenv.py
#	tests/hermes_cli/test_update_venv_health.py
#	tests/hermes_cli/test_update_venv_ownership_preflight.py
#	tests/hermes_cli/test_update_wedged_gateway.py
#	tests/hermes_cli/test_update_yes_flag.py
#	tests/hermes_cli/test_update_zip_two_phase.py
#	tests/hermes_cli/test_urllib_security.py
#	tests/hermes_cli/test_ux_messages_auth_config.py
#	tests/hermes_cli/test_ux_messages_startup.py
#	tests/hermes_cli/test_venv_holder_classifier.py
#	tests/hermes_cli/test_verify_console_scripts.py
#	tests/hermes_cli/test_verify_core_dependencies.py
#	tests/hermes_cli/test_web_server.py
#	tests/hermes_cli/test_web_server_console_ws.py
#	tests/hermes_cli/test_web_server_ws_ping.py
#	tests/hermes_cli/test_web_ui_build.py
#	tests/hermes_state/test_fts_rebuild_admission.py
#	tests/hermes_state/test_hermes_state.py
#	tests/plugins/memory/test_memory_lazy_install.py
#	tests/plugins/test_google_meet_plugin.py
#	tests/plugins/test_langfuse_plugin.py
#	tests/plugins/test_security_guidance_plugin.py
#	tests/plugins/test_transform_llm_output_hook.py
#	tests/scripts/desktop_update/test_desktop_update_windows_gateway_flag.py
#	tests/scripts/desktop_update/test_desktop_update_windows_python_handoff.py
#	tests/scripts/desktop_update/test_desktop_update_windows_timestamp.py
#	tests/scripts/install/test_install_clone_throttle_fallback.py
#	tests/scripts/install/test_install_lockfile_churn.py
#	tests/scripts/install/test_install_no_initial_commit.py
#	tests/scripts/install/test_install_sh_browser_install.py
#	tests/scripts/install/test_install_sh_node_prerelease.py
#	tests/scripts/install/test_install_sh_symlink_stomp.py
#	tests/scripts/install/test_install_sh_uv_lock_config.py
#	tests/scripts/install/test_install_unmerged_index.py
#	tests/scripts/test_contributor_map.py
#	tests/scripts/test_run_tests_parallel.py
#	tests/skills/test_competitor_news_monitor_skill.py
#	tests/skills/test_document_to_action_items_skill.py
#	tests/skills/test_google_workspace_setup.py
#	tests/skills/test_google_workspace_setup_deps.py
#	tests/skills/test_grounded_citations_skill.py
#	tests/skills/test_ip_as_logo_skill.py
#	tests/skills/test_live_dashboard_skill.py
#	tests/skills/test_mcp_oauth_remote_gateway_skill.py
#	tests/skills/test_office_document_skills.py
#	tests/skills/test_openclaw_migration.py
#	tests/skills/test_product_price_monitor_skill.py
#	tests/skills/test_scrollcraft_skill.py
#	tests/skills/test_setup_wizard_generator_skill.py
#	tests/skills/test_weekly_review_planning_skill.py
#	tests/test_engines_satisfiable.py
#	tests/test_fast_safe_load.py
#	tests/test_hermes_bootstrap.py
#	tests/test_hermes_constants.py
#	tests/test_hermes_logging.py
#	tests/test_managed_runtime_resolution.py
#	tests/test_model_tools_async_bridge.py
#	tests/test_packaging_build_guard.py
#	tests/test_packaging_metadata.py
#	tests/test_yaml_indent_consistency.py
#	tests/tools/test_approval_timeout_overflow.py
#	tests/tools/test_base_environment.py
#	tests/tools/test_bot_mode_dm.py
#	tests/tools/test_browser_chromium_check.py
#	tests/tools/test_browser_hardening.py
#	tests/tools/test_browser_homebrew_paths.py
#	tests/tools/test_browser_npx_warmup.py
#	tests/tools/test_browser_orphan_reaper.py
#	tests/tools/test_browser_real_profile.py
#	tests/tools/test_browser_use_cli.py
#	tests/tools/test_clipboard.py
#	tests/tools/test_code_execution.py
#	tests/tools/test_code_execution_modes.py
#	tests/tools/test_code_execution_windows_env.py
#	tests/tools/test_computer_use.py
#	tests/tools/test_delegate_liveness_timeout.py
#	tests/tools/test_execute_code_approval_cluster.py
#	tests/tools/test_execution_flag_detection.py
#	tests/tools/test_fal_common.py
#	tests/tools/test_file_operations.py
#	tests/tools/test_file_tools.py
#	tests/tools/test_file_tools_cwd_resolution.py
#	tests/tools/test_file_tools_live.py
#	tests/tools/test_lazy_deps.py
#	tests/tools/test_lazy_deps_durable_target.py
#	tests/tools/test_lazy_deps_managed.py
#	tests/tools/test_local_env_blocklist.py
#	tests/tools/test_local_tempdir.py
#	tests/tools/test_macos_protected_search.py
#	tests/tools/test_mcp_npx_cached_bin.py
#	tests/tools/test_oneshot_completion_linger.py
#	tests/tools/test_process_registry.py
#	tests/tools/test_read_file_schema_gating.py
#	tests/tools/test_skill_improvements.py
#	tests/tools/test_skills_sync.py
#	tests/tools/test_termux_api_detection.py
#	tests/tools/test_tirith_security.py
#	tests/tools/test_transcription_tools.py
#	tests/tools/test_tts_streaming.py
#	tests/tools/test_wake_word.py
#	tests/tui_gateway/test_compute_host_borrowed_lease.py
#	tests/tui_gateway/test_compute_host_turn_protocol.py
#	tests/tui_gateway/test_isolated_orphan_activity.py
#	tests/tui_gateway/test_protocol.py
#	tests/tui_gateway/test_slash_worker_profile_home.py
#	tests/tui_gateway/test_subprocess_encoding.py
#	tests/tui_gateway/test_tui_gateway_server.py
#	ui-tui/src/__tests__/terminalParity.test.ts
#	ui-tui/src/__tests__/termuxComposerLayout.test.ts
#	ui-tui/src/__tests__/textInputFastEcho.test.ts
2026-09-23 07:02:44 -04:00

688 lines
26 KiB
Python

"""Regression tests for issue #87711 — Ctrl+key / Alt+key combos broken
under modifyOtherKeys level 2.
When the CLI pushes ``ESC[>4;2m`` (modifyOtherKeys=2) to supported
terminals so Shift+Enter is distinguishable from Enter, the terminal
re-encodes EVERY Ctrl+key combo as ``ESC[27;5;<codepoint>~`` instead of
the raw control byte (``\\x01`` etc.). prompt_toolkit 3.x only ships a
mapping for ``ESC[27;5;13~`` (Ctrl+Enter = Ctrl+M); all other Ctrl+letter
combos are unmapped and leak as literal text or get swallowed.
``install_modify_other_keys_aliases()`` populates ``ANSI_SEQUENCES`` with
the full set so every Ctrl+combo continues to fire the same key binding
the raw byte would.
"""
from __future__ import annotations
import pytest
from prompt_toolkit.input.ansi_escape_sequences import ANSI_SEQUENCES
from prompt_toolkit.input.vt100_parser import Vt100Parser
from prompt_toolkit.keys import Keys
from prompt_toolkit.output import DummyOutput
from hermes_cli.pt_input_extras import install_modify_other_keys_aliases
@pytest.fixture(autouse=True)
def _ensure_alias_installed():
"""Install the alias for each test, then restore ANSI_SEQUENCES to its
prior state so the hundreds of installed mappings don't leak into
sibling test files."""
from prompt_toolkit.input.ansi_escape_sequences import ANSI_SEQUENCES as _seq
saved = dict(_seq)
install_modify_other_keys_aliases()
yield
_seq.clear()
_seq.update(saved)
# Drop the parser's prefix cache too — it was computed against the
# augmented table and would go stale after the restore above.
from prompt_toolkit.input.vt100_parser import _IS_PREFIX_OF_LONGER_MATCH_CACHE
_IS_PREFIX_OF_LONGER_MATCH_CACHE.clear()
def _parse(byte_seq: str):
"""Feed bytes through prompt_toolkit's VT100 parser and return the
list of KeyPress objects."""
out = []
parser = Vt100Parser(out.append)
for ch in byte_seq:
parser.feed(ch)
parser.flush()
return [kp.key for kp in out]
# ---------------------------------------------------------------------------
# Ctrl+letter: a-z
# ---------------------------------------------------------------------------
CTRL_LETTERS = [chr(c) for c in range(ord('a'), ord('z') + 1)]
@pytest.mark.parametrize("letter", CTRL_LETTERS)
def test_modify_other_keys_ctrl_letter_parses_as_raw_byte(letter):
"""Ctrl+<letter> under modifyOtherKeys must parse identically to the
raw control byte that prompt_toolkit already understands."""
raw_byte = chr(ord(letter) - ord('a') + 1) # Ctrl+a = \x01, etc.
raw_result = _parse(raw_byte)
assert len(raw_result) == 1, f"raw byte {raw_byte!r} should produce 1 keypress"
# modifyOtherKeys format
mok_seq = f"\x1b[27;5;{ord(letter)}~"
mok_result = _parse(mok_seq)
assert mok_result == raw_result, (
f"modifyOtherKeys Ctrl+{letter} ({mok_seq!r}) should parse identically "
f"to raw {raw_byte!r}; got {mok_result!r} vs {raw_result!r}"
)
# CSI-u format
csiu_seq = f"\x1b[{ord(letter)};5u"
csiu_result = _parse(csiu_seq)
assert csiu_result == raw_result, (
f"CSI-u Ctrl+{letter} ({csiu_seq!r}) should parse identically "
f"to raw {raw_byte!r}; got {csiu_result!r} vs {raw_result!r}"
)
# ---------------------------------------------------------------------------
# Critical individual shortcuts
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Ctrl+digit: 0-9
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("digit", [str(d) for d in range(10)])
def test_modify_other_keys_ctrl_digit(digit):
"""Ctrl+digit under modifyOtherKeys must map to Keys.Control<digit>."""
codepoint = ord(digit)
expected = getattr(Keys, f"Control{digit}")
mok_seq = f"\x1b[27;5;{codepoint}~"
assert _parse(mok_seq) == [expected], (
f"modifyOtherKeys Ctrl+{digit} ({mok_seq!r}) should parse as {expected}"
)
csiu_seq = f"\x1b[{codepoint};5u"
assert _parse(csiu_seq) == [expected], (
f"CSI-u Ctrl+{digit} ({csiu_seq!r}) should parse as {expected}"
)
# ---------------------------------------------------------------------------
# Ctrl+symbol
# ---------------------------------------------------------------------------
def test_ctrl_left_bracket_under_modify_other_keys():
"""Ctrl+[ = Escape under modifyOtherKeys."""
assert _parse("\x1b[27;5;91~") == _parse("\x1b")
assert _parse("\x1b[91;5u") == _parse("\x1b")
def test_ctrl_backslash_under_modify_other_keys():
"""Ctrl+\\ = ControlBackslash under modifyOtherKeys."""
assert _parse("\x1b[27;5;92~") == [Keys.ControlBackslash]
assert _parse("\x1b[92;5u") == [Keys.ControlBackslash]
def test_ctrl_right_bracket_under_modify_other_keys():
"""Ctrl+] = ControlSquareClose under modifyOtherKeys."""
assert _parse("\x1b[27;5;93~") == [Keys.ControlSquareClose]
assert _parse("\x1b[93;5u") == [Keys.ControlSquareClose]
def test_ctrl_space_under_modify_other_keys():
"""Ctrl+Space = ControlAt (NUL) under modifyOtherKeys."""
assert _parse("\x1b[27;5;32~") == _parse("\x00")
assert _parse("\x1b[32;5u") == _parse("\x00")
# ---------------------------------------------------------------------------
# Alt+letter
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("letter", [chr(c) for c in range(ord('a'), ord('z') + 1)])
def test_modify_other_keys_alt_letter_parses_as_escape_letter(letter):
"""Alt+<letter> under modifyOtherKeys must parse to the same tuple
that a bare ESC+<letter> produces."""
bare_result = _parse(f"\x1b{letter}")
assert len(bare_result) == 2, f"bare ESC+{letter} should produce 2 keypresses"
mok_seq = f"\x1b[27;3;{ord(letter)}~"
mok_result = _parse(mok_seq)
assert mok_result == bare_result, (
f"modifyOtherKeys Alt+{letter} ({mok_seq!r}) should parse identically "
f"to bare ESC+{letter}; got {mok_result!r} vs {bare_result!r}"
)
csiu_seq = f"\x1b[{ord(letter)};3u"
csiu_result = _parse(csiu_seq)
assert csiu_result == bare_result, (
f"CSI-u Alt+{letter} ({csiu_seq!r}) should parse identically "
f"to bare ESC+{letter}; got {csiu_result!r} vs {bare_result!r}"
)
# ---------------------------------------------------------------------------
# Idempotency and non-clobbering
# ---------------------------------------------------------------------------
def test_install_is_idempotent():
install_modify_other_keys_aliases()
assert install_modify_other_keys_aliases() == 0
# ---------------------------------------------------------------------------
# Shift+letter → uppercase
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("letter", [chr(c) for c in range(ord('a'), ord('z') + 1)])
def test_modify_other_keys_shift_letter_produces_uppercase(letter):
"""Shift+<letter> under modifyOtherKeys must produce the uppercase
character, not leak as literal escape text — the 'caps locked' bug."""
upper = letter.upper()
# modifyOtherKeys format
mok_seq = f"\x1b[27;2;{ord(letter)}~"
assert _parse(mok_seq) == [upper], (
f"modifyOtherKeys Shift+{letter} ({mok_seq!r}) should produce '{upper}'"
)
# CSI-u format
csiu_seq = f"\x1b[{ord(letter)};2u"
assert _parse(csiu_seq) == [upper], (
f"CSI-u Shift+{letter} ({csiu_seq!r}) should produce '{upper}'"
)
# ---------------------------------------------------------------------------
# Shift+symbols (modifyOtherKeys tilde form)
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("cp", [
58, # ':'
95, # '_' (Shift+Minus)
123, # '{'
125, # '}'
])
def test_modify_other_keys_shift_symbol_produces_char(cp):
"""Under modifyOtherKeys=2, Shift+symbol (e.g. Shift+[ -> '{') arrives as
ESC[27;2;<produced_cp>~ (xterm's own key table; Ghostty follows it). It must parse
to the produced character, not leak literal escape text. The kitty CSI-u spelling
carries the UNSHIFTED codepoint and must stay unmapped (layout-specific)."""
ch = chr(cp)
mok_seq = f"\x1b[27;2;{cp}~"
assert _parse(mok_seq) == [ch], (
f"modifyOtherKeys Shift+symbol ({mok_seq!r}) should produce {ch!r}"
)
assert ANSI_SEQUENCES.get(f"\x1b[{cp};2u") is None
@pytest.mark.parametrize("seq, ch", [
("\x1b[27;9;111~", "o"), # Super+o (the #114242 report)
("\x1b[27;10;79~", "O"), # Super+Shift+o
])
def test_modify_other_keys_super_printable_produces_char(seq, ch):
"""Ghostty encodes Super+<printable> as ESC[27;9;<cp>~ under modifyOtherKeys=2;
the CLI has no Super bindings, so it must type the character (Ink TUI parity),
not leak the escape text (#114242)."""
assert _parse(seq) == [ch]
def test_shift_symbol_data_normalized_in_buffer():
"""End-to-end: Vt100Parser with install_keypress_data_normalization
must deliver the character in KeyPress.data, not the raw escape."""
from hermes_cli.pt_input_extras import install_keypress_data_normalization
install_keypress_data_normalization()
out = []
parser = Vt100Parser(out.append)
for c in "\x1b[27;2;95~":
parser.feed(c)
parser.flush()
assert len(out) == 1
assert out[0].key == "_"
assert out[0].data == "_"
def test_does_not_clobber_shift_enter_alias():
"""install_modify_other_keys_aliases must not overwrite mappings
installed by install_shift_enter_alias (modifier=2, not 5)."""
from hermes_cli.pt_input_extras import install_shift_enter_alias
install_shift_enter_alias()
assert ANSI_SEQUENCES["\x1b[27;2;13~"] == (Keys.Escape, Keys.ControlM)
assert ANSI_SEQUENCES["\x1b[13;2u"] == (Keys.Escape, Keys.ControlM)
def test_ctrl_enter_still_works_under_modify_other_keys():
"""Ctrl+Enter must produce the Alt+Enter newline tuple, not plain Ctrl+M.
This is the install_ctrl_enter_alias behavior — our new function must
not clobber it."""
from hermes_cli.pt_input_extras import install_ctrl_enter_alias
install_ctrl_enter_alias()
install_modify_other_keys_aliases()
alt_enter = _parse("\x1b\r")
ctrl_enter_mok = _parse("\x1b[27;5;13~")
ctrl_enter_csiu = _parse("\x1b[13;5u")
assert ctrl_enter_mok == alt_enter
assert ctrl_enter_csiu == alt_enter
# ---------------------------------------------------------------------------
# Kitty keyboard protocol: the Esc KEY (disambiguate mode, #56684)
# ---------------------------------------------------------------------------
def test_kitty_plain_escape_key():
"""The Esc key under kitty disambiguate mode (ESC[27u) must parse as
Keys.Escape, not leak '[27u' as literal text."""
assert _parse("\x1b[27u") == [Keys.Escape]
@pytest.mark.parametrize("modifier", range(2, 9))
def test_kitty_modified_escape_key(modifier):
"""Modified Esc (Shift+Esc etc.) still behaves as Escape."""
assert _parse(f"\x1b[27;{modifier}u") == [Keys.Escape]
# ---------------------------------------------------------------------------
# Modified Enter / Tab / Backspace / Space
# ---------------------------------------------------------------------------
def test_alt_enter_produces_newline_tuple():
"""Alt+Enter under either protocol must equal bare ESC+CR (newline)."""
alt_enter = _parse("\x1b\r")
assert _parse("\x1b[13;3u") == alt_enter
assert _parse("\x1b[27;3;13~") == alt_enter
def test_shift_tab_produces_backtab():
"""Shift+Tab must behave like the legacy ESC[Z (BackTab)."""
assert _parse("\x1b[9;2u") == [Keys.BackTab]
assert _parse("\x1b[27;2;9~") == [Keys.BackTab]
def test_ctrl_backspace_produces_backward_kill_word():
"""Ctrl+Backspace must produce the (Escape, ControlH) tuple that fires
prompt_toolkit's backward-kill-word — parity with the Ink TUI and
Desktop (#78285)."""
expected = [Keys.Escape, Keys.ControlH]
assert _parse("\x1b[127;5u") == expected
assert _parse("\x1b[27;5;127~") == expected
def test_alt_backspace_produces_backward_kill_word():
"""Alt+Backspace = ESC+DEL = backward-kill-word."""
assert _parse("\x1b[127;3u") == [Keys.Escape, Keys.ControlH]
assert _parse("\x1b[27;3;127~") == [Keys.Escape, Keys.ControlH]
def test_shift_backspace_is_plain_backspace():
assert _parse("\x1b[127;2u") == _parse("\x7f")
def test_shift_space_inserts_space():
"""Shift+Space must insert a space, not leak escape text (#86866)."""
assert _parse("\x1b[32;2u") == [" "]
assert _parse("\x1b[27;2;32~") == [" "]
def _parse_presses(byte_seq: str):
"""Feed bytes through the VT100 parser and return the full KeyPress
objects (key + data), so buffer-level data leakage is observable."""
from prompt_toolkit.input.vt100_parser import Vt100Parser as _Vt100Parser
from prompt_toolkit.key_binding.key_processor import KeyPress as _KeyPress
out = []
parser = _Vt100Parser(out.append)
for ch in byte_seq:
parser.feed(ch)
parser.flush()
assert all(isinstance(kp, _KeyPress) for kp in out)
return out
@pytest.mark.parametrize(
"seq",
["\x1b[32;2u", "\x1b[27;2;32~"], # kitty CSI-u and xterm modifyOtherKeys
)
def test_shift_space_keypress_data_is_plain_space(seq):
"""The KeyPress data for Shift+Space must be ' ', not the raw CSI
sequence — self-insert inserts event.data, so raw bytes would leak
into the buffer even though the key is correctly mapped (#88071)."""
from hermes_cli.pt_input_extras import install_keypress_data_normalization
install_keypress_data_normalization()
presses = _parse_presses(seq)
assert [kp.key for kp in presses] == [" "]
assert [kp.data for kp in presses] == [" "], (
f"{seq!r} KeyPress data must be ' ', got {[kp.data for kp in presses]!r}"
)
@pytest.mark.parametrize("seq", ["\x1b[97;2u", "\x1b[27;2;97~"])
def test_shift_letter_keypress_data_is_uppercase(seq):
"""Shift+letter (modifier 2) maps to the uppercase letter; its KeyPress
data must be that letter, not the raw escape text (#88071)."""
from hermes_cli.pt_input_extras import install_keypress_data_normalization
install_keypress_data_normalization()
presses = _parse_presses(seq)
assert [kp.key for kp in presses] == ["A"]
assert [kp.data for kp in presses] == ["A"], (
f"{seq!r} KeyPress data must be 'A', got {[kp.data for kp in presses]!r}"
)
def test_keypad_digit_keypress_data_is_digit():
"""Keypad digits (Kitty PUA) map to plain digits; their KeyPress data
must be the digit, not the raw escape text (#88071)."""
from hermes_cli.pt_input_extras import install_keypress_data_normalization
install_keypress_data_normalization()
presses = _parse_presses("\x1b[57404u")
assert [kp.key for kp in presses] == ["5"]
assert [kp.data for kp in presses] == ["5"], (
f"keypad 5 KeyPress data must be '5', got {[kp.data for kp in presses]!r}"
)
def test_buffer_level_shift_space_no_raw_csi():
"""End-to-end: feeding Shift+Space through a real Application must put
a space in the buffer, not raw CSI bytes (#88071).
This is the regression the parser-only tests miss: Vt100Parser maps
the key to ' ' but the KeyPress data still carried the raw sequence,
and the default self-insert binding inserts event.data.
"""
import asyncio
from prompt_toolkit import Application
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.input import create_pipe_input
from prompt_toolkit.layout import HSplit, Layout, Window, BufferControl
from hermes_cli.pt_input_extras import install_keypress_data_normalization
install_keypress_data_normalization()
async def _probe(payload: str) -> str:
buf = Buffer()
with create_pipe_input() as inp:
app = Application(
layout=Layout(HSplit([Window(BufferControl(buf))])), input=inp, output=DummyOutput()
)
run_task = asyncio.ensure_future(app.run_async())
await asyncio.sleep(0.05)
inp.send_text("ab")
inp.send_text(payload)
inp.send_text("cd")
await asyncio.sleep(0.15)
result = buf.text
app.exit()
try:
await asyncio.wait_for(run_task, 2)
except Exception:
pass
return result
for label, payload in (
("Shift+Space xterm", "\x1b[27;2;32~"),
("Shift+Space kitty", "\x1b[32;2u"),
("plain space", " "),
):
buffer = asyncio.run(_probe(payload))
assert buffer == "ab cd", (
f"{label}: buffer={buffer!r} — expected 'ab cd'; raw CSI bytes "
f"must never land in the buffer"
)
def test_buffer_level_shift_letter_no_raw_csi():
"""End-to-end: Shift+letter through a real Application must type the
capital letter, not literal ``^[[27;2;<code>~`` text (#92343).
Same KeyPress.data defect as Shift+Space (#88071), surfaced live on
Ghostty after 1a8fea3ce2 dropped it onto the modifyOtherKeys-only path:
ANSI_SEQUENCES maps the sequence to 'M', but self-insert pastes
event.data — the raw escape bytes.
"""
import asyncio
from prompt_toolkit import Application
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.input import create_pipe_input
from prompt_toolkit.layout import HSplit, Layout, Window, BufferControl
from hermes_cli.pt_input_extras import install_keypress_data_normalization
install_keypress_data_normalization()
async def _probe(payload: str) -> str:
buf = Buffer()
with create_pipe_input() as inp:
app = Application(
layout=Layout(HSplit([Window(BufferControl(buf))])), input=inp, output=DummyOutput()
)
run_task = asyncio.ensure_future(app.run_async())
await asyncio.sleep(0.05)
inp.send_text("ab")
inp.send_text(payload)
inp.send_text("cd")
await asyncio.sleep(0.15)
result = buf.text
app.exit()
try:
await asyncio.wait_for(run_task, 2)
except Exception:
pass
return result
for label, payload, expected in (
("Shift+M xterm modifyOtherKeys", "\x1b[27;2;77~", "abMcd"),
("Shift+M kitty CSI-u (shifted cp)", "\x1b[77;2u", "abMcd"),
("Shift+L kitty CSI-u (unshifted cp)", "\x1b[108;2u", "abLcd"),
("plain letter", "M", "abMcd"),
):
buffer = asyncio.run(_probe(payload))
assert buffer == expected, (
f"{label}: buffer={buffer!r} — expected {expected!r}; raw CSI "
f"bytes must never land in the buffer"
)
# ---------------------------------------------------------------------------
# Multi-modifier combos (Ctrl+Shift / Ctrl+Alt / Shift+Alt / Ctrl+Alt+Shift)
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("letter", ["c", "r", "z"])
def test_ctrl_shift_letter_behaves_as_ctrl(letter):
"""Ctrl+Shift+<letter> (modifier 6) must fire the same key as
Ctrl+<letter> — both the unshifted and shifted codepoint variants."""
expected = _parse(chr(ord(letter) & 0x1F))
for cp in (ord(letter), ord(letter) - 32):
assert _parse(f"\x1b[{cp};6u") == expected
assert _parse(f"\x1b[27;6;{cp}~") == expected
def test_ctrl_alt_letter_behaves_as_escape_ctrl():
"""Ctrl+Alt+a (modifier 7) = Escape prefix + ControlA."""
assert _parse("\x1b[97;7u") == [Keys.Escape, Keys.ControlA]
def test_ctrl_alt_shift_letter_behaves_as_escape_ctrl():
"""Ctrl+Alt+Shift+a (modifier 8) = Escape prefix + ControlA."""
assert _parse("\x1b[97;8u") == [Keys.Escape, Keys.ControlA]
def test_shift_alt_letter_behaves_as_escape_upper():
"""Shift+Alt+f (modifier 4) = Escape prefix + 'F'."""
assert _parse("\x1b[102;4u") == [Keys.Escape, "F"]
# ---------------------------------------------------------------------------
# Kitty functional keys (Private Use Area codepoints)
# ---------------------------------------------------------------------------
def test_keypad_enter_behaves_as_enter():
assert _parse("\x1b[57414u") == _parse("\r")
@pytest.mark.parametrize("digit", range(10))
def test_keypad_digits_type_digits(digit):
assert _parse(f"\x1b[{57399 + digit}u") == [str(digit)]
def test_keypad_navigation_maps_to_arrows():
assert _parse("\x1b[57417u") == [Keys.Left]
assert _parse("\x1b[57418u") == [Keys.Right]
assert _parse("\x1b[57419u") == [Keys.Up]
assert _parse("\x1b[57420u") == [Keys.Down]
def test_f13_maps_to_f13():
assert _parse("\x1b[57376u") == [Keys.F13]
@pytest.mark.parametrize("code", [57358, 57428, 57441, 57448])
def test_lock_media_modifier_events_are_consumed(code):
"""Caps Lock, media keys, and bare modifier press events must be
consumed (Keys.Ignore), never leak as literal text."""
result = _parse(f"\x1b[{code}u")
assert result == [Keys.Ignore], f"CSI {code}u leaked: {result!r}"
def test_cmd_backspace_alias_not_clobbered():
"""install_cmd_backspace_alias's super-modifier mappings must survive."""
from hermes_cli.pt_input_extras import install_cmd_backspace_alias
install_cmd_backspace_alias()
install_modify_other_keys_aliases()
assert _parse("\x1b[127;9u") == [Keys.ControlU]
# ---------------------------------------------------------------------------
# Lock-bit variants (#89651): kitty/ghostty OR the CapsLock (64) / NumLock
# (128) state into the CSI-u modifier parameter, so with a lock enabled
# every combo arrives shifted (ESC[99;133u instead of ESC[99;5u) and died
# as literal text without these aliases.
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("letter", CTRL_LETTERS)
def test_ctrl_letter_with_numlock_parses_as_raw_byte(letter):
"""Ctrl+<letter> with NumLock on (modifier + 128) must parse identically
to the raw control byte — the exact garbage from #89651 ([127;133u)."""
raw_byte = chr(ord(letter) - ord('a') + 1)
raw_result = _parse(raw_byte)
numlock_seq = f"\x1b[{ord(letter)};133u" # 5 + 128
assert _parse(numlock_seq) == raw_result, (
f"NumLock Ctrl+{letter} ({numlock_seq!r}) should parse identically "
f"to raw {raw_byte!r}"
)
@pytest.mark.parametrize("letter", ["a", "c", "z"])
def test_ctrl_letter_with_capslock_parses_as_raw_byte(letter):
raw_byte = chr(ord(letter) - ord('a') + 1)
capslock_seq = f"\x1b[{ord(letter)};69u" # 5 + 64
assert _parse(capslock_seq) == _parse(raw_byte)
def test_ctrl_c_with_both_locks_parses_as_raw_byte():
"""Ctrl+C with CapsLock and NumLock both on (5 + 64 + 128 = 197)."""
assert _parse("\x1b[99;197u") == _parse("\x03")
def test_alt_letter_with_numlock_keeps_escape_prefix():
assert _parse("\x1b[97;131u") == [Keys.Escape, "a"] # 3 + 128
def test_shift_letter_with_capslock_types_uppercase():
assert _parse("\x1b[97;66u") == ["A"] # 2 + 64
def test_esc_key_with_numlock_is_escape():
assert _parse("\x1b[27;129u") == [Keys.Escape] # 1 + 128
assert _parse("\x1b[27;133u") == [Keys.Escape] # 5 + 128
def test_ctrl_backspace_with_numlock_is_backward_kill_word():
"""The exact sequence from the #89651 report ([127;133u)."""
assert _parse("\x1b[127;133u") == [Keys.Escape, Keys.ControlH]
def test_modify_other_keys_tilde_form_has_no_lock_variants():
"""The xterm modifyOtherKeys encoding never carries lock bits, so no
+64/+128 variants of the ESC[27;N;CP~ form may be installed."""
for seq in ("\x1b[27;69;99~", "\x1b[27;133;99~", "\x1b[27;197;99~"):
assert seq not in ANSI_SEQUENCES
# ---------------------------------------------------------------------------
# Follow-up widening: lock twins on the alias installers, legacy CSI-letter /
# CSI-tilde navigation, unmodified CSI-u keys, and PUA functional keys.
# ---------------------------------------------------------------------------
def test_lock_bits_on_legacy_cursor_keys_map_to_plain_keys():
"""kitty stamps lock bits onto legacy CSI-letter arrows too:
plain Down + NumLock = ESC[1;129B, CapsLock = ESC[1;65B, both = 193."""
for mod, key in ((129, Keys.Down), (65, Keys.Down), (193, Keys.Down)):
assert _parse(f"\x1b[1;{mod}B") == [key]
assert _parse("\x1b[1;130B") == [Keys.ShiftDown] # Shift + NumLock
assert _parse("\x1b[1;131D") == [Keys.Escape, Keys.Left] # Alt + NumLock
assert _parse("\x1b[1;133D") == [Keys.ControlLeft] # Ctrl + NumLock
def test_lock_bits_on_tilde_navigation_keys():
"""Delete/PageUp/etc. carry the modifier in CSI-tilde form."""
assert _parse("\x1b[3;129~") == [Keys.Delete]
assert _parse("\x1b[3;69~") == [Keys.ControlDelete] # Ctrl+Delete + Caps
assert _parse("\x1b[5;193~") == [Keys.PageUp] # both locks
def test_lock_bits_on_plain_f1_through_f4():
"""Plain F1-F4 base mappings are SS3 (ESC O P); their CSI lock twins
must still resolve (ESC[1;129P etc.)."""
base = _parse("\x1bOP")
assert _parse("\x1b[1;129P") == base
assert _parse("\x1b[1;65P") == base
def test_lock_bits_on_unmodified_csi_u_keys():
"""Tab/Enter/Space/Backspace with only a lock held (modifier 1+lock)."""
assert _parse("\x1b[9;65u") == _parse("\t")
assert _parse("\x1b[13;193u") == _parse("\r")
assert _parse("\x1b[32;129u") == _parse(" ")
assert _parse("\x1b[127;129u") == _parse("\x7f")
def test_lock_bits_on_pua_functional_keys():
"""Kitty PUA functional keys (keypad, F13+) keep working under locks —
NumLock especially matters because it gates the keypad itself."""
assert _parse("\x1b[57399;129u") == ["0"] # KP_0 + NumLock
assert _parse("\x1b[57376;129u") == [Keys.F13] # F13 + NumLock
assert _parse("\x1b[57427;129u") == [Keys.Ignore] # KP_BEGIN + NumLock
def test_lock_bits_on_shift_enter_and_ctrl_enter_aliases():
from hermes_cli.pt_input_extras import (
install_ctrl_enter_alias,
install_shift_enter_alias,
)
install_shift_enter_alias()
install_ctrl_enter_alias()
newline = _parse("\x1b\r")
assert _parse("\x1b[13;130u") == newline # Shift+Enter + NumLock
assert _parse("\x1b[13;66u") == newline # Shift+Enter + CapsLock
assert _parse("\x1b[13;133u") == newline # Ctrl+Enter + NumLock
def test_lock_bits_on_cmd_backspace_alias():
from hermes_cli.pt_input_extras import install_cmd_backspace_alias
install_cmd_backspace_alias()
assert _parse("\x1b[127;137u") == [Keys.ControlU] # Cmd+Backspace + NumLock
assert _parse("\x1b[127;73u") == [Keys.ControlU] # Cmd+Backspace + Caps
assert _parse("\x1b[3;137~") == [Keys.ControlK] # Cmd+FwdDel + NumLock