[BUG] Unexpected compaction on terminal close with background tasks - context lost at 11% usage
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
When closing the terminal with active background tasks (which get killed), Claude Code creates a summary record in the session JSONL file and compacts the conversation - even though context usage was only ~11% (far below the documented 95% auto-compaction threshold).
On --resume, only this brief summary is loaded instead of the full conversation history. The summary is extremely terse (just one sentence describing the general topic) and provides no useful context about what was being worked on.
Key observations:
- Context was at ~11% (21k/200k tokens) - nowhere near 95% threshold
- No
compact_boundarymarker in JSONL - only asummaryrecord - The
summarywas created immediately aftertask-notificationwithstatus: killed - Full conversation history (153 messages, 5.7MB) exists in JSONL but is not loaded on resume
What Should Happen?
- Compaction should NOT trigger when closing terminal if context is below threshold (95% by default)
- On
--resume, the full conversation history should be loaded from the JSONL file - If compaction must happen on exit, the summary should be detailed enough to preserve important context
Per documentation:
"When you resume a session withclaude --continueorclaude --resume, you pick up where you left off. Your full conversation history is restored."
Error Messages/Logs
No errors - the session simply loses context silently.
Session file analysis:
$ wc -l session.jsonl
2213 lines (5.7MB)
$ jq -r '.type' session.jsonl | sort | uniq -c
1078 saved_hook_context
933 progress
114 assistant
67 user
1 summary ← Created on terminal close
$ jq -c 'select(.type == "summary")' session.jsonl
{"type":"summary","summary":"<one-sentence description>","leafUuid":"dd5ff8de-..."}
Timeline before summary creation:
L2167: user message (task-notification, status: killed)
L2168: user message (task-notification, status: killed)
L2169: user message (task-notification, status: killed)
L2170: summary record ← Created immediately after killed tasks
Steps to Reproduce
- Start a Claude Code session
- Run background tasks using
run_in_background: true - Work on the session until context is ~10-20% (well below 95%)
- Close the terminal (Ctrl+D, close window, or kill the process)
- Background tasks are killed with
status: killednotifications - Resume with
claude --resume <session-id> - Result: Only a brief summary is loaded, full conversation context is lost
Claude Model
Opus
Is this a regression?
I don't know
Claude Code Version
2.1.30 (Claude Code)
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
iTerm2
Additional Information
This is different from #22107 (broken parent chains). In this case:
- Parent chain is intact - all
parentUuidreferences are valid - The problem is unexpected compaction trigger when terminal closes with background tasks
- And resume not loading full history from intact JSONL
Workaround: Before closing terminal, manually stop all background tasks with /stop or run /compact with a detailed description.
Session file available for debugging if needed.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