Skip to content

fix(mobile): focus Kimi terminal input after touch - #11865

Merged
brennanb2025 merged 3 commits into
mainfrom
brennanb2025/kimi-ios-keyboard-focus-fix
Aug 1, 2026
Merged

brennanb2025 merged 3 commits into
mainfrom
brennanb2025/kimi-ios-keyboard-focus-fix

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

What changed

Kimi and other mouse-aware terminal TUIs consume a touch as terminal mouse input. Mobile now preserves those PTY mouse bytes and requests native live-input focus after the WebView finishes the touch gesture.

The focus request lives in a bounded lifecycle hook. It revalidates the active terminal, connection, client identity, and live-input mode before focusing; replaces earlier pending work; and clears on route blur, client replacement, disconnect, or unmount. The visible native keyboard control keeps its immediate focus behavior.

Links/files, selection dismissal, TUI mouse bytes, native chat, buffered input, reconnect gating, SSH/folder-workspace routing, and terminal hot paths are otherwise unchanged. The fix adds no polling or persistent listeners and keeps at most one replaceable timer per accepted terminal tap.

Root cause and user impact

The first implementation posted terminal-tap after the SGR click but called the hidden native TextInput.focus() synchronously from the WebView message callback. On a fresh route or host reconnect remount, WKWebView could still be completing the mouse-aware touchend/first-responder handoff, so the native focus request was lost even though SGR press/release bytes reached the PTY.

At the prior regressing head fe16936c2b8be9dd0aa7d7bfffee629a0946ab97, initial input could work after focus was already established, but navigation/remount and full host reconnect/remount reproduced mouse bytes with dropped typed text. The continuously re-armed SGR fixture exposed that cold-remount failure.

At final head eb963fc419b4ad7086769f62fa55da592624c1ef, touch still reaches the TUI first, then native focus occurs after the gesture. A review-discovered P2 follow-up also clears deferred focus on retained-route blur and ties cleanup to the current client lifecycle identity, preventing stale delayed focus after navigation or client replacement.

Mouse-aware external-pointer clicks keep sending only terminal mouse input. Plain external-pointer clicks retain their prior keyboard-focus behavior.

Review verdict

  • A fresh gpt-5.6-sol / xhigh reviewer found the retained-route blur cleanup gap described above; it was fixed and covered by tests.
  • A genuinely fresh same-model, same-thinking reviewer returned literal CLEAN for exact final head eb963fc419b4ad7086769f62fa55da592624c1ef (task task_78b4fdeb5a43, dispatch ctx_bdf09fba89ae).
  • Scope review found no correctness, accessibility, lifecycle, SSH/folder-workspace, external-pointer, or performance regression requiring another change.

Automated validation

  • 40 focused mobile tests passed across native-focus lifecycle, WebView tap routing, retained-route/client-replacement cleanup, external-pointer mouse semantics, scroll routing, and the live-input affordance.
  • Full mobile suite: 380 files, 2,840 passed, 3 skipped.
  • Mobile TypeScript passed.
  • Changed-file oxlint and oxfmt passed.
  • Commit hooks and diff hygiene passed.
  • Exact-head pnpm build:cli passed.
  • Exact-head pnpm build:electron-vite passed.
  • The initially flaky activity-portal-readiness-loop.react185.test.tsx passed locally 4/4; failed CI jobs were rerun and the final PR check rollup is green.

Native validation at the exact final head

Validated only through Orca's documented emulator surface on approved iPhone 50FD514C-96F6-446E-AC90-D07A24CAB3A0, using its stream at port 3100. The exact final-head runtime served on port 53901, and the dev client loaded the final-head Expo bundle from port 8128.

  • Initial mouse-aware input: the continuously re-armed MOCK_TUI SGR 1002/1006 fixture visibly received mouse press/release bytes and submitted livekimipass.
  • Full reconnect/remount: Terminal 1 visibly received SGR press/release bytes and submitted mouse-terminal1-final after exact-runtime reconnection and client remount.
  • Background/foreground: Home/background and foreground through the Orca app preserved the connected session and completed submission.
  • Navigation away/back: after leaving and reopening the session, Terminal 1 visibly submitted nav-final with fresh SGR press/release bytes.
  • Non-mouse comparison: Terminal 2 independently retained successful nonmouse-final and direct-input-final submissions without SGR mouse bytes.
  • Full client reload/remount: an exact-bundle reload returned through hosts, reconnected Host 41, and restored all three terminal sessions and output.
  • Stale-listener cleanup produced no native delayed-focus/navigation side effect; retained-route and client-replacement behavior is directly covered by the automated lifecycle tests.

Final-head evidence

Initial mouse-aware submission:

initial mouse-aware final-head submission

Mouse-aware submission preserved after background/foreground:

mouse-aware submission after background and foreground

Navigation-away/back submission after exact-bundle remount:

mouse-aware navigation and remount submission

Non-mouse comparison after remount:

non-mouse final-head comparison

Validation limitations

  • Actual Kimi remains unavailable because no kimi binary is installed. The closest shipped fixture is the continuously re-armed MOCK_TUI SGR 1002/1006 lane.
  • The terminal lane used hardware-keyboard input. The terminal software keyboard never visibly rendered, so visible summon/dismiss/reopen could not be assessed; native typing/submission is reported only where supported.
  • External-pointer injection is unsupported by the approved Orca emulator surface. Automated coverage verifies mouse-aware external pointers remain PTY-only and plain external-pointer clicks keep their prior focus behavior.
  • The required $electron skill is unavailable. Per repository instructions, no Computer Use, AppleScript, OS automation, or input injection was substituted.
  • No iPad, protected device, other simulator, third stream, raw simctl, raw serve-sim, or emulator kill command was used.
  • Screenshots and reports remain outside the repository and are not part of the diff.

