Preserve terminal history across ANSI control sequences#1367
Preserve terminal history across ANSI control sequences#1367juliusmarminge merged 2 commits intomainfrom
Conversation
- sanitize PTY output before persisting transcript history - handle chunk-split control sequences and reset pending state on restart/exit - add regression tests for reopened sessions
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Three-byte escape sequences leak final byte into history
- Added detection for ECMA-48 intermediate bytes (0x20-0x2F) after ESC, consuming all intermediate bytes plus the final byte so sequences like ESC ( B are fully skipped instead of leaking the final byte into visibleText.
Or push these changes by commenting:
@cursor push 87caa1b78f
Preview (87caa1b78f)
diff --git a/apps/server/src/terminal/Layers/Manager.ts b/apps/server/src/terminal/Layers/Manager.ts
--- a/apps/server/src/terminal/Layers/Manager.ts
+++ b/apps/server/src/terminal/Layers/Manager.ts
@@ -321,6 +321,22 @@
continue;
}
+ if (nextCodePoint >= 0x20 && nextCodePoint <= 0x2f) {
+ let cursor = index + 2;
+ while (
+ cursor < input.length &&
+ input.charCodeAt(cursor) >= 0x20 &&
+ input.charCodeAt(cursor) <= 0x2f
+ ) {
+ cursor++;
+ }
+ if (cursor >= input.length) {
+ return { visibleText, pendingControlSequence: input.slice(index) };
+ }
+ index = cursor + 1;
+ continue;
+ }
+
index += 2;
continue;
}- keep visible ANSI styling and screen clears in persisted terminal history - strip replay-unsafe query/reply sequences and avoid chunk-boundary byte leakage - add regression coverage for split escape sequences
Merged upstream improvements while preserving fork identity: - Claude Code adapter support (pingdotgg#179) - Context window usage meter (pingdotgg#1351) - Sidebar project/thread sorting by recency (pingdotgg#1372) - Git action progress streaming (pingdotgg#1214, pingdotgg#1388) - Terminal history ANSI preservation (pingdotgg#1367) - Word wrapping setting (pingdotgg#1326) - Resizable chat sidebar (pingdotgg#1347) - Configurable timestamp format (pingdotgg#855) - Selective file staging in commit dialog (pingdotgg#872) - Fuzzy workspace search (pingdotgg#256) - Scroll-to-bottom pill (pingdotgg#619) - Numerous bug fixes and improvements Preserved fork customizations: - Fatma branding (all @t3tools -> @fatma) - Mobile UI (swipe gestures, bottom nav, mobile shells) - VSCode-like source control (staged/unstaged, file diffs) - PWA support - Custom CI/CD workflows Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>


Summary
Testing
bun fmtbun lintbun typecheckbun run test apps/server/src/terminal/Layers/Manager.test.tsNote
Medium Risk
Adds a custom parser to strip terminal query/reply control sequences while persisting PTY output, which could inadvertently drop or mangle legitimate escape sequences and affect users’ saved terminal history.
Overview
Terminal history persistence now sanitizes PTY output via
sanitizeTerminalHistoryChunk, stripping replay-unsafe CSI/OSC (and related string) query/reply sequences while preserving normal output and style/clear sequences.To avoid corrupting history when escape sequences are split across data events, session state now tracks
pendingHistoryControlSequenceand resets it onclear,restart, process stop, and exit; pure control-sequence chunks no longer trigger a persist write. Adds regression tests covering whole and chunk-split ANSI sequences and ensuring no byte leakage from ESC sequences.Written by Cursor Bugbot for commit 56063fa. This will update automatically on new commits. Configure here.
Note
Preserve terminal history across ANSI control sequences by stripping replay-unsafe sequences
sanitizeTerminalHistoryChunkin Manager.ts to parse incoming terminal data and strip CSI/OSC query and reply sequences (e.g. cursor position reports, color queries) that are unsafe to replay on session restore.pendingHistoryControlSequenceand prepended to the next chunk.pendingHistoryControlSequenceis reset on process exit, stop, clear, and session restart/reopen.Macroscope summarized 56063fa.