Skip to content

fix(mecatui): make the newline chord reachable and the hint honest on every terminal - #1550

Merged
aponcedeleonch merged 5 commits into
mainfrom
fix/1500-newline-chord-discoverability
Sep 16, 2026
Merged

aponcedeleonch merged 5 commits into
mainfrom
fix/1500-newline-chord-discoverability

Conversation

@aponcedeleonch

Copy link
Copy Markdown
Member

Fixes #1500.

The constraint this is shaped around

enter is a single carriage-return byte in legacy terminal encoding, with no room for a modifier bit. A terminal without the Kitty keyboard protocol or xterm's modifyOtherKeys therefore cannot send shift+enter at all: the keypress is not merely unbound, it is invisible, so nothing arrives for the app to respond to or to warn about. ctrl+enter is in exactly the same position. Only ctrl+j (a plain LF byte) and alt+enter (an ESC prefix) survive.

That rules out two shapes the issue considered. There is no runtime hint for a dead keypress, and a purely in-app fix cannot make shift+enter work where the terminal cannot encode it.

What this changes

1. Newline binds four chords (cmd/mecatui/ui/keys.go): shift+enter, ctrl+j, ctrl+enter, alt+enter. Bubble Tea requests both protocols on every frame and already decodes all four cleanly, so ctrl+enter and alt+enter were arriving at the prompt and being dropped on the floor.

alt+enter is the one that adds reach rather than convenience. It is what ESC CR decodes to, which is what "Use Option as Meta Key" sends on macOS Terminal.app, and what a terminal-side shift+enter rebind produces. So a terminal that cannot encode shift+enter natively can now be configured to deliver it, which is the only way shift+enter can be made to work there.

2. The prompt hint corrects itself (cmd/mecatui/ui/update.go, model.go). It names shift+enter and falls back to the binding's first legacy-safe chord once the terminal has proved it cannot deliver one. Two signals settle it, through one function so they cannot drift:

  • an explicit KeyboardEnhancementsMsg reporting no enhancements settles it immediately;
  • silence settles it at a deadline, because silence is the only negative signal a terminal gives.

The default is optimistic deliberately. Defaulting to the fallback would have changed what every capable-terminal user reads at startup, and capable terminals are now the overwhelming majority. The chord comes from the live binding, so a --keymap rebind flows through rather than the hint growing a second hardcoded chord.

Composition arms the deadline only when stdout is a real TTY, mirroring ThemeAutoDetect: there is nothing to answer on redirected output, and Init's existing contract (tests assert it returns the startup command) stays intact.

3. inputRenderKey gains a placeholder slot (cmd/mecatui/ui/render.go, view.go). Its validity key documented the placeholder as "fixed per process", which change 2 makes false. See "How this was caught" below.

Verification

Byte sequences injected into bin/mecatui --mock running in a real pty, which is the layer the bug lives at:

