Skip to content

perf(terminal): skip kitty scans for plain PTY output - #21643

Merged
nwparker merged 2 commits into
mainfrom
nwparker/perf-kitty-scan-09-19
Sep 19, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/perf-kitty-scan-09-19

Conversation

@nwparker

@nwparker nwparker commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​26 $\color{#cf222e}{\Huge{\mathbf{−}}}$​6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​20
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​6 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​5

ELI5

The kitty keyboard tracker is a small referee that watches terminal output for special keyboard messages. Before this change, it reopened its rulebook for every ordinary shell-output chunk, even when the chunk contained no escape marker. The tracker now checks for those markers first and skips the expensive parser when there is nothing to inspect. Special messages and split messages still use the existing parser.

What Changed

Every PTY output chunk still reaches TerminalKittyKeyboardModeTracker.scan(), but scanInternal() returns immediately when there is no pending partial sequence and the chunk contains neither ESC (\\x1b) nor C1 CSI (\\x9b). Escape-bearing chunks, replay scans, and pending split sequences follow the unchanged parser path. A focused test records that ordinary output leaves negotiated flags and their snapshot provenance unchanged.

The guard replaced a string concatenation (this.scanTail + data) that used to coerce a nullish chunk to a harmless string, so scanInternal() now normalizes the chunk once at the top and both the guard and the concatenation use it. That keeps the function total: two unchecked main-process call sites reach this tracker — orca-runtime-capture-provider-terminal-buffer.ts (scanReplay(snapshot.data)) and orca-runtime-on-pty-data.ts (.scan(data) on every PTY data event) — and both files are @ts-nocheck, so the declared non-optional data: string is not enforced at either.

The AgentTerminalPreview connect mock is now typed against the real preload signature, which turns a stale bare-string replay fixture into a compile error rather than a runtime throw.

For users, terminal output with no kitty protocol traffic spends much less renderer CPU in this bookkeeping path. Kitty keyboard negotiation, dense SGR output, ACK accounting, output ordering, snapshots, replay, and remote projection retain their existing behavior.

Why

The output path calls this tracker once per PTY chunk. A warmed Node 24 benchmark on macOS measured 100,000 scans:

Input Before After Change
16 KiB plain output 1,105 ms 58 ms ~19x faster
1-character plain output 29.0 ms 10.3 ms ~2.8x faster
Dense SGR output 276 ms 282 ms effectively unchanged

The guard is the smallest safe boundary because it does not alter the protocol parser or retain ordinary output. Checking the pending tail is required so a sequence split across PTY chunks is still completed.

Linked Issue

Fixes #21642

Visual Proof

N/A — this is a shared parser fast path with no visual or interaction layout change.

Testing

  • Automated tests added/updated, or explained why not below

  • I manually tested these changes locally

  • pnpm test src/shared/terminal-kitty-keyboard-mode-tracker.test.ts src/shared/terminal-kitty-keyboard-tail-retention.test.ts — 30 tests passed.

  • pnpm test src/renderer/src/components/dashboard-popout/AgentTerminalPreview.test.tsx — 23 tests passed.

  • pnpm tc passed.

  • pnpm run check:code-quality:changed passed.

  • Oxlint, Oxfmt, and git diff --check passed.

  • Benchmark compares the current tracker with the exact origin/main implementation using warmed Node 24 calls; dense SGR remains on the parser path.

Review

  • Invariant: every escape-bearing kitty sequence and every pending split sequence is parsed exactly as before; plain chunks do not mutate tracker state.
  • Failure source: regex and tail bookkeeping on every ordinary PTY chunk can add renderer CPU during large output bursts.
  • Oracle: the existing split-sequence and C1 tests plus the new plain-output state test; focused suite passed.
  • Reliability gate: no existing gate directly covers this kitty-mode scanner. Accepted gap: the shared parser tests prove byte-level behavior on macOS, while live Electron latency and Linux/Windows/WSL/SSH process runs remain CI/follow-up coverage.
  • Diagnostics: a future regression is visible in the focused tracker suite and the plain-versus-escape benchmark.
  • Provider/platform coverage: local, daemon, SSH, remote-runtime, WSL, and mobile transport behavior is unaffected because this change only short-circuits a provider-independent shared scanner; macOS was the validation host. No wire format, filesystem, process, or lifecycle code changed.
  • Performance budget: no timers, polling, retries, subprocesses, retained plain chunks, or provider calls were added. Plain chunks now perform two marker checks and return.

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

The change is platform-neutral string parsing. It preserves remote/SSH output ordering and snapshot/replay entry points because only the no-marker path returns early.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

Full pnpm lint, the complete test suite, and packaging are left to CI; the focused gates above passed locally.

@coderabbitai

coderabbitai Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 64792923-6241-4d65-8f27-4d98e7bb0a1a

📥 Commits

Reviewing files that changed from the base of the PR and between 3ff9ac8 and 5be3013.

📒 Files selected for processing (3)
  • src/renderer/src/components/dashboard-popout/AgentTerminalPreview.test.tsx
  • src/shared/terminal-kitty-keyboard-mode-tracker.test.ts
  • src/shared/terminal-kitty-keyboard-mode-tracker.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

scanInternal now sanitizes non-string input and skips kitty escape-sequence parsing when no pending tail or escape byte exists. Tests verify that absent data and large plain-output chunks preserve tracker state. The terminal preview test now uses typed connection and refresh values and object-based replay entries.

Priority: ➖ Normal

Severity of issue fixed: Medium

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The changes in src/renderer/src/components/dashboard-popout/AgentTerminalPreview.test.tsx only update mock typing and replay fixture shape. They do not test or implement the plain-output parser guar… Remove the unrelated AgentTerminalPreview.test.tsx changes, or provide a direct coding connection to issue #21642.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Issue #21642 requires an early return only when no pending scan tail exists and the chunk contains neither \\x1b nor \\x9b. scanInternal() applies this condition before input concatenation, tail e…
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 3…
Title check ✅ Passed The title clearly and concisely describes the main change: skipping Kitty keyboard protocol scans for plain terminal output.
Description check ✅ Passed The description is complete and relevant. It explains the change, rationale, scope, linked issue, testing, performance results, platform considerations, and visual proof. The unchecked manual-testing …
Full details: Out of Scope Changes check

Explanation

The changes in src/renderer/src/components/dashboard-popout/AgentTerminalPreview.test.tsx only update mock typing and replay fixture shape. They do not test or implement the plain-output parser guard in issue #21642. The tracker implementation and tracker tests are in scope.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR

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.

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

✅ No new issues found.

Reviewed changes

  • Fast-path guard in scanInternal — TerminalKittyKeyboardModeTracker.scanInternal() now returns immediately when scanTail is empty and the chunk contains neither ESC (\x1b) nor C1 CSI (\x9b), skipping extractScanTail and the kitty-mode regex for plain output. Escape-bearing chunks, scanReplay, and pending split sequences keep the unchanged parser path.
  • Plain-output state test — asserts that a plain chunk arriving after \x1b[>1u leaves both flags and snapshotFlags at 1.

The guard is sound: every alternative in kittyModeRe begins with \x1b or \x9b, so no matchable sequence can be skipped, and with an empty scanTail the parser path was already a no-op for plain data. It mirrors the identical pre-filter in src/main/daemon/terminal-shell-lifecycle-scanner.ts:97. The new test pins the invariant rather than the fast path (it would pass without the guard); the benchmark in the PR body is the performance oracle, which is reasonable for a shared-parser fast path with no CI latency gate.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

The new escape-byte fast path dereferences the chunk before the string
concatenation that used to coerce a nullish value, so an unchecked
caller now throws instead of no-opping. Normalize once at the top.

Also type the AgentTerminalPreview connect mock against the real preload
signature, which turns the stale bare-string replay fixture that tripped
this into a compile error.
@nwparker
nwparker force-pushed the nwparker/perf-kitty-scan-09-19 branch from 3ff9ac8 to 5be3013 Compare September 19, 2026 22:06

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

✅ No new issues found.

Reviewed changes

  • Nullish-chunk regression fixed — scanInternal() normalizes a non-string data to '' before the escape-byte fast path, restoring totality for unchecked main-process callers; scan() and scanReplay() both funnel through it.
  • Regression test added — a new test drives scan() and scanReplay() with an absent chunk, asserting neither throws and negotiated flags survive; it fails against the guard as first committed (data.includes(...)).
  • AgentTerminalPreview connect mock typed — the mock is now typed against TerminalPreviewApi['connect'], which turned the stale bare-string replay fixture into a compile error; it is corrected to { data, mode: 'replay' }, matching TerminalPreviewReplayChunk[]. That invalid fixture was the runtime path that fed undefined into the tracker.

The fast-path guard remains sound — every alternative in kittyModeRe starts with \x1b or \x9b, so nothing matchable is skipped — and the fix covers every caller at the shared boundary rather than one @ts-nocheck site. Verified locally: focused tracker/preview suites (53 tests) and pnpm tc:web pass.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@nwparker
nwparker merged commit e4c7632 into main Sep 19, 2026
30 checks passed
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.

perf(terminal): skip kitty-mode parsing for plain PTY output

1 participant