Context compaction clears terminal scrollback history

Status Fixed / completed
Maintainer reply ✓ Yes — ashwin-ant
Activity 4 comments · opened Mar 15, 2026 · closed Apr 18, 2026
💡 Likely answer: A maintainer (ashwin-ant, collaborator) responded on this thread — see the highlighted reply below.

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:

  1. Don't emit terminal-clearing escape sequences during compaction
  2. Use an approach that preserves scrollback (e.g., just continue output below without clearing)
  3. Add a configuration option to disable screen clearing during compaction

Environment

  • macOS (iTerm2 terminal)
  • Claude Code CLI

View original on GitHub ↗

4 Comments

yurukusa · 5 months ago

This is a known pain point with Ink's terminal rendering. The compaction triggers a full re-render that clears the scrollback.

Practical workarounds:

  1. Use script to capture full session output:

``bash
script -q ~/claude-sessions/$(date +%Y%m%d-%H%M%S).log claude
`
This logs everything to a file regardless of terminal clearing. You can
grep or less` the log anytime.

  1. Use tmux with increased scrollback:

``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.

  1. Pipe important output to files proactively:

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.

  1. Use --output-format stream-json if 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 script approach (option 1) is the most transparent — you get everything regardless of what the TUI does.

chreekat · 5 months ago

This affects me as well, and fyi tmux is not a workaround. It's affected the same way. :P

  • NixOS 25.11.8023.4590696c8693 (Xantusia)
  • kitty 0.440
  • tmux 3.6a
  • claude code 2.1.25

The script workaround (I used script -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.

ashwin-ant collaborator · 4 months ago

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.

github-actions[bot] · 4 months ago

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.