Injected What it is Result
ESC[13;2u shift+enter, Kitty newline
ESC[27;2;13~ shift+enter, modifyOtherKeys newline
0x0A ctrl+j newline
ESC[13;5u ctrl+enter, Kitty newline (was silently dropped)
ESC CR alt+enter newline (was silently dropped)
0x0D plain enter submits

New tests in cmd/mecatui/ui/newline_hint_test.go cover the chord classifier, the hint rule under --keymap rebinds, both settle signals, a late reply that loses the race with the deadline, the Init arming and its TTY gate, all four chords through the real dispatch path, and that a bare enter still submits. Each was mutation-checked: reverting the corresponding production change fails it.

task lint, task docs, and task site:build pass, as does task test except for pre-existing failures in cmd/mecatui, cmd/mecatui/embed, internal/app, and deploy/helm/mecak8s (provider credentials, port binding, helm). I confirmed the first two by reverting to a clean tree; this diff touches no file in internal/app or deploy/helm/mecak8s, and neither can import a cmd/ main, so their working tree is already HEAD. go run ./cmd/mecademo still prints a full offline session.

Goldens: two files, one line each, the help overlay's newline row picking up the new chords.

How the render-cache bug was caught

Worth flagging because it argues for the pty test rather than the unit tests. Every model-level test passed while the real binary was still wrong: the corrected chord reached the model, but renderInput's memoized output kept the stale line, so the hint only flipped after an unrelated keystroke happened to re-key the cache. That is precisely when the user no longer needs it. The correction arrives on a timer with no other model change behind it, which is the one case the "re-keyed in the same reducer step" argument above inputRenderKey did not cover. TestCorrectedHintIsActuallyPainted asserts the rendered region rather than the model field.

Residuals and scope

  • tmux answers the capability query on its own behalf, so with its default extended-keys off the hint can name shift+enter while tmux collapses it to a bare enter. I measured this: a real S-Enter submitted the prompt. Documented, with the two set lines that fix it. mecatui cannot detect it, since tmux's answer is the only one it gets.
  • The positive path is unit-tested, not pty-verified. A detached tmux pane has no client to answer the query, and this sandbox has no controlling tty, so I could not observe a confirmed-capable terminal end to end. Worth a reviewer pressing shift+enter in their own terminal.
  • The help overlay still leads with shift+enter in its key column, listing the rest as "(also …)". It shows every chord on one line, so it is honest; making it adapt too means rebuilding the renderer's cached helpKeys on the capability signal, which is a wider change than this issue needs.
  • No /terminal-setup equivalent. Documented the per-terminal config instead. A command that writes third-party app configs needs backups, per-app detection, and its own issue.

🤖 Generated with Claude Code

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Panel review follow-up: two independently-verified test-coverage gaps and one doc-wording nit surfaced while cross-checking a second review pass. No blockers — these are additive.

Comment thread cmd/mecatui/main.go Resolved
Comment thread cmd/mecatui/ui/newline_hint_test.go Resolved
Comment thread user-docs/mecatui/keybindings.md Resolved
JAORMX
JAORMX previously requested changes Sep 15, 2026

@JAORMX JAORMX left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed fcff829 against 34d905f in a separate detached worktree, with independent Spec, Standards, Test adequacy, UX, Architecture, Reuse, and Duplication reviewers. The test/reuse reviewers initially encountered provider failures; both completed after targeted fresh retries. No local tests or linters ran. Current CI is green; live e2e is skipped.

The default-key path looks good: four newline chords share dispatch, plain Enter remains submit, positive/negative/late capability replies update state coherently, and the placeholder is correctly included in the render-cache key. The rendered-hint regression test is particularly useful.

One requested fix before approval: the new fallback selector treats every non-Enter chord as legacy-safe. An accepted custom binding can therefore continue advertising an unencodable chord even when a working ctrl+j fallback is configured. This is a concrete gap in the advertised live-keymap fallback behavior, not a request to change the deliberate optimistic startup policy.

Panel axes:

  • Spec: default behavior and main/recovery paths satisfy #1500; only stale two-chord dispatch comments were noted as advisory.
  • Standards: non-blocking reference synchronization: the linked exhaustive developer key tables still list the old two defaults.
  • Test adequacy: missing custom modifier-chord boundary case, independently confirming the fallback gap. Existing reducer, timer, render, and dispatch tests have useful coverage.
  • Domain: the custom-chord classification issue is blocking; the Kitty-vs-modifyOtherKeys wording and inverted boolean parameter name are non-blocking. No actionable reuse or duplication findings.

I checked the upstream Kitty protocol and the pinned Bubble Tea/Ultraviolet input code. A missing Kitty reply is not proof that modifyOtherKeys is unavailable. The conservative ctrl+j fallback itself remains safe; this is a wording/guarantee issue, not a demand for another protocol probe. The documented tmux residual, static help overlay, and deferred terminal-config writer do not block this review.

PANEL: ship_blockers=1 important=3 advisory=2 reviewer_failures=0

Comment thread cmd/mecatui/ui/keys.go Resolved
Comment thread user-docs/mecatui/keybindings.md Resolved
Comment thread cmd/mecatui/ui/model.go Resolved
Comment thread user-docs/mecatui/keybindings.md Resolved
@aponcedeleonch

Copy link
Copy Markdown
Member Author

Addressed the review in two commits.

1015376 — the blocking finding. chordNeedsKeyDisambiguation is now chordSurvivesLegacyEncoding, an allowlist instead of a denylist: a chord qualifies as a fallback only when a legacy terminal carries it unambiguously (a bare key, ctrl+<letter> minus h/i/m, or either behind a single alt). --keymap Newline=ctrl+shift+x,ctrl+j now selects ctrl+j. Mutation-checked by restoring the old denylist in place: six classifier cases and both new selector cases fail.

The same commit drops the "proved it cannot deliver" framing everywhere it appeared. You were right that a missing Kitty reply is not evidence about modifyOtherKeys, so the comments, the placeholder rationale and the user docs now say the capability is unconfirmed and ctrl+j is a chord that works, not that shift+enter is broken. No extra probing. promptPlaceholder's boolean is legacyOnly now.

d35a7e1 — the coverage and reference gaps. resolveKeyboardProbe extracts the TTY gate with a test, mirroring resolveThemeAutoDetect; docs/tui.md's two key tables carry all four chords; two stale dispatch comments in update.go fixed.

Also added TestRemappedNewlineChordInsertsNewline, which drives a rebound chord through Model.Update via Deps.KeyOverrides and asserts the replaced default went inert, closing the gap between what the hint advertises under a rebind and what dispatch accepts.

One residual, flagged in-thread: the resolveKeyboardProbe test pins the gate's rule, not the call site, exactly as resolveThemeAutoDetect's does. Closing that needs a seam over the ~40-field ui.Deps literal in runWithOptions, which felt out of scope here. Happy to do it if you disagree.

task lint, task docs, task site:build and go run ./cmd/mecademo are green; ./cmd/mecatui/ui passes in full, and ./cmd/mecatui has only the pre-existing TestLocalProviderCheckRuns credential failure.

aponcedeleonch and others added 5 commits September 15, 2026 17:02
Enter's legacy encoding is a bare CR with no room for a modifier, so a
terminal without the Kitty keyboard protocol or modifyOtherKeys cannot
send shift+enter at all. Bubble Tea requests both protocols on every
frame and already decodes ctrl+enter and alt+enter cleanly, so both
chords were arriving at the prompt and being dropped.

alt+enter is the one that adds reach rather than convenience: it is what
ESC CR decodes to, which is what "Use Option as Meta Key" sends on macOS
Terminal.app and what a terminal-side shift+enter rebind produces. A
terminal that cannot encode shift+enter natively can now be configured to
deliver it.

Verified end to end by injecting the real byte sequences into mecatui
running in a pty: ESC[13;2u, ESC[27;2;13~, ESC[13;5u, ESC CR, and 0x0A all
insert a newline; a bare CR still submits.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The prompt placeholder advertised shift+enter unconditionally, including
on terminals that cannot encode it, where the key does nothing or submits.
The placeholder was also built once in New, before the terminal has
answered any capability query, so it could not adapt even in principle.

The hint now names shift+enter and corrects itself to the binding's first
legacy-safe chord once the terminal has PROVED it cannot deliver one: an
explicit reply reporting no enhancements settles it immediately, and
silence settles it at a deadline, since silence is the only negative
signal a terminal gives. The optimistic default keeps capable terminals,
which are the overwhelming majority, reading the chord they want.

Both signals settle through one function, so the reply path and the
deadline path cannot drift. The chord comes from the live binding, so a
--keymap rebind flows through instead of the hint growing a second
hardcoded chord. Composition arms the deadline only when stdout is a real
TTY, mirroring ThemeAutoDetect: there is nothing to answer on redirected
output, and Init's existing contract stays intact.

inputRenderKey gains a placeholder slot. Its validity key documented the
placeholder as fixed per process, which this change makes false: the
correction arrives on a timer with no other model change behind it, so
without the slot the corrected hint sat in the model unpainted until
unrelated input re-keyed the cache. Caught by running the real binary in
a pty, where the hint only flipped after a keystroke; the model-level
tests all passed.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Keybindings owns the treatment: which chords a terminal can deliver and
why, what the prompt hint means, how to configure a terminal so
shift+enter reaches mecatui, and the tmux caveat (tmux answers the
capability query on its own behalf, so the hint can name shift+enter
while tmux collapses it).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…encode

The fallback selector answered "legacy-safe" for every chord that was not a
modified Enter, which is too narrow a rule. A legacy terminal encodes
ctrl+shift+x as the same control byte as ctrl+x, so `--keymap
Newline=ctrl+shift+x,ctrl+j` kept advertising ctrl+shift+x after the deadline
and skipped the working ctrl+j — the exact broken-hint problem this path exists
to fix, preserved for a supported custom binding.

Replace the denylist with an allowlist, chordSurvivesLegacyEncoding: a chord
qualifies only when a legacy terminal carries it unambiguously — a bare key,
ctrl+<letter> minus h/i/m (whose control bytes ARE backspace/tab/CR, so they
arrive as those keys), or either behind a single alt (ESC prefix). Everything
else, including the ctrl+shift and shift+letter collisions, is disqualified.
The cost of being wrong is asymmetric: a fallback that cannot be delivered is
worse than declining to offer one.

Also stop calling an unanswered probe a proof. The reply consumed here is the
Kitty protocol's `CSI ? flags u` response; the pinned Ultraviolet decoder
reports xterm's modifyOtherKeys separately, as an event this reducer does not
read. A modifyOtherKeys-capable terminal can therefore deliver shift+enter and
still let the deadline pass. The fallback stays (it is usable, and the
asymmetry above still holds), but the comments, the placeholder rationale and
the user-facing docs now say "unconfirmed", not "cannot deliver". promptPlaceholder's
boolean is renamed legacyOnly to match the selector's vocabulary.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…key tables

The `ProbeKeyboardCapability: stdoutIsTTY` wiring had no test: reverting it to a
hardcoded true, arming the deadline on redirected stdout, failed nothing. Fold
the rule into resolveKeyboardProbe and pin it, mirroring the resolveThemeAutoDetect
seam next to it. (Like that sibling, this covers the gate's rule rather than the
call site — main.go's deps literal has no test seam of its own.)

docs/tui.md's two key tables still listed Newline as shift+enter/ctrl+J, which
the user guide links to as the exhaustive reference. Both now carry all four
chords and the rule that decides which one the hint advertises.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@aponcedeleonch
aponcedeleonch force-pushed the fix/1500-newline-chord-discoverability branch from d35a7e1 to e05b546 Compare September 15, 2026 14:17

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Panel review (Spec/Standards/Test adequacy/Domain: software-architect, library-reuse-reviewer, code-duplication-reviewer) plus cross-check against a second independent review found no blockers on the current branch. All findings from both review rounds — the legacy-safe-chord classification bug, the TTY-gate test gap, and the remapped-chord dispatch test gap — are addressed in the latest commits (1eb0c252a, e05b54690). go test ./cmd/mecatui/... is green.

@aponcedeleonch
aponcedeleonch dismissed JAORMX’s stale review September 16, 2026 07:12

Reviews is attended. Dismissing since Jakub already re-reviewed

@aponcedeleonch
aponcedeleonch merged commit 389d18d into main Sep 16, 2026
35 checks passed
@aponcedeleonch
aponcedeleonch deleted the fix/1500-newline-chord-discoverability branch September 16, 2026 07:12
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Sep 21, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mecatui advertises shift+enter for newline on terminals that cannot send it

3 participants