Repository navigation
perf(terminal): bound the reattach payload agent-signal scan - #12681
Conversation
hasCursorAgentReattachPayloadScreenSignal built a char-by-char copy of the entire reattach payload so it could read the last header plus 5000 chars. On a 2MB daemon snapshot that cost 17.5ms of synchronous renderer main-thread work — ~75% of what xterm then spends parsing the same bytes — and the miss case paid it in full for a result that is always false. Two changes, both matching existing in-tree precedent: bound the scan to a 256KB tail (as the kitty tracker already bounds its own scan), and strip via the shared precompiled CSI_SEQUENCE_PATTERN instead of a hand-rolled loop, which is also faster in V8 because it copies spans rather than building a rope per character. 2MB snapshot, header hit 17.5ms -> 0.80ms (22x) 2MB snapshot, miss 8.7ms -> 0.52ms (17x) 200KB snapshot, header hit 1.5ms -> 0.62ms (2.4x) config/scripts/terminal-reattach-payload-scan-benchmark.mjs reproduces this and asserts every candidate agrees with the baseline before timing it. It also records a negative result: porting the daemon mouse mirror's includes() pre-filter to the kitty tracker makes reattach slower, because snapshots always contain the introducer. Adds guards for the two behaviours a future shortcut would silently break: a CSI-split header must still match, and a header behind the tail bound must not. Also byte-pins POST_REPLAY_REATTACH_RESET_KEEP_MOUSE, which shipped unpinned. Co-authored-by: Orca <[email protected]>
333d977 to
85e4b10
Compare
📝 WalkthroughWalkthroughThe PTY replay path now limits Cursor Agent screen detection to the final 256 KiB and uses shared CSI stripping. Regression tests cover styled headers, bounded-tail misses, and reset-profile sequences. A deterministic benchmark compares scan implementations, Kitty replay scanning, escape gating, and xterm headless parsing. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
📝 Generate docstrings
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: bab5e7f0-3ff0-47f2-9995-31103ac878c2
📒 Files selected for processing (4)
config/scripts/terminal-reattach-payload-scan-benchmark.mjssrc/renderer/src/components/terminal-pane/pty-connection.test.tssrc/renderer/src/components/terminal-pane/pty-connection.tssrc/shared/terminal-mode-reset-profiles.test.ts
| const kittyGated = (data) => { | ||
| if (!data.includes('\x1b[?') && !data.includes('\x1bc') && !data.includes('\x9b')) { | ||
| return 0 | ||
| } | ||
| return kittyScan(data) | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Make the Kitty gate behavior-equivalent before timing it.
kittyGated('\x1b[>1u') returns 0, but TerminalKittyKeyboardModeTracker.scanReplay parses that Kitty enable sequence and returns flags. The benchmark can report a faster result by skipping valid Kitty protocol input. Add protocol fixtures and assert equal results before measuring.
Proposed fix
const kittyGated = (data) => {
- if (!data.includes('\x1b[?') && !data.includes('\x1bc') && !data.includes('\x9b')) {
+ if (!data.includes('\x1b') && !data.includes('\x9b')) {
return 0
}
return kittyScan(data)
}…i#12681) hasCursorAgentReattachPayloadScreenSignal built a char-by-char copy of the entire reattach payload so it could read the last header plus 5000 chars. On a 2MB daemon snapshot that cost 17.5ms of synchronous renderer main-thread work — ~75% of what xterm then spends parsing the same bytes — and the miss case paid it in full for a result that is always false. Two changes, both matching existing in-tree precedent: bound the scan to a 256KB tail (as the kitty tracker already bounds its own scan), and strip via the shared precompiled CSI_SEQUENCE_PATTERN instead of a hand-rolled loop, which is also faster in V8 because it copies spans rather than building a rope per character. 2MB snapshot, header hit 17.5ms -> 0.80ms (22x) 2MB snapshot, miss 8.7ms -> 0.52ms (17x) 200KB snapshot, header hit 1.5ms -> 0.62ms (2.4x) config/scripts/terminal-reattach-payload-scan-benchmark.mjs reproduces this and asserts every candidate agrees with the baseline before timing it. It also records a negative result: porting the daemon mouse mirror's includes() pre-filter to the kitty tracker makes reattach slower, because snapshots always contain the introducer. Adds guards for the two behaviours a future shortcut would silently break: a CSI-split header must still match, and a header behind the tail bound must not. Also byte-pins POST_REPLAY_REATTACH_RESET_KEEP_MOUSE, which shipped unpinned. Co-authored-by: Orca <[email protected]>
hasCursorAgentReattachPayloadScreenSignalclassifies the reattach payload by building a character-by-character full copy of it (normalized += data[index]), then reading the last"Cursor Agent"occurrence plus 5000 chars. The payload is capped only by the 16MB daemon frame, the work is synchronous on the renderer main thread with no yield, and it runs on all four reattach branches — including the miss case, which pays the whole copy for a result that is alwaysfalse.Measured
config/scripts/terminal-reattach-payload-scan-benchmark.mjs, deterministic SGR-heavy payloads, median of 7 rounds:For scale, xterm's own parse of that 2MB payload is ~23 ms — so the old scan was adding ~75% on top of the parse for the same bytes.
Change
Two lines of idea, both with existing in-tree precedent:
scrollbackAnsi + rehydrateSequences + snapshotAnsi). This is the same shape the kitty tracker already uses for its own scan. 256KB clears even a fully SGR-styled frame by ~2x, so the cut can only drop stale scrollback — which the post-parse viewport veto would have rejected anyway.CSI_SEQUENCE_PATTERNinstead of the hand-rolled loop. Deletes 20 lines. It is also faster in V8: the loop builds a rope node per character, the regex copies spans.Deliberately CSI-only, not the full
stripAnsiEscapeSequences— OSC-carried titles must keep counting as a header occurrence, exactly as they did before.No exotic optimization: no streaming stripper, no rolling-hash search over raw bytes, no worker offload. The bound makes payload size irrelevant, so the constant factor stops mattering.
Negative result, recorded
Porting the daemon mouse mirror's
includes()pre-filter toTerminalKittyKeyboardModeTracker.scanReplaymakes reattach slower — 2.1 ms → 3.7 ms at 2MB — because snapshots always contain\x1b[?, so the gate never fires and the three extra full scans are pure cost. It only pays on the live per-chunk path with escape-free output, worth ~2.7 µs/chunk. The benchmark keeps measuring this so the question doesn't get re-asked.scanReplayis left untouched.Guards
The two behaviours a future "just
lastIndexOfthe raw bytes" shortcut would silently break are now pinned, and both were confirmed to fail without the code under test:Cursor \x1b[1mAgent) must still match — this is why the strip existsAlso byte-pins
POST_REPLAY_REATTACH_RESET_KEEP_MOUSE, which shipped unpinned in #12461 — without it, silently collapsing that profile into the live-agent one would pass CI.Verification
pnpm typecheckcleanFollows #12461, now merged; rebased onto main.
Made with Orca 🐋