Repository navigation
perf(terminal): skip background SGR scans without ESC - #21646
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review. 📝 WalkthroughWalkthrough
Priority: ⬇️ Low Severity of issue fixed: Low 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 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.
✅ No new issues found.
Reviewed changes
- Skip background SGR scan without an ESC introducer —
containsBackgroundSgr()insrc/renderer/src/lib/pane-manager/terminal-complex-script.tsnow returnsfalsewhen the chunk lacks the literal\x1b[before running the global SGR regex.
The guard is a strict necessary condition: every match of SGR_SEQUENCE_PATTERN (ESC \[ ([0-9:;]*) m) contains \x1b[, so classification is unchanged. It is per-chunk with no cross-chunk SGR tail state, and the pattern intentionally only handles the ESC form (never C1 CSI \x9b), which matches the learnings note that a pre-filter is safe only when every regex alternative starts with the matched introducer.
The only production caller (fresh-spawn-follow-reset.ts:94) already gates on scanData.includes('\x1b[') || containsNonAsciiOutput(scanData), so the saved work is the no-match regex scan on non-ASCII, no-ESC chunks — the PR body states this accurately. The existing suite covers both branches (ESC/no-background and no-ESC/non-ASCII), and per the repo's scanner learnings a pre-filter test cannot fail here, so the absence of a new test is not a coverage gap.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes (since prior review of f1474af)
- Remove the duplicate caller pre-gate —
fresh-spawn-follow-reset.tsnow callsterminalOutputPrefersRenderRefresh(scanData)directly instead of(scanData.includes('\x1b[') || session.containsNonAsciiOutput(scanData)) && terminalOutputPrefersRenderRefresh(scanData).
The removal is behavior-equivalent. hasESC || hasNonAscii was already a necessary condition for the classifier to return true: the background-SGR branch requires the \x1b[ introducer, and the Unicode branch requires non-ASCII. When both are false, the classifier's own guard returns false. The prior commit's guard in containsBackgroundSgr plus the existing non-ASCII fast path reproduce the exact predicate, so the caller no longer pays a second JavaScript walk over the same chunk. No side effects were dropped — containsNonAsciiOutput is pure.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
The foreground renderer-risk scan splices the carried tail onto the incoming chunk before classifying it, and nothing covered that ordering: a pre-gate moved back above the concatenation would silently drop the refresh for a background SGR split across ConPTY chunks. Also pins the escape-free ASCII path and the shared global SGR pattern's statelessness across calls, since the background hit returns mid-loop.
534a248 to
f0de5d9
Compare
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes (since prior pullfrog review of 534a248)
- Pin the carried renderer-risk scan tail — added
foreground-renderer-risk-scan-tail.test.ts, which drivessession.foregroundRendererRiskOutputPrefersRenderRefreshthrough a background SGR split across ConPTY chunks ('\x1b[4'then'1m x') and asserts the spliced tail is consumed and cleared. - Cover the escape-free ASCII path and global-pattern statelessness — two tests added to
terminal-complex-script.test.ts. The statelessness case is a real oracle: I confirmed the sharedSGR_SEQUENCE_PATTERN.lastIndexreset is what makes the second background hit returntrue. - Ran both touched suites: 38 tests pass.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

ELI5
The renderer checks terminal output for colored backgrounds so it can repaint a few tricky cases. Before this change, that check opened a full regex rulebook even when the text had no escape marker at all. It now looks for the marker first; if there is none, it skips that one check and continues with the normal Unicode safety check.
What Changed
containsBackgroundSgr()now returns early when the chunk does not contain an ESC +[introducer. Escape-bearing chunks still use the same global SGR regex and parameter handling. The existing renderer-risk and Unicode checks are unchanged.For users, non-ASCII terminal output spends less renderer CPU on an irrelevant SGR scan. Background colors and dense SGR output keep the same refresh decisions.
The production caller now delegates directly to this classifier instead of scanning for non-ASCII text and then scanning the same chunk again. The classifier owns the exact same predicate, so ASCII still returns false while non-ASCII output avoids the duplicate JavaScript walk.
Why
A warmed Node 24 benchmark on macOS compared the exact
origin/mainimplementation with this guard over 100,000 calls:An isolated 4 KiB
containsBackgroundSgrbenchmark showed the avoided no-match regex work more clearly: plain ASCII 426.5 ms → 90.3 ms, CJK 213.0 ms → 90.1 ms, and Arabic 2,285.8 ms → 159.1 ms over 1,000,000 calls; ANSI/background input remained on the existing regex path.The early check is the smallest safe boundary: the SGR regex still receives every chunk that could contain its
ESC[introducer, and no cross-chunk state is changed. Removing the caller pre-gate is behavior-equivalent: 50,000 deterministic generated inputs produced identical old-gate and direct-classifier results. A warmed 200,000-call comparison of the caller path measured 4 KiB input at ASCII 3,456 → 3,166 ms, CJK 1,801 → 1,195 ms, Arabic 2,322 → 1,998 ms, and emoji 67.8 → 36.1 ms.Linked Issue
Fixes #21645
Visual Proof
N/A — this changes a shared classifier’s CPU path and does not change layout or visual output.
Testing
Automated tests added/updated, or explained why not below
I manually tested these changes locally
pnpm test src/renderer/src/lib/pane-manager/terminal-complex-script.test.tsand the newforeground-renderer-risk-scan-tail.test.ts— 38 tests passed.Added coverage for the carried cross-chunk scan tail, which had none: a background SGR split across PTY chunks is classified from the spliced string, so a pre-gate moved back above the concatenation would silently drop the refresh. Also pins that the shared global SGR regex stays stateless across calls. Both were confirmed to fail when the behaviour they pin is broken.
pnpm tcpassed.pnpm run check:code-quality:changedpassed.A deterministic equivalence check matched the old caller gate and direct classifier on 50,000 generated escape, Unicode, and control-byte inputs.
Oxlint, Oxfmt, and
git diff --checkpassed.Existing tests cover background SGR detection, foreground-only SGR, ordinary Unicode output, and renderer-risk classifications.
Review
ESC[reaches the existing background-SGR regex and produces the same refresh decision; chunks without it skip only a check that cannot match. The caller’s removed pre-gate and the classifier return the same boolean.Agent skill upstream boundary
docs/reference/agent-skill-sharing-upstream-boundary.mdand copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.Notes
The guard does not change support for C1 CSI or split sequences because this classifier’s existing pattern only matches the ESC +
[form and carries no SGR tail state.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (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.