Skip to content

fix(terminal): send LF for Pi Shift+Enter to stop the CSI-u decode freeze (#10203) - #12299

Closed
itisvincent wants to merge 2 commits into
stablyai:mainfrom
itisvincent:fix/terminal-pi-shift-enter-lf-no-freeze
Closed

itisvincent wants to merge 2 commits into
stablyai:mainfrom
itisvincent:fix/terminal-pi-shift-enter-lf-no-freeze

Conversation

@itisvincent

Copy link
Copy Markdown
Contributor

Summary

Fixes the freeze half of #10203 (and supersedes the csi-u encoding #11769 set for Pi). After #11769, Pi's Shift+Enter correctly inserts a newline instead of submitting, but it is visibly slower than Alt+J — a brief input freeze before the newline lands. That freeze is the cost of Pi decoding the CSI-u sequence (\x1b[13;2u, 6 bytes), versus Alt+J's fast 2-byte \x1bj binding.

Fix

Send a 1-byte LF (\n, 0x0A) for Pi's Shift+Enter instead of CSI-u. Pi binds Ctrl+J (0x0A) as a newline alias by default — so LF is a fast, reliable newline that:

Adds a new 'lf' value to windowsShiftEnterEncoding, switches Pi to it, and routes it in the Shift+Enter policy. Droid stays on csi-u; agents without an override keep the legacy Esc+CR fallback unchanged.

Why this rides on #11769

The encoding resolver still gates on routingTrusted, so 'lf' only applies when the pane's foreground is trusted as Pi. #11769's Windows foreground detection (PI_AGENT_NODE_ENTRYPOINT_RE) is what makes that trust hold — and it covers both the npm path and the pnpm virtual-store path (verified below).

Verification (from source)

  • pnpm typecheck — clean.
  • pnpm exec vitest run on the changed suites — 134/134 pass, including a new policy test that Shift+Enter sends '\n' when the encoding is 'lf', and an updated resolver test that Pi resolves to 'lf'.
  • pnpm exec oxlint + pnpm exec oxfmt --check on changed files — clean.
  • Confirmed the foreground detection recognizes the reporter's real pnpm Pi command line:
    D:\programming\SDK\node\node.exe E:\pnpm/global/5/.pnpm/@earendil-works+pi-coding-a_<hash>/node_modules/@earendil-works/pi-coding-agent/dist/cli.js ...
    → recognizeAgentProcessFromCommandLine returns { agent: 'pi', processName: 'pi' } (the regex matches node_modules/@earendil-works/pi-coding-agent/dist/cli.js at the path tail). So routingTrusted holds and 'lf' applies for pnpm-installed Pi, not just npm-installed.

Relationship to #11778

My earlier #11778 tried to fix the freeze via the IME deferred-newline path, but that was the wrong target — the freeze is CSI-u decode cost, not an IME delay. This PR is the correct fix; #11778 can be closed.

Cross-platform / compatibility notes

  • Windows-only in effect (the defer/encoding path is Windows-gated). macOS/Linux already send CSI-u where negotiated and are untouched.
  • Pi-specific (only pi uses 'lf'); other agents unchanged.
  • No UI/visual change. No perf risk — it replaces a 6-byte sequence with a 1-byte one.
  • The agent encoding lookup is still called once per Shift+Enter (refactored to a single windowsEncoding read), preserving the lazy-call-count behavior the existing tests assert.

Reviewer checklist

  • Cross-platform: Windows fix; macOS/Linux untouched
  • SSH/remote/local: renderer input path; unaffected by transport
  • Agent/integration: Pi only; Droid stays csi-u, others unchanged
  • Performance: 6 bytes -> 1 byte; single encoding read
  • Security: no new surface

Closes #10203

X handle: @itisvincent

…eeze (stablyai#10203)

Pi binds Ctrl+J (0x0A) as a newline alias by default, so a 1-byte LF is a
fast newline that stays distinct from Enter's CR (no submit, stablyai#9703). The
previous csi-u encoding made Shift+Enter correct (newline, not submit) but
Pi's CSI-u decode is visibly slower than the 2-byte Alt+J chord - surfacing
as the brief input freeze reported in stablyai#10203.

Add a 'lf' Windows Shift+Enter encoding that sends \n, and switch Pi to it.
The encoding resolver still gates on routingTrusted, so this rides on the
Pi Windows foreground detection added in stablyai#11769 (which recognizes both the
npm and pnpm-virtual-store cli.js entrypoints). Agents without the override
keep the legacy Esc+CR fallback unchanged; Droid stays on csi-u.

Verified from source: a test with the reporter's real pnpm command line
(E:\pnpm/global/5/.pnpm/@earendil-works+pi-coding-a_<hash>/.../dist/cli.js)
is recognized as 'pi' by the foreground detection, so routingTrusted holds
and the 'lf' encoding applies.
@coderabbitai

coderabbitai Bot commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds lf as a supported Windows Shift+Enter encoding. Pi now selects LF encoding. Encoding resolution preserves the launch agent type when trusted foreground routing data is unavailable. The shortcut policy caches the encoding and sends a literal LF for lf. Tests cover agent resolution and LF input.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the Pi Shift+Enter LF fix and the CSI-u decode freeze it addresses.
Description check ✅ Passed The description explains the user-visible change, testing, platform scope, compatibility, performance, and security impact in sufficient detail.
Linked Issues check ✅ Passed The changes address #10203 by sending LF for trusted Pi panes on Windows while preserving newline behavior and leaving other agents unchanged.
Out of Scope Changes check ✅ Passed The modified resolver, policy, configuration, and regression tests are directly related to the Pi Shift+Enter freeze fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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.

…-scan misses (stablyai#9703 stablyai#10203)

The encoding (csi-u/lf) only applied while routingTrusted held, so a single
Shift+Enter newline was lost whenever the Windows foreground scan transiently
missed the Pi node process during tool-subprocess churn - the 'sometimes not
working' intermittency. Fall back to the pane's launch agent when live trust is
missing, so a Pi-launched pane keeps its 'lf' encoding across a transient scan
miss. shellForeground still fails closed when the agent has exited to a shell.

This reverses the prior fail-closed-on-launch-only behavior (the resolver
previously returned alt-enter for launch-only and while awaiting fresh trust);
the trade-off is accepted because a single Shift+Enter newline byte routed to a
stale launch agent is low-harm, whereas losing the newline every tool run is the
reported bug.

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

🧹 Nitpick comments (1)
src/renderer/src/components/terminal-pane/terminal-windows-shift-enter.test.ts (1)

18-24: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover Pi with an untrusted foreground signal.

The Pi test covers trusted Pi data and launch-only resolution. It does not prove that an untrusted foreground agent cannot override the Pi launch agent. Add a case with foreground.agent: 'droid', routingTrusted: false, and launchAgentType: 'pi'. Assert 'lf'.

Proposed regression case
     expect(resolveWindowsShiftEnterEncoding({ launchAgentType: 'pi' })).toBe('lf')
+    expect(
+      resolveWindowsShiftEnterEncoding({
+        foreground: { agent: 'droid', routingTrusted: false, shellForeground: false },
+        launchAgentType: 'pi'
+      })
+    ).toBe('lf')

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5b91837f-514c-4bdc-a8d8-de9cc0c0fc70

📥 Commits

Reviewing files that changed from the base of the PR and between 55363a0 and 3f5d02a.

📒 Files selected for processing (2)
  • src/renderer/src/components/terminal-pane/terminal-windows-shift-enter.test.ts
  • src/renderer/src/components/terminal-pane/terminal-windows-shift-enter.ts

@itisvincent

Copy link
Copy Markdown
Contributor Author

Runtime diagnostic confirmed the root cause

Installed the diagnostic build on Windows (v1.4.167 + a console.warn in the Shift+Enter policy) and captured the actual encoding resolution at runtime:

[S+Enter] {"enc":"csi-u","host":true,"kitty":false}   → CSI-u → newline (with freeze)
[S+Enter] {"enc":"alt-enter","host":true,"kitty":false} → Esc+CR → submit
[S+Enter] {"enc":"csi-u","host":true,"kitty":false}

The encoding flickers between csi-u and alt-enter. When routingTrusted drops (the foreground scan transiently misses Pi during tool subprocess churn), the resolver returns alt-enter → Esc+CR → submit. When trust holds, it returns csi-u → newline (with the CSI-u decode freeze).

kitty is always false — the Kitty keyboard protocol path never helps; the config path is the only thing that works, and it flickers.

After applying this PR (LF + launch fallback)

[S+Enter] {"enc":"lf","host":true,"kitty":false}   → always LF → always a fast newline

The launch fallback makes the encoding always resolve to lf (the pane was launched as Pi), regardless of the transient scan miss. The LF encoding sends \n (1 byte, Pi binds as the Ctrl+J newline alias) instead of CSI-u — no submit, no freeze.

Remaining lag vs Alt+J

Confirmed: the remaining ~10ms lag vs Alt+J is Orca's sendInput dispatch overhead (JS policy → IPC → PTY), not Pi decode cost. Alt+J bypasses the policy entirely (xterm.js writes to the PTY natively). No [S+Enter DEFER] appeared — the IME deferred-newline path is NOT the cause. This architectural overhead is not fixable by changing the byte; it would need an xterm-level custom key handler (a separate, deeper PR).

#11778 (IME defer) — wrong fix, should be closed

The IME deferred-newline path was NOT firing (no [S+Enter DEFER] in the diagnostic). The freeze was CSI-u decode cost, not an IME delay. Closing #11778.

@itisvincent

Copy link
Copy Markdown
Contributor Author

New finding:
Orca only sends the good newline (LF) when it can see that Pi is the program running in your pane right now. When I answer you, I run
little helper programs (to read files, run commands). While those helpers are running, Orca sees the helper, not Pi — so it switches to
the default "alt-enter" (which submits). And after my answer finishes, Orca doesn't look back — it keeps thinking "not Pi" until you
click the pane again.

@itisvincent

Copy link
Copy Markdown
Contributor Author

Closing this PR — LF approach was wrong

After runtime diagnostic + analysis of Pi's parseKey (in @earendil-works/pi-tui/dist/keys.js):

  1. LF (\n) has the same Kitty-flag dependency as \x1b\r. Pi's parseKey checks _kittyProtocolActive for both \n and \x1b\r. When Kitty flag is OFF (after tool churn), both parse as enter → submit. So switching to LF didn't fix the submit — it just changed which byte submits.

  2. The launch-agent fallback was non-functional. agentLaunchConfigByPaneKey[paneKey] is ephemeral — cleared on session transitions. After Orca restart (restored panes), the fallback reads null. So Patch B never fired.

  3. CSI-u (\x1b[13;2u) is the ONLY unconditional byte. Pi's parseKey runs parseKittySequence(data) first and unconditionally — no Kitty-flag check. CSI-u → shift+enter → newline in every state (fresh, restored, post-churn). \n and \x1b\r both depend on the Kitty flag, which flips OFF on tool churn.

The correct fix is title-based fallback: when foreground detection fails (returns alt-enter), read the pane's OSC title (which Pi sets to ⠸ Pi and keeps stable across restore/tool-churn/typed-shell states), recover the agent type, and send CSI-u for CSI-u agents (Pi, Droid). Raw shells (no agent title) keep the safe alt-enter. Filing as a separate PR.

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.

[Bug]: Pi agent input freezes briefly on Shift+Enter newline (other CLI agents unaffected)

3 participants