diff --git a/libs/code/deepagents_code/widgets/chat_input.py b/libs/code/deepagents_code/widgets/chat_input.py index 5498275d04b..8ca9fb92de2 100644 --- a/libs/code/deepagents_code/widgets/chat_input.py +++ b/libs/code/deepagents_code/widgets/chat_input.py @@ -61,6 +61,22 @@ def _default_history_path() -> Path: _PASTE_BURST_START_CHARS = {"'", '"'} """Characters that can start dropped-path payloads.""" +_PASTE_BURST_MIN_CHARS = 3 +"""Consecutive fast keystrokes before a stream is treated as a paste burst. + +Terminals that lack bracketed paste replay a paste as individual key events. +Counting a short run of rapid chars distinguishes that from human typing, +which has much larger inter-key gaps. +""" + +_PASTE_ENTER_SUPPRESS_WINDOW_SECONDS = 0.12 +"""Window after recent burst activity during which `enter` inserts a newline. + +Keeps multi-line pastes grouped as one input even when newlines arrive as +`enter` key events slightly after the surrounding characters (e.g. across +terminal read boundaries), instead of submitting mid-paste. +""" + _BACKSLASH_ENTER_GAP_SECONDS = 0.15 """Maximum gap between a `\\` key and a following `enter` key to treat the pair as a terminal-emitted shift+enter sequence. @@ -408,12 +424,19 @@ def __init__(self, **kwargs: Any) -> None: self._chat_input_owner: ChatInput | None = None self._skip_history_change_events = 0 self._completion_active = False - # Buffer quote-prefixed high-frequency key bursts from terminals that - # emulate paste via rapid key events instead of dispatching a paste - # event. + # Buffer high-frequency key bursts from terminals that emulate paste via + # rapid key events instead of dispatching a paste event. self._paste_burst_buffer = "" self._paste_burst_last_char_time: float | None = None self._paste_burst_timer: Timer | None = None + # Counts consecutive rapid keystrokes so a paste-like stream can be + # detected even when it doesn't begin with a quote. + self._paste_burst_run = 0 + self._paste_burst_last_key_time: float | None = None + self._paste_burst_last_suppressed_enter_time: float | None = None + # Deadline until which `enter` inserts a newline rather than submitting, + # keeping multi-line pastes grouped across read boundaries. + self._paste_burst_window_until: float | None = None # See _BACKSLASH_ENTER_GAP_SECONDS for context. self._backslash_pending_time: float | None = None @@ -651,6 +674,7 @@ def _start_paste_burst(self, char: str, now: float) -> None: """Start buffering a paste-like keystroke burst.""" self._paste_burst_buffer = char self._paste_burst_last_char_time = now + self._paste_burst_window_until = now + _PASTE_ENTER_SUPPRESS_WINDOW_SECONDS self._schedule_paste_burst_flush() def _append_paste_burst(self, text: str, now: float) -> None: @@ -660,13 +684,66 @@ def _append_paste_burst(self, text: str, now: float) -> None: return self._paste_burst_buffer += text self._paste_burst_last_char_time = now + self._paste_burst_window_until = now + _PASTE_ENTER_SUPPRESS_WINDOW_SECONDS self._schedule_paste_burst_flush() + def _note_paste_burst_keystroke(self, now: float) -> None: + """Track inter-key timing to count consecutive rapid keystrokes.""" + last = self._paste_burst_last_key_time + if last is not None and (now - last) <= _PASTE_BURST_CHAR_GAP_SECONDS: + self._paste_burst_run += 1 + else: + self._paste_burst_run = 1 + self._paste_burst_last_key_time = now + + def _reset_paste_burst_run(self) -> None: + """Clear consecutive-keystroke tracking after non-burst input.""" + self._paste_burst_run = 0 + self._paste_burst_last_key_time = None + self._paste_burst_last_suppressed_enter_time = None + + def _enter_inserts_newline_during_burst(self, now: float) -> bool: + """Return whether `enter` should insert a newline rather than submit. + + True when the preceding keystroke was part of a rapid run or the + previous `enter` was already suppressed, and the suppression window is + still open. The char-gap check keeps a deliberate `enter` pressed after + a burst settles from being swallowed; the window bounds how long a + replayed paste's newlines stay grouped. Slash-command context always + keeps `enter`'s submit/dispatch semantics. + """ + if self._in_slash_command_context(): + return False + # Defensive: the plain-Enter caller appends-and-returns or flushes any + # active buffer before this helper runs, so this branch is unreachable + # today. It keeps the helper's contract self-contained. + if self._paste_burst_buffer: + return True + until = self._paste_burst_window_until + if until is None or now > until: + return False + last_enter = self._paste_burst_last_suppressed_enter_time + if last_enter is not None: + return True + last_key = self._paste_burst_last_key_time + return ( + last_key is not None and (now - last_key) <= _PASTE_BURST_CHAR_GAP_SECONDS + ) + + def _in_slash_command_context(self) -> bool: + """Return whether the current input is composing a slash command.""" + owner = self._chat_input_owner + if owner is not None and owner.mode == "command": + return True + return self.text.startswith("/") + def _should_start_paste_burst(self, char: str) -> bool: """Return whether a keypress should start paste-burst buffering. Restricting to quote-prefixed input at an empty cursor reduces false - positives for normal typing and slash-command entry. + positives for normal typing and slash-command entry. General multi-line + pastes are handled by the Enter-suppression window instead, which keeps + their newlines from submitting without rerouting text insertion. """ if char not in _PASTE_BURST_START_CHARS: return False @@ -782,6 +859,23 @@ async def _on_key(self, event: events.Key) -> None: event.stop() return + # Track rapid keystroke runs so a terminal replaying a paste as key + # events (no bracketed paste) arms the Enter-suppression window, keeping + # the paste's newlines from submitting mid-stream. `enter` is exempt so + # newlines within a paste don't break the run. + if event.is_printable and event.character is not None: + self._paste_burst_last_suppressed_enter_time = None + self._note_paste_burst_keystroke(now) + if ( + self._paste_burst_run >= _PASTE_BURST_MIN_CHARS + and not self._in_slash_command_context() + ): + self._paste_burst_window_until = ( + now + _PASTE_ENTER_SUPPRESS_WINDOW_SECONDS + ) + elif event.key != "enter": + self._reset_paste_burst_run() + # Some terminals (e.g. VSCode built-in) send a literal backslash # followed by enter for shift+enter. When enter arrives shortly # after a backslash, delete the backslash and insert a newline. @@ -837,10 +931,21 @@ async def _on_key(self, event: events.Key) -> None: event.prevent_default() return - # Plain Enter submits + # Plain Enter submits, unless a recent keystroke burst suggests this + # newline is part of a paste replayed as key events; then insert a + # newline and keep the window alive so the rest of the paste stays + # grouped instead of submitting mid-stream. if event.key == "enter": event.prevent_default() event.stop() + if self._enter_inserts_newline_during_burst(now): + self._paste_burst_window_until = ( + now + _PASTE_ENTER_SUPPRESS_WINDOW_SECONDS + ) + self._paste_burst_last_suppressed_enter_time = now + self.action_insert_newline() + return + self._paste_burst_last_suppressed_enter_time = None if ( self._chat_input_owner is not None and self._chat_input_owner._handle_stale_slash_enter() @@ -946,6 +1051,8 @@ def set_text_from_history(self, text: str, *, cursor_at_end: bool = True) -> Non """ self._paste_burst_buffer = "" self._paste_burst_last_char_time = None + self._paste_burst_window_until = None + self._reset_paste_burst_run() self._cancel_paste_burst_timer() self._backslash_pending_time = None self._skip_history_change_events += 1 @@ -969,6 +1076,8 @@ def clear_text(self) -> None: self._skip_history_change_events += 1 self._paste_burst_buffer = "" self._paste_burst_last_char_time = None + self._paste_burst_window_until = None + self._reset_paste_burst_run() self._cancel_paste_burst_timer() self._backslash_pending_time = None self.text = "" diff --git a/libs/code/tests/unit_tests/test_chat_input.py b/libs/code/tests/unit_tests/test_chat_input.py index 9db30356fef..c7b6f4574cf 100644 --- a/libs/code/tests/unit_tests/test_chat_input.py +++ b/libs/code/tests/unit_tests/test_chat_input.py @@ -3126,3 +3126,172 @@ async def test_preserves_widget_focus(self) -> None: await pilot.pause() assert chat._text_area.has_focus is True + + +class TestPasteBurstEnterSuppression: + """Multi-line pastes replayed as key events must not submit mid-stream. + + Terminals without bracketed paste deliver a paste as rapid `Char`/`Enter` + key events. A short run of fast keystrokes arms a suppression window so the + embedded `enter` events insert newlines instead of submitting. + """ + + async def test_rapid_burst_with_newline_does_not_submit( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + """A fast keystroke run followed by enter inserts a newline.""" + # Widen the burst gap so wall-clock delays between pilot.press calls on + # slow CI runners still register as a single rapid burst. + monkeypatch.setattr(chat_input_module, "_PASTE_BURST_CHAR_GAP_SECONDS", 60.0) + monkeypatch.setattr( + chat_input_module, "_PASTE_ENTER_SUPPRESS_WINDOW_SECONDS", 60.0 + ) + + app = _RecordingApp() + async with app.run_test() as pilot: + chat = app.query_one(ChatInput) + ta = chat._text_area + assert ta is not None + + for char in "hello": + await pilot.press(char) + await pilot.press("enter") + await pilot.press("w") + await pilot.pause() + + assert len(app.submitted) == 0 + assert "\n" in ta.text + + async def test_slow_typing_then_enter_submits( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + """Deliberate typing (no burst) keeps enter as submit.""" + # Force every inter-key gap to exceed the burst threshold. + monkeypatch.setattr(chat_input_module, "_PASTE_BURST_CHAR_GAP_SECONDS", 0.0) + + app = _RecordingApp() + async with app.run_test() as pilot: + chat = app.query_one(ChatInput) + ta = chat._text_area + assert ta is not None + + for char in "hello": + await pilot.press(char) + await pilot.press("enter") + await pilot.pause() + + assert len(app.submitted) == 1 + assert app.submitted[0].value == "hello" + + async def test_single_line_burst_then_manual_enter_submits( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + """Single-line paste followed by manual enter still submits.""" + monkeypatch.setattr(chat_input_module, "_PASTE_BURST_CHAR_GAP_SECONDS", 0.03) + monkeypatch.setattr( + chat_input_module, "_PASTE_ENTER_SUPPRESS_WINDOW_SECONDS", 0.12 + ) + + app = _RecordingApp() + async with app.run_test() as pilot: + chat = app.query_one(ChatInput) + ta = chat._text_area + assert ta is not None + + ta.text = "abc" + now = chat_input_module.time.monotonic() + ta._paste_burst_last_key_time = ( + now - chat_input_module._PASTE_BURST_CHAR_GAP_SECONDS - 0.01 + ) + ta._paste_burst_window_until = ( + now + chat_input_module._PASTE_ENTER_SUPPRESS_WINDOW_SECONDS + ) + + await ta._on_key(events.Key("enter", None)) + await pilot.pause() + + assert len(app.submitted) == 1 + assert app.submitted[0].value == "abc" + assert "\n" not in ta.text + + async def test_suppressed_enter_rearms_window( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + """A suppressed enter extends the window so trailing lines stay grouped.""" + monkeypatch.setattr(chat_input_module, "_PASTE_BURST_CHAR_GAP_SECONDS", 0.03) + monkeypatch.setattr( + chat_input_module, "_PASTE_ENTER_SUPPRESS_WINDOW_SECONDS", 0.12 + ) + + app = _RecordingApp() + async with app.run_test() as pilot: + chat = app.query_one(ChatInput) + ta = chat._text_area + assert ta is not None + + ta.text = "abc" + now = chat_input_module.time.monotonic() + # Fresh keystroke within the char gap and an open (but nearly + # closed) window: this enter belongs to a replayed paste. + ta._paste_burst_last_key_time = now + original_until = now + 0.01 + ta._paste_burst_window_until = original_until + + await ta._on_key(events.Key("enter", None)) + await pilot.pause() + + assert len(app.submitted) == 0 + assert "\n" in ta.text + assert ta._paste_burst_window_until is not None + assert ta._paste_burst_window_until > original_until + + async def test_blank_line_paste_keeps_consecutive_enter_grouped( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + """A delayed second enter in a blank-line paste does not submit.""" + monkeypatch.setattr(chat_input_module, "_PASTE_BURST_CHAR_GAP_SECONDS", 0.03) + monkeypatch.setattr( + chat_input_module, "_PASTE_ENTER_SUPPRESS_WINDOW_SECONDS", 60.0 + ) + + app = _RecordingApp() + async with app.run_test() as pilot: + chat = app.query_one(ChatInput) + ta = chat._text_area + assert ta is not None + + ta.text = "abc" + ta.move_cursor((0, len(ta.text))) + now = chat_input_module.time.monotonic() + ta._paste_burst_last_key_time = now + ta._paste_burst_window_until = now + 60.0 + + await ta._on_key(events.Key("enter", None)) + await pilot.pause() + assert ta.text == "abc\n" + + ta._paste_burst_last_key_time = ( + chat_input_module.time.monotonic() + - chat_input_module._PASTE_BURST_CHAR_GAP_SECONDS + - 0.01 + ) + await ta._on_key(events.Key("enter", None)) + await pilot.pause() + + assert len(app.submitted) == 0 + assert ta.text == "abc\n\n" + + async def test_slash_command_enter_still_submits_during_burst(self) -> None: + """Slash-command context keeps enter dispatching even after a burst.""" + app = _RecordingApp() + async with app.run_test() as pilot: + chat = app.query_one(ChatInput) + ta = chat._text_area + assert ta is not None + + for char in "/help": + await pilot.press(char) + await pilot.press("enter") + await pilot.pause() + + assert len(app.submitted) == 1