Skip to content

feat(chat): Escape stops the running turn from the composer - #23463

Closed
RohanK6 wants to merge 2 commits into
mainfrom
fix/22315-escape-stops-turn-samerepo
Closed

RohanK6 wants to merge 2 commits into
mainfrom
fix/22315-escape-stops-turn-samerepo

Conversation

@RohanK6

@RohanK6 RohanK6 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #22315

Problem / Motivation

Goal: Pressing Escape in the focused prompt box stops the running turn, matching the CLI's cancel-streaming binding.

Enhancement: the dashboard had only the Stop button and no keyboard way to stop a turn; the CLI binds Esc to cancel the streaming turn.

Why it matters

Stopping a runaway or mis-aimed turn is a frequent, time-sensitive action. Reaching for the mouse to hit Stop is slower than the Escape every CLI user already has muscle memory for, and keyboard users had no equivalent at all.

Not a goal

What changed (motivation → approach → change)

  • Goal: a keyboard way to stop the turn, scoped and precedence-safe.
  • Approach: bind a bare Escape inside the composer's own key handling (not a global shortcut), behind one precedence gate that yields to everything that already owns Escape in the composer.
  • Gate: useComposerStopKey (chat-input/stopKey.ts) returns a callback (event, { native, composing }) => boolean. It claims the key only when a turn is running on this slot and no picker, dictation, approval, approval nudge, IME composition, voice read-back or other Escape owner has it, then runs the same onStop protocol the Stop button runs (first press soft-cancels, a deliberate second press after the arm window force-kills). A held (auto-repeat) Escape is swallowed and never escalates.
  • Both editors call the gate, so the binding works whichever composer is active:
stop-speaking (useKeyboardShortcuts, capture phase)
  speech playing? -> noteEscapeStoppedSpeech(e), voice-stop
        │ same keydown continues
        v
textarea keydown (keyboard.ts)          ─┐
  composing = ime.isComposing(e)          │
                                          ├─> useComposerStopKey gate ─> onStop()  (same as Stop button)
