fix(desktop): filter terminal control sequence leakage#529
fix(desktop): filter terminal control sequence leakage#529
Conversation
📝 WalkthroughWalkthroughThese changes enhance terminal lifecycle management and escape sequence handling to prevent stale closures, cross-terminal data leakage, and broken ANSI sequences across batch boundaries. Three files are modified: the Terminal component gains lifecycle guards and cleanup logic, CSI suppression expands to cover focus events, and the data batcher detects and preserves complete escape sequences during flush operations. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🧰 Additional context used📓 Path-based instructions (5)apps/desktop/**/*.{ts,tsx}📄 CodeRabbit inference engine (apps/desktop/AGENTS.md)
Files:
**/*.{ts,tsx}📄 CodeRabbit inference engine (AGENTS.md)
Files:
apps/desktop/src/**/*.{ts,tsx}📄 CodeRabbit inference engine (AGENTS.md)
Files:
apps/**/src/**/**/[A-Z]*.tsx📄 CodeRabbit inference engine (AGENTS.md)
Files:
apps/desktop/src/{shared/ipc-channels.ts,main/**/*ipcs.ts,renderer/**/*.tsx}📄 CodeRabbit inference engine (AGENTS.md)
Files:
🧠 Learnings (1)📚 Learning: 2025-12-28T01:56:39.021ZApplied to files:
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
🔇 Additional comments (13)
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: 0
🧹 Nitpick comments (1)
apps/desktop/src/renderer/screens/main/components/WorkspaceView/ContentView/TabsContent/Terminal/filterOrphanedSequences.ts (1)
39-49: LGTM! Solid iterative approach with safety guard.The function correctly handles multiple consecutive orphaned sequences with an early exit when no more are found. The 100-iteration limit is a good safety measure to prevent infinite loops from potential regex bugs.
Consider adding JSDoc comments for better API documentation:
Optional: Add JSDoc for exported function
+/** + * Filters orphaned terminal control sequence fragments from a data string. + * Iteratively removes sequence fragments that appear at the start of the string + * without their ESC prefix (which arrived in a previous data chunk). + * + * @param data - The terminal data string to filter + * @returns The filtered string with orphaned sequences removed + * @example + * filterOrphanedSequences("1R[IHello") // returns "Hello" + * filterOrphanedSequences("Hello World") // returns "Hello World" + */ export function filterOrphanedSequences(data: string): string {
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
apps/desktop/src/renderer/screens/main/components/WorkspaceView/ContentView/TabsContent/Terminal/filterOrphanedSequences.test.tsapps/desktop/src/renderer/screens/main/components/WorkspaceView/ContentView/TabsContent/Terminal/filterOrphanedSequences.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- apps/desktop/src/renderer/screens/main/components/WorkspaceView/ContentView/TabsContent/Terminal/filterOrphanedSequences.test.ts
🧰 Additional context used
📓 Path-based instructions (3)
apps/desktop/**/*.{ts,tsx}
📄 CodeRabbit inference engine (apps/desktop/AGENTS.md)
apps/desktop/**/*.{ts,tsx}: For Electron interprocess communication, ALWAYS use tRPC as defined insrc/lib/trpc
Use alias as defined intsconfig.jsonwhen possible
Prefer zustand for state management if it makes sense. Do not use effect unless absolutely necessary.
For tRPC subscriptions with trpc-electron, ALWAYS use the observable pattern from@trpc/server/observableinstead of async generators, as the library explicitly checksisObservable(result)and throws an error otherwise
Files:
apps/desktop/src/renderer/screens/main/components/WorkspaceView/ContentView/TabsContent/Terminal/filterOrphanedSequences.ts
**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
Use type safety and avoid
anytypes unless absolutely necessary in TypeScript files
Files:
apps/desktop/src/renderer/screens/main/components/WorkspaceView/ContentView/TabsContent/Terminal/filterOrphanedSequences.ts
apps/desktop/src/**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
Move Node.js functionality needed in Electron renderer to
src/main/lib/and communicate via IPC
Files:
apps/desktop/src/renderer/screens/main/components/WorkspaceView/ContentView/TabsContent/Terminal/filterOrphanedSequences.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Build
🔇 Additional comments (3)
apps/desktop/src/renderer/screens/main/components/WorkspaceView/ContentView/TabsContent/Terminal/filterOrphanedSequences.ts (3)
1-23: LGTM! Clear documentation and smart optimization.The file documentation clearly explains the problem of orphaned sequence fragments, and the
ORPHAN_START_CHARSSet provides an efficient fast-path check before running the more expensive regex.
30-37: LGTM! Efficient implementation with proper edge case handling.The helper function correctly implements the fast-path optimization and returns the appropriate length for matched orphaned sequences or zero when no match is found.
25-28: Remove case-insensitive flag from regex pattern — terminal sequences are case-sensitive.The
/iflag on line 63 is incorrect. Terminal control sequences have case-sensitive final bytes:ESC[I(FocusIn) is distinct fromESC[i, and lowercase variants are not valid orphaned sequences. The flag can cause false positives by matching lowercase text that isn't a terminal sequence.Remove the
"i"flag argument to the RegExp constructor on line 63.The patterns for
;\d{1,4}R(orphaned CPR when stream splits) and4;...rgb:...(OSC 4 color palette) are correct based on actual terminal output.
2560df7 to
d40ef4f
Compare
181bee4 to
5235667
Compare
Summary
1R1R1Rand[I/[Oappearing when switching between terminal instancesESC[I,ESC[O)filterOrphanedSequencesfilter for sequences split across data chunks that arrive without their ESC prefixTest plan
1R,[I,[Ogarbage appears🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Improvements
✏️ Tip: You can customize this high-level summary in your review settings.