Residual native risk is limited to actual Kimi-specific behavior, a visibly rendered software-keyboard lifecycle, and injectable external-pointer behavior that the approved surface could not exercise. This PR remains open and was not merged.

@coderabbitai

coderabbitai Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The session route now uses useTerminalLiveInputFocus for terminal input focus and tap handling. The hook manages immediate and deferred focus, active terminal handles, lifecycle resets, and timer cleanup. Surface-tap notifications now include explicit keyboard-focus intent. Mouse-tracking and ordinary touch taps have updated routing behavior with regression coverage.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 10.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the primary mobile terminal focus fix after touch input.
Description check ✅ Passed The description provides detailed change, screenshot, testing, review, and limitation information, but omits explicit Security Audit and Notes sections.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Greptile Summary

This PR fixes a remount regression where WKWebView's touchend/first-responder handoff would race the synchronous TextInput.focus() call, causing typed text to be dropped on fresh-route or reconnect scenarios in mouse-aware terminal TUIs (e.g. Kimi). The fix defers native focus by 50 ms and wraps the entire focus lifecycle in a new useTerminalLiveInputFocus hook with proper cancellation on navigation and reconnect.

  • New useTerminalLiveInputFocus hook: extracts focusLiveInput, handleTerminalTap, and resetLiveInputFocus from the session route; defers terminal-tap focus via scheduleTerminalLiveInputFocus; cancels pending work when lifecycleKey or lifecycleIdentity changes.
  • notifyTerminalSurfaceTap signature change: adds a focusKeyboard boolean parameter; touch taps pass true so terminal-tap fires even in mouse-tracking mode; external pointer clicks pass false to preserve existing pointer-only behaviour.

Confidence Score: 5/5

Safe to merge; the change is narrowly scoped to the terminal tap-focus path with no touch points on PTY I/O, session state, or non-terminal UI.

The new hook correctly defers native focus past the WKWebView first-responder handoff, guards every deferred callback against stale handle/session/connection state, and cancels pending work on both navigation blur and lifecycle key changes. The notifyTerminalSurfaceTap signature change is minimal and the focusKeyboard flag cleanly separates touch from pointer-click semantics. Tests cover all lifecycle scenarios.

Files Needing Attention: No files require special attention.

Important Files Changed

Filename Overview
mobile/src/terminal/use-terminal-live-input-focus.ts New hook encapsulating deferred native-input focus for terminal taps; uses contextRef pattern for stale-closure safety, lifecycle cleanup via useLayoutEffect, and handle revalidation before firing the deferred callback.
mobile/src/terminal/terminal-webview-url-tap.ts Adds focusKeyboard parameter to notifyTerminalSurfaceTap; touch taps now emit terminal-tap after mouse bytes, pointer clicks retain prior skip-tap-in-mouse-mode behaviour.
mobile/app/h/[hostId]/session/[worktreeId].tsx Replaces inline focusLiveInput/handleTerminalTap callbacks with useTerminalLiveInputFocus hook; adds useFocusEffect cleanup to cancel pending focus when a pushed screen covers this route.
mobile/src/terminal/use-terminal-live-input-focus.test.ts New comprehensive test file covering initial mount, remount, reconnect, stale-handle, and navigation-blur scenarios for the new hook; 8 test cases.

Reviews (3): Last reviewed commit: "fix(mobile): reset deferred terminal foc..." | Re-trigger Greptile

@coderabbitai coderabbitai Bot 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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
mobile/src/terminal/terminal-webview-url-tap.ts (1)

208-252: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Add missing pointer-click coverage for drag/any tracking modes.

isClickMouseTrackingMode(mode) already treats any non-none mode as click-tracking, including drag and any. Add a pointer-click test for drag/any alongside the existing vt200 case so no terminal-tap is posted in those active modes.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d804e558-117a-4f78-979d-ab0bf1a3c2da

📥 Commits

Reviewing files that changed from the base of the PR and between 377b580 and ced8df3.

📒 Files selected for processing (10)
  • mobile/app/h/[hostId]/session/[worktreeId].tsx
  • mobile/src/terminal/terminal-live-input-affordance.test.ts
  • mobile/src/terminal/terminal-webview-mouse-click-drag-injected.ts
  • mobile/src/terminal/terminal-webview-mouse-click.test.ts
  • mobile/src/terminal/terminal-webview-scroll-routing.test.ts
  • mobile/src/terminal/terminal-webview-tap-dispatch-injected.ts
  • mobile/src/terminal/terminal-webview-tap-routing.test.ts
  • mobile/src/terminal/terminal-webview-url-tap.ts
  • mobile/src/terminal/use-terminal-live-input-focus.test.ts
  • mobile/src/terminal/use-terminal-live-input-focus.ts

@brennanb2025
brennanb2025 merged commit c2e3d13 into main Aug 1, 2026
82 of 84 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
* fix(mobile): focus terminal input after TUI touch

* fix(mobile): defer terminal focus after WebView taps

* fix(mobile): reset deferred terminal focus on route blur
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.

1 participant