Context compaction clears terminal scrollback history
Problem
When Claude Code compacts context during a long session, it clears the terminal screen. This destroys the terminal scrollback buffer, making it impossible to scroll back and review earlier output (command results, test output, build logs, etc.).
Expected Behavior
Context compaction should preserve terminal scrollback history. The compaction is an internal operation — it shouldn't affect the user's ability to review their terminal history.
Current Behavior
During compaction, the terminal is cleared (likely via ANSI clear-screen escape sequences). After compaction completes, scrolling up shows nothing before the compaction point. All prior command output, test results, and conversation history is gone from the terminal buffer.
Impact
This is particularly painful during long development sessions where you need to:
- Reference earlier test output or build errors
- Compare before/after results across multiple runs
- Review a sequence of changes that were made earlier in the session
Suggested Fix
Either:
- Don't emit terminal-clearing escape sequences during compaction
- Use an approach that preserves scrollback (e.g., just continue output below without clearing)
- Add a configuration option to disable screen clearing during compaction
Environment
- macOS (iTerm2 terminal)
- Claude Code CLI
4 Comments
This is a known pain point with Ink's terminal rendering. The compaction triggers a full re-render that clears the scrollback.
Practical workarounds:
scriptto capture full session output:``
bash
`script -q ~/claude-sessions/$(date +%Y%m%d-%H%M%S).log claude
grepThis logs everything to a file regardless of terminal clearing. You can
orless` the log anytime.``
bash
``# In ~/.tmux.conf
set -g history-limit 50000
tmux maintains its own scrollback buffer that's more resilient to ANSI clear sequences. Not immune, but catches more than raw terminal.
In your CLAUDE.md, add a rule like:
``
``When running tests or builds, tee output to /tmp/last-test.log
This way Claude saves critical output before compaction can eat it.
--output-format stream-jsonif you need a programmatic record:``
bash
``claude --output-format stream-json 2>&1 | tee session.jsonl
The JSON stream captures all tool results and assistant messages.
The
scriptapproach (option 1) is the most transparent — you get everything regardless of what the TUI does.This affects me as well, and fyi tmux is not a workaround. It's affected the same way. :P
The
scriptworkaround (I usedscript -q ~/claude-sessions/$(pwd | tr / -)_$(date +%s).log -c claude) is certainly a thing, but not awesome. It captures all the line drawing characters verbatim, and it will balloon in size very quickly.This was fixed in v2.1.101 — Context compaction no longer clears the terminal's scrollback buffer. If you're still seeing this in the latest version, please comment with your version and repro and we'll reopen.
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.