waveterm icon indicating copy to clipboard operation
waveterm copied to clipboard

fix: prevent terminal history duplication on restart

Open sgeraldes opened this issue 1 month ago • 2 comments

Problem

Terminal output was being duplicated when restarting Wave Terminal, causing the same content to appear multiple times in the scrollback buffer.

Root Cause

When Wave Terminal restarts and loads a terminal block:

  1. Cache restoration: Loads the serialized terminal state (full scrollback buffer + screen) that was saved at PTY offset X
  2. Main file append: Then loads the raw PTY data from offset X onward and re-renders it

The issue is that the serialized cache already contains the rendered terminal output from offset 0 to X. When the raw PTY data from offset X is then appended and re-rendered, it creates duplicates because that data was already processed into the cached terminal state.

Solution

Modified loadInitialTerminalData() in frontend/app/view/term/termwrap.ts to:

  • Track whether we loaded from cache with loadedFromCache flag
  • When cache is loaded, fetch the main terminal file from ptyOffset (which returns only NEW data that arrived after the cache was saved)
  • Only append truly new data that wasn't part of the cached state

Testing

  1. Run some commands in a terminal
  2. Wait for state to be saved (5 second idle timeout)
  3. Restart Wave Terminal
  4. Verify terminal history appears correctly without duplication

Changes

  • Modified frontend/app/view/term/termwrap.ts (lines 716-757)
  • Added better logging to track cache usage and offsets

Fixes the terminal history corruption issue where scrolling up would show repeated blocks of output.

sgeraldes avatar Jan 15 '26 01:01 sgeraldes

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Jan 15 '26 01:01 CLAassistant

Walkthrough

This pull request modifies termwrap.ts to enhance terminal state management. In handleOsc7Command, logic is added to automatically update the active tab's base directory when it is unlocked and either unset or set to "~". In loadInitialTerminalData, a loadedFromCache flag tracks whether data originates from cache, which determines whether the main data fetch uses ptyOffset or offset 0. Additionally, the terminal write operation is conditional: mainData is only appended if both mainFile and mainData exist and mainData has non-zero byteLength, preventing unnecessary writes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: prevent terminal history duplication on restart' directly summarizes the main change—fixing a bug that caused terminal output duplication when Wave Terminal restarts, which is the core issue addressed in the changeset.
Description check ✅ Passed The description clearly explains the problem (terminal duplication on restart), root cause (cache + raw data re-rendering), solution (loadedFromCache flag and offset handling), and testing steps, all directly related to the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • [ ] 📝 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Jan 15 '26 01:01 coderabbitai[bot]