From 6a5699068384de6deeca02b51b35fc9a61784890 Mon Sep 17 00:00:00 2001 From: Mason Daugherty <61371264+mdrxy@users.noreply.github.com> Date: Wed, 10 Jun 2026 23:30:26 +0000 Subject: [PATCH 1/4] fix(code): treat multi-line key-event pastes as one input Terminals without bracketed paste replay a paste as rapid Char/Enter key events, so each embedded newline submitted separately. Arm an Enter-suppression window on rapid keystroke runs so in-paste newlines insert instead of submitting, mirroring Codex's PasteBurst. Slash-command context keeps Enter's submit/dispatch semantics. Co-authored-by: open-swe[bot] --- .../deepagents_code/widgets/chat_input.py | 100 +++++++++++++++++- libs/code/tests/unit_tests/test_chat_input.py | 71 +++++++++++++ 2 files changed, 166 insertions(+), 5 deletions(-) diff --git a/libs/code/deepagents_code/widgets/chat_input.py b/libs/code/deepagents_code/widgets/chat_input.py index 5498275d04b..6d25a7eb91d 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,18 @@ 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 + # 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 +673,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 +683,51 @@ 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 + + def _enter_inserts_newline_during_burst(self, now: float) -> bool: + """Return whether `enter` should insert a newline rather than submit. + + True while a burst is buffering or within the suppression window that + outlives it, except in slash-command context where `enter` must keep + its submit/dispatch semantics. + """ + if self._in_slash_command_context(): + return False + if self._paste_burst_buffer: + return True + until = self._paste_burst_window_until + return until is not None and now <= until + + 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 +843,22 @@ 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._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 +914,19 @@ 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.action_insert_newline() + return if ( self._chat_input_owner is not None and self._chat_input_owner._handle_stale_slash_enter() @@ -946,6 +1032,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 +1057,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..ffbc1734814 100644 --- a/libs/code/tests/unit_tests/test_chat_input.py +++ b/libs/code/tests/unit_tests/test_chat_input.py @@ -3126,3 +3126,74 @@ 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_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 From 3414274b8987946e568309951ec62b6f33ebf225 Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Wed, 10 Jun 2026 21:57:35 -0400 Subject: [PATCH 2/4] cr --- .../deepagents_code/widgets/chat_input.py | 3 ++ libs/code/tests/unit_tests/test_chat_input.py | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/libs/code/deepagents_code/widgets/chat_input.py b/libs/code/deepagents_code/widgets/chat_input.py index 6d25a7eb91d..043852a4ae5 100644 --- a/libs/code/deepagents_code/widgets/chat_input.py +++ b/libs/code/deepagents_code/widgets/chat_input.py @@ -711,6 +711,9 @@ def _enter_inserts_newline_during_burst(self, now: float) -> bool: return False if self._paste_burst_buffer: return True + last_key = self._paste_burst_last_key_time + if last_key is None or (now - last_key) > _PASTE_BURST_CHAR_GAP_SECONDS: + return False until = self._paste_burst_window_until return until is not None and now <= until diff --git a/libs/code/tests/unit_tests/test_chat_input.py b/libs/code/tests/unit_tests/test_chat_input.py index ffbc1734814..2eb025a0c34 100644 --- a/libs/code/tests/unit_tests/test_chat_input.py +++ b/libs/code/tests/unit_tests/test_chat_input.py @@ -3183,6 +3183,37 @@ async def test_slow_typing_then_enter_submits( 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_slash_command_enter_still_submits_during_burst(self) -> None: """Slash-command context keeps enter dispatching even after a burst.""" app = _RecordingApp() From 578462a79313e10624c0e8dea8271144c6e67003 Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Wed, 10 Jun 2026 22:02:15 -0400 Subject: [PATCH 3/4] cr --- .../deepagents_code/widgets/chat_input.py | 12 +++++-- libs/code/tests/unit_tests/test_chat_input.py | 31 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/libs/code/deepagents_code/widgets/chat_input.py b/libs/code/deepagents_code/widgets/chat_input.py index 043852a4ae5..9b7dbb40465 100644 --- a/libs/code/deepagents_code/widgets/chat_input.py +++ b/libs/code/deepagents_code/widgets/chat_input.py @@ -703,12 +703,18 @@ def _reset_paste_burst_run(self) -> None: def _enter_inserts_newline_during_burst(self, now: float) -> bool: """Return whether `enter` should insert a newline rather than submit. - True while a burst is buffering or within the suppression window that - outlives it, except in slash-command context where `enter` must keep - its submit/dispatch semantics. + True only when the preceding keystroke was itself part of a rapid run + (within `_PASTE_BURST_CHAR_GAP_SECONDS`) 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 last_key = self._paste_burst_last_key_time diff --git a/libs/code/tests/unit_tests/test_chat_input.py b/libs/code/tests/unit_tests/test_chat_input.py index 2eb025a0c34..0fa09ab8b6c 100644 --- a/libs/code/tests/unit_tests/test_chat_input.py +++ b/libs/code/tests/unit_tests/test_chat_input.py @@ -3214,6 +3214,37 @@ async def test_single_line_burst_then_manual_enter_submits( 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_slash_command_enter_still_submits_during_burst(self) -> None: """Slash-command context keeps enter dispatching even after a burst.""" app = _RecordingApp() From 669aeb5469795b9b23b52937aed1833f8f6db46b Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Wed, 10 Jun 2026 22:14:30 -0400 Subject: [PATCH 4/4] fix openswe --- .../deepagents_code/widgets/chat_input.py | 22 ++++++++---- libs/code/tests/unit_tests/test_chat_input.py | 36 +++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/libs/code/deepagents_code/widgets/chat_input.py b/libs/code/deepagents_code/widgets/chat_input.py index 9b7dbb40465..8ca9fb92de2 100644 --- a/libs/code/deepagents_code/widgets/chat_input.py +++ b/libs/code/deepagents_code/widgets/chat_input.py @@ -433,6 +433,7 @@ def __init__(self, **kwargs: Any) -> None: # 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 @@ -699,12 +700,13 @@ 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 only when the preceding keystroke was itself part of a rapid run - (within `_PASTE_BURST_CHAR_GAP_SECONDS`) and the suppression window is + 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 @@ -717,11 +719,16 @@ def _enter_inserts_newline_during_burst(self, now: float) -> bool: # today. It keeps the helper's contract self-contained. if self._paste_burst_buffer: return True - last_key = self._paste_burst_last_key_time - if last_key is None or (now - last_key) > _PASTE_BURST_CHAR_GAP_SECONDS: - return False until = self._paste_burst_window_until - return until is not None and now <= 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.""" @@ -857,6 +864,7 @@ async def _on_key(self, event: events.Key) -> None: # 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 @@ -934,8 +942,10 @@ async def _on_key(self, event: events.Key) -> None: 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() diff --git a/libs/code/tests/unit_tests/test_chat_input.py b/libs/code/tests/unit_tests/test_chat_input.py index 0fa09ab8b6c..c7b6f4574cf 100644 --- a/libs/code/tests/unit_tests/test_chat_input.py +++ b/libs/code/tests/unit_tests/test_chat_input.py @@ -3245,6 +3245,42 @@ async def test_suppressed_enter_rearms_window( 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()