Lexical KEY_ESCAPE_COMMAND               │      yields to: IME │ escapeStoppedSpeech(e) │ picker │
  (LexicalComposerInput, "Style           │                 dictation │ approval/nudge │
   Markdown While Typing" on)            ─┘                 anotherEscapeOwnerIsOpen()
  composing = editor.isComposing() || latch || native flags

Each editor supplies its own composition verdict from its own IME latch, so an Escape that only cancels a candidate window never stops the turn in either editor. The Lexical command ignores a modified Escape and is registered at COMMAND_PRIORITY_HIGH with the editor's other key commands.

  • Voice read-back: with auto-speak on, the reply is read aloud while the turn streams, and Escape is the stop-speaking shortcut. That capture-phase listener deliberately does not preventDefault, and stopping speech clears voicePlaying synchronously, so by the time the same keydown reaches the composer the store no longer shows speech playing. The listener now records the keydown it spent in a WeakSet (utils/speechStopKey.ts: noteEscapeStoppedSpeech / escapeStoppedSpeech), and the gate yields on a marked event. So the press that silences speech never also cancels the turn, and a second press for the next sentence can't force-kill it. The next unmarked Escape stops the turn as usual. This narrows Escape in the composer should stop the running turn, like the CLI's cancel-streaming binding #22315 item 4, which let read-aloud stop share the press: it still does, but the turn is no longer the second thing that press acts on.
  • Escape-owner precedence: a shared probe anotherEscapeOwnerIsOpen() (chat-input/escapeOwners.ts) is the single rule both the stop gate and voice.ts's dictation discard consult, so the two cannot drift. It yields to:
    • [role="dialog"] surfaces (command bar, snip overlay, confirms, and the non-modal SessionControlHost, which lets Escape propagate unstopped by design and relies on this probe), except one carrying data-escape-passthrough. ComputerUseLiveView, a read-only live-desktop panel open alongside a focused composer that never steals focus or claims Escape, carries that attribute, so Escape still stops the turn while it is up.
    • [data-escape-owner]: a non-dialog surface whose own document-level handler closes on Escape whether or not the event was already handled (it does not read defaultPrevented), so without the marker one Escape would both close it and stop the turn. Carried while open by the embedded file preview (MarkdownPanel via DetailPanel's opt-in escapeOwner prop), the message-search bar (SearchBar), and the paste-preview tooltip (PastePreviewTooltip via an opt-in escapeOwner, set by PasteHoverLayer and PastedChip).
  • Dictation discard behaviour changes too, on purpose. voice.ts used to yield only to [role="dialog"]; it now asks the same shared probe. So while the file preview, the message-search bar or the paste-preview tooltip is open, Escape closes that surface and keeps the dictation (before, one Escape closed the surface and also discarded the dictation). And while the read-only live-desktop view is open, Escape now discards a dictation (before, that view's role="dialog" made Escape do nothing to it).
  • Known cost of the probe: it checks the whole document, not just the focused surface. While a file preview is open in a side panel, Escape in the focused composer closes the preview first and leaves the turn running; the next Escape stops it. That is the price of one Escape never doing two things.
  • The notification banner is deliberately not an Escape owner. Its document handler already skips an Escape whose defaultPrevented is set, and both composer claimants (stop gate, dictation discard) preventDefault the Escape they take before it bubbles to document. So with the composer focused, Escape stops the turn or discards the dictation and the pending card stays; with nothing running, Escape dismisses the card as today. Marking the banner would have made any pending card silently disable both composer bindings.
  • IME ratchet left untouched: ImeEnterClaimRatchet.test.ts keeps its original predicate. The gate takes the caller's composition verdict as a parameter and folds it into its opening compound condition, so it never presents the bare composition-decline-return shape the ratchet pins.

Backwards compatibility

Compatible: Escape still does all of its existing composer jobs first (closes a picker, discards a recording or draining dictation, dismisses a pending approval or its nudge, cancels IME composition, closes a dialog). It only falls through to stop when none of those apply and a turn is running.

Removes nothing: the diff adds a key binding, two opt-in DOM markers, three optional props (DetailPanel.escapeOwner, PastePreviewTooltip.escapeOwner, LexicalComposerInput.onEscapeStop) and a per-event speech mark the stop-speaking listener writes without changing what it does; no key, prop, record or public function is removed or renamed.

Tests

  • composerStopKey.test.ts (new): 13 cases pinning the gate's precedence matrix. Fires when running and clear; yields for not-running, no onStop, IME composing (no preventDefault either), an Escape already marked as having stopped speech (and stops on the next unmarked one), picker open, recording, draining, approval, nudge, open role="dialog", and an open data-escape-owner surface; stops the turn when the open dialog opts out with data-escape-passthrough; swallows a held Escape without escalating.
  • ChatInput.lexicalKeys.test.tsx: three new cases on the real Lexical composer. A bare Escape while a turn runs calls onStop and a Shift+Escape does not; an Escape marked by the stop-speaking listener leaves the turn running; Escape does nothing with no turn running and on a keyCode-229 (post-composition) keydown. The first case fails with the onEscapeStop wiring removed from ChatInput (checked locally).
  • ChatInput.escapeStop.test.tsx (new): the same wiring on the mounted textarea composer. Bare Escape stops, Shift+Escape doesn't, nothing with no turn running, and a speech-marked Escape leaves the turn running.
  • useKeyboardShortcuts.test.tsx: two new cases. The stop-speaking listener marks the Escape it spent, and only that one; it marks nothing when no speech is playing.
  • The three speech-marked cases above fail with the gate's escapeStoppedSpeech check removed (checked locally).
  • NotificationBanner.test.tsx: new case. An Escape already defaultPrevented by a focused surface leaves the pending card in place.
  • ComputerUseLiveView.test.tsx: asserts the rendered dialog carries data-escape-passthrough.
  • ImeEnterClaimRatchet.test.ts: all 22 cases pass against the unmodified ratchet.
  • Proof: not provable -- frontend test (prove.py runs Python only)
  • Local gates: tsc -p tsconfig.app.json clean, eslint clean on every changed file, and the related vitest subset (ChatInput, Lexical, NotificationBanner, SearchBar, MarkdownPanel, DetailPanel, voice/Dictation, Paste, composer, ratchets, i18n, ComputerUse, keyboard shortcuts) 257 files / 4647 tests passing.

Manual verification

Behavior is covered at the gate and on the mounted Lexical composer. A full live check (press Escape mid-turn in the dashboard and confirm the turn stops, while a picker, dictation or approval still takes Escape first) needs a running gateway with an active turn (and auto-speak for the read-back case), which the sandbox this was built in cannot provision.

Screenshots / video

Why no screenshot: no rendered surface changes; the change binds a key to the existing Stop protocol, and the composer, Stop button and pickers look identical.

Related Issues

Fixes #22315. Deferred items 3 and 5 are tracked in #23462. Supersedes fork PR #22410.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title. Two here: the feature, and the review fix for the read-back Escape, kept separate rather than squashed because this branch is never force-pushed.
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable) -- N/A, no user docs cover composer key bindings
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributions are accepted under the project's Apache-2.0 license.

A bare Escape in the focused prompt box runs the same Stop protocol as the
Stop button (soft cancel, then force kill on a deliberate second press after
the arm window). A shared precedence gate (useComposerStopKey) yields to an
open picker, a recording or draining dictation, a pending approval or its
nudge, IME composition, and any other Escape owner found by the shared
anotherEscapeOwnerIsOpen() probe. A held Escape never escalates.

The gate is wired into both composer editors: the textarea's keydown and the
rich-text (Lexical) editor's KEY_ESCAPE_COMMAND, so the binding also works
with "Style Markdown While Typing" on. Each editor supplies its own IME
verdict from its own latch.

The notification banner is not an Escape owner: it already skips an Escape
whose defaultPrevented is set, and the composer's claimants preventDefault,
so a pending card no longer disables Escape-to-stop or dictation discard.

Refs #22315
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ✅ PASS

UX-level review of c83208d10ecaee6d2fcf3e1480055d794f3d106a — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

UX-Verdict: PASS

Nothing blocks: the diff only adds an Escape key binding and DOM markers that aren't visible. It adds or changes no visible control, string or state, so no screenshots are needed.

Notes

[UX-REVIEWED] c83208d

Description read

[DESCRIPTION-READ] f6be917816f51bf84a48397b50c9f002bf8812e8d0ddcb36bd64a023e6a8b63d

That sha256 names everything this verdict read: the PR description, plus the 3 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of c83208d10ecaee6d2fcf3e1480055d794f3d106a — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: this adds a key binding behind one shared precedence gate. It renders nothing new and removes nothing.

Notes

  • anotherEscapeOwnerIsOpen() checks the whole document, not just the focused surface. While a file preview is open in a side panel, [data-escape-owner] makes Escape do nothing to the turn even with the composer focused. That is the cost of one Escape not doing two things at once. If users report "Escape didn't stop it", look at this marker first.
  • Dictation discard now also yields to the new data-escape-owner surfaces (voice.ts). The description implies this but doesn't say it outright. It's a deliberate behaviour change, not just a refactor, so name it in the PR body.

[DESIGN-REVIEWED] c83208d

Description read

[DESCRIPTION-READ] 4fb8444ea6179f391b79d0faf234b7bf3818cd3e70405f587f2563a7126333ad

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of c83208d10ecaee6d2fcf3e1480055d794f3d106a — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

Nothing blocks. One rider changes dictation-discard behaviour beyond the stop binding.

Notes

  • Item 6 — rides along: in voice.ts, document.querySelector('[role="dialog"]') became anotherEscapeOwnerIsOpen(). Two things change for dictation discard. It now yields to the file preview, search bar and paste preview. It also now fires while the live-desktop view is open. The description presents this only as a way to keep the two gates in sync ("so the two cannot drift"). It does not say that discard behaviour changes. Subtraction: none needed if the change is intended. Otherwise keep the old [role="dialog"] selector in voice.ts.

What this change ships

Inventory (7 items) — 6 justified

Intent: stop a running turn from the keyboard by pressing Escape in the focused prompt box, as the CLI allows (#22315). ADDITION.

  1. A bare Escape in the textarea composer stops the running turn — justified
  2. The same binding in the rich-text (Lexical) composer — justified
  3. A held Escape is swallowed and never force-kills — justified
  4. An Escape that stopped voice read-back no longer also stops the turn — justified
  5. While the file preview, message search or paste preview is open, Escape closes it and leaves the turn running — justified
  6. Dictation discard now yields to those surfaces and fires with the live-desktop view open — rides along, partly undeclared
  7. The live-desktop view no longer blocks Escape in the composer — justified

The patch also carries the test_md_notebook_write_safety.py change. It is already on base (#23438), so it adds nothing.

[FIRST-PRINCIPLES-REVIEWED] c83208d

Description read

[DESCRIPTION-READ] 1ac3f246e4c0fa698dd587458db3b8988be0e9325dc73a4395e47e39a74511ec

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed c83208d10ecaee6d2fcf3e1480055d794f3d106a — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] c83208d

Verdict parsed from the review's SHA-scoped output markers for commit c83208d10ecaee6d2fcf3e1480055d794f3d106a.

False positive or not applicable? A repository writer can comment:
/ai-review override fable c83208d10ecaee6d2fcf3e1480055d794f3d106a: <one-sentence reason>

@RohanK6 RohanK6 closed this Oct 23, 2026
@RohanK6 RohanK6 reopened this Oct 23, 2026
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 23, 2026
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — 🔴 changes requested (blocking)

GPT 6.1 found at least one blocking issue that must be resolved before merging c83208d10ecaee6d2fcf3e1480055d794f3d106a. Adjudication upheld 1 of 1 blocking finding(s); those must be resolved before merging.

This comment is updated in place on each push.

BLOCKING -- website/src/components/chat-input/escapeOwners.ts:55 -- Unmarked Escape owners also cancel the running turn

document.querySelector('[role="dialog"]:not([data-escape-passthrough]), [data-escape-owner]') != null
Trigger: During a running turn, refocus the textarea composer with a redaction inspection card open, or hover a folded tool-call pill while the composer retains focus. These surfaces render as region/tooltip without either marker, so useComposerKeyDown → useComposerStopKey reaches onStop → requestStop.
Outcome: Escape cancels the turn instead of only dismissing the surface. The same event also reaches RedactionProvider’s document listener or useInstantTip’s window listener, which dismisses without checking defaultPrevented.
Anchor: defect
Fix: Mark the open redaction card and InstantTip tooltip with data-escape-owner so the composer yields before stopping the turn.

[BLOCK-MERGE] c83208d
[GPT-REVIEWED] c83208d

Adjudication (Opus 5.5) — is blocking on each finding proportionate?

F1 stays blocking. The harm is at the LOW-to-MEDIUM rung, and the fix is small.

F1 (MEDIUM/LOW):

  • What I confirmed in the code:

    • This PR adds the Escape-stops-turn binding: ChatInput.tsx:679 and keyboard.ts:158.
    • stopKey.ts:74 yields only when anotherEscapeOwnerIsOpen() finds [role="dialog"] or [data-escape-owner] (escapeOwners.ts:55).
    • The open redaction card closes on Escape through a document listener (RedactionCards.tsx:349), and the open InstantTip dismisses through a window listener (InstantTip.tsx:399). Neither listener checks defaultPrevented, and neither surface carries the marker.
    • So one Escape both dismisses the surface and soft-cancels the running turn. There is no fallback: the in-flight work is lost and the user has to re-prompt.
  • Cost of the real fix: add the existing data-escape-owner attribute while each surface is open. That is the same pattern this PR already uses in PastePreviewTooltip.tsx:99 and DetailPanel.tsx:247. It adds no new mechanism, so the fix is cheaper than the harm it prevents.

    [ADJUDICATION] c83208d total=1 uphold=1 downgrade=0
    UPHOLD F1 website/src/components/chat-input/escapeOwners.ts:55 reason=harm-warrants-remedy
    [GPT-ADJUDICATED] c83208d

False positive or not applicable? A repository writer can comment:
/ai-review override gpt c83208d10ecaee6d2fcf3e1480055d794f3d106a: <one-sentence reason>

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 23, 2026
With auto-speak on, the reply is read aloud while the turn streams, and
Escape is the stop-speaking shortcut. Its capture-phase listener does not
preventDefault, so the same keydown reached the composer's Escape-to-stop
gate and also soft-cancelled the turn; a second press to silence the next
sentence was read as a force kill.

Stopping speech clears voicePlaying synchronously, so the gate cannot ask
the store. The stop-speaking listener now marks the keydown it spent
(utils/speechStopKey.ts) and the gate yields on a marked event, in both the
textarea and the rich-text editor.

Refs #22315
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 23, 2026
@RohanK6 RohanK6 closed this Oct 23, 2026
@RohanK6 RohanK6 reopened this Oct 23, 2026
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 23, 2026
@RohanK6

RohanK6 commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #23496: the same change as one commit on top of current main, which carries the Windows test_proof_line_check fix (#23445). Taking that fix here would have meant a third commit, so the change moved to a fresh branch instead.

@RohanK6 RohanK6 closed this Oct 23, 2026
@bolichen97
bolichen97 deleted the fix/22315-escape-stops-turn-samerepo branch November 6, 2026 07:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

readiness: action required A blocking check or review needs attention

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Escape in the composer should stop the running turn, like the CLI's cancel-streaming binding

1 participant