fix(cli): keep destructive slash modal on Linux

This commit is contained in:
LeonSGP43 2026-05-26 11:16:42 +08:00 committed by Teknium
parent f0de3cd0a0
commit 458a94e425
2 changed files with 99 additions and 38 deletions

80
cli.py
View file

@ -7154,11 +7154,13 @@ class HermesCLI:
* ``sys.platform == "win32"`` native Windows console (ConPTY /
win32_input) does not support the modal reliably.
* Called from a non-main thread the prompt_toolkit event loop only
runs on the main thread; key bindings can't fire from a daemon
thread (same rationale as the ``_prompt_text_input`` thread guard
in PR #23454).
* ``self._app`` is not set unit tests / non-interactive contexts.
On non-Windows platforms the modal itself is still safe from the
``process_loop`` daemon thread as long as the main-thread event loop
owns the prompt_toolkit buffer mutations. When we are off the main
thread, schedule the modal snapshot / restore work on ``self._app.loop``
via ``call_soon_threadsafe`` and keep the queue-based response path.
"""
import threading
import time as _time
@ -7179,33 +7181,62 @@ class HermesCLI:
if sys.platform == "win32":
return self._prompt_text_input("Choice [1/2/3]: ")
# Mirror the thread-aware guard from _prompt_text_input (PR #23454):
# run_in_terminal and the modal queue both depend on the main-thread
# event loop. From a daemon thread the modal key bindings never fire.
if threading.current_thread() is not threading.main_thread():
try:
app_loop = self._app.loop
except Exception:
app_loop = None
in_main_thread = threading.current_thread() is threading.main_thread()
if not in_main_thread and app_loop is None:
return self._prompt_text_input("Choice [1/2/3]: ")
response_queue = queue.Queue()
self._capture_modal_input_snapshot()
self._slash_confirm_state = {
"title": title,
"detail": detail,
"choices": choices,
"selected": 0,
"response_queue": response_queue,
}
self._slash_confirm_deadline = _time.monotonic() + timeout
self._invalidate()
def _setup_modal() -> None:
self._capture_modal_input_snapshot()
self._slash_confirm_state = {
"title": title,
"detail": detail,
"choices": choices,
"selected": 0,
"response_queue": response_queue,
}
self._slash_confirm_deadline = _time.monotonic() + timeout
self._invalidate()
def _teardown_modal() -> None:
self._slash_confirm_state = None
self._slash_confirm_deadline = 0
self._restore_modal_input_snapshot()
self._invalidate()
def _run_on_app_loop(fn) -> bool:
if in_main_thread or app_loop is None:
fn()
return True
ready = threading.Event()
def _wrapped() -> None:
try:
fn()
finally:
ready.set()
try:
app_loop.call_soon_threadsafe(_wrapped)
except Exception:
return False
return ready.wait(timeout=5)
if not _run_on_app_loop(_setup_modal):
return self._prompt_text_input("Choice [1/2/3]: ")
_last_countdown_refresh = _time.monotonic()
try:
while True:
try:
result = response_queue.get(timeout=1)
self._slash_confirm_state = None
self._slash_confirm_deadline = 0
self._restore_modal_input_snapshot()
self._invalidate()
_run_on_app_loop(_teardown_modal)
return result
except queue.Empty:
remaining = self._slash_confirm_deadline - _time.monotonic()
@ -7217,10 +7248,7 @@ class HermesCLI:
self._invalidate()
finally:
if self._slash_confirm_state is not None:
self._slash_confirm_state = None
self._slash_confirm_deadline = 0
self._restore_modal_input_snapshot()
self._invalidate()
_run_on_app_loop(_teardown_modal)
return None
def _submit_slash_confirm_response(self, value: str | None) -> None: