Tools
Tools: Claude Code Lost My 4-Hour Session. Here's the $0 Fix That Actually Works
2026-02-21
0 views
admin
It Happened on a Friday Afternoon ## Why Claude Code Compaction Hurts So Much ## What I Tried (And Why It Failed) ## The Fix: Snapshot Your Session State ## Taking This Further: Mantra ## The Broader Problem Four hours into a complex auth refactor. State machines, edge cases, all the subtle context Claude and I had built up together. Claude's response came back generic. It had forgotten the architecture decisions we'd made. The nuanced understanding of why we'd structured things a certain way — gone. I spent 45 minutes trying to rebuild the context. It never fully came back. If you use Claude Code regularly, this has probably happened to you. Claude Code has a context window limit. When you hit it, it "compacts" — summarizes your conversation to free up space. The summary is lossy. Nuanced architectural decisions, the "why" behind choices, subtle patterns — these get flattened into generic descriptions. The worst part? It happens silently. No warning. You're deep in flow and suddenly you're talking to a Claude that doesn't know your codebase anymore. Approach 1: Manual CLAUDE.md summaries I started writing summaries of important context into CLAUDE.md. This helped with project-level context but didn't capture session-specific state — the conversation flow, what we'd just tried, where we were in the middle of a task. Approach 2: Copying conversation snippets Tedious. You have to decide what's important in real-time, which breaks flow. And it's not searchable. Approach 3: /compact before it auto-compacts Slightly better — at least you control timing. But you're still losing context; you're just choosing when to lose it. The real problem: None of these preserve the conversation history — the actual back-and-forth that contains the reasoning. Claude Code stores conversation history as JSONL files at: Each file is a complete record of a coding session. The insight: if you snapshot these files, you can restore exact session state. Here's a simple script: Run this before any risky operation. If compaction destroys your context: Then claude --resume [session-id] brings back exact context. I automated this workflow into a tool called Mantra — it creates visual snapshots of your Claude Code (and Cursor, Windsurf) sessions so you can browse and restore them like a git history. The free manual approach above works. Mantra just makes it: Download at mantra.gonewx.com (macOS/Windows/Linux). This isn't just a Claude Code issue. Cursor has the same problem with its AI chat history. Windsurf sessions disappear. Aider loses context between runs. We're all building complex things with AI assistants, but the tools don't yet treat session state as something worth preserving. Until they do, we have to manage it ourselves. The script above is free and takes 2 minutes to set up. Your future self will thank you the next time compaction hits at the worst possible moment. Have you lost important session context to compaction? What's your workaround? Share in the comments. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse CODE_BLOCK:
~/.claude/projects/[project-hash]/[session-id].jsonl Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
~/.claude/projects/[project-hash]/[session-id].jsonl CODE_BLOCK:
~/.claude/projects/[project-hash]/[session-id].jsonl COMMAND_BLOCK:
#!/bin/bash
# save-session.sh
PROJECT_DIR="$HOME/.claude/projects"
BACKUP_DIR="$HOME/.claude/session-backups/$(date +%Y%m%d_%H%M%S)" mkdir -p "$BACKUP_DIR"
cp -r "$PROJECT_DIR" "$BACKUP_DIR"
echo "Session saved to $BACKUP_DIR" Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
#!/bin/bash
# save-session.sh
PROJECT_DIR="$HOME/.claude/projects"
BACKUP_DIR="$HOME/.claude/session-backups/$(date +%Y%m%d_%H%M%S)" mkdir -p "$BACKUP_DIR"
cp -r "$PROJECT_DIR" "$BACKUP_DIR"
echo "Session saved to $BACKUP_DIR" COMMAND_BLOCK:
#!/bin/bash
# save-session.sh
PROJECT_DIR="$HOME/.claude/projects"
BACKUP_DIR="$HOME/.claude/session-backups/$(date +%Y%m%d_%H%M%S)" mkdir -p "$BACKUP_DIR"
cp -r "$PROJECT_DIR" "$BACKUP_DIR"
echo "Session saved to $BACKUP_DIR" COMMAND_BLOCK:
# Restore a specific session
cp ~/.claude/session-backups/20260221_143022/projects/[hash]/[session].jsonl \ ~/.claude/projects/[hash]/ Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
# Restore a specific session
cp ~/.claude/session-backups/20260221_143022/projects/[hash]/[session].jsonl \ ~/.claude/projects/[hash]/ COMMAND_BLOCK:
# Restore a specific session
cp ~/.claude/session-backups/20260221_143022/projects/[hash]/[session].jsonl \ ~/.claude/projects/[hash]/ - 3+ hour coding sessions on complex features
- Refactoring sessions with many interdependencies
- Debugging sessions where context about what you already tried matters
- Multi-file changes where Claude needs to hold a lot in mind - Visual — see your session timeline
- Automatic — snapshots happen as you work
- Cross-tool — works with Claude Code, Cursor, and Windsurf
how-totutorialguidedev.toailinuxbashgit