[BUG] File Edit/Write Fails After Context Compaction
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?
After context compaction (session summarization), the Read/Edit/Write tools fail for files that were accessed before compaction, even though the Read tool appears to succeed.
## Environment
- Platform: Windows 11
- Claude Code Version: 2.1.1
## Steps to Reproduce
- Start a new Claude Code session
- Read and edit a file multiple times (File A)
- Continue the conversation until context compaction occurs
- After compaction, Read File A → Success
- Edit File A → ERROR "File has been unexpectedly modified"
- Read File A again, then Write → ERROR "File has not been read yet"
## Observed Behavior
| File Type | Read | Edit | Write |
|-----------|------|------|-------|
| Accessed before compaction | ✓ (appears to work) | ✗ Error | ✗ Error |
| Created with Write tool after compaction | ✓ | ✓ | ✓ |
| Never accessed before compaction | ✓ | ✓ | ✓ |
## Root Cause Analysis
After context compaction, the internal file state tracking appears corrupted. Read executes but does NOT register the file in internal state, causing Edit/Write to fail.
## Workaround
Start a new session instead of continuing from compaction.
What Should Happen?
After context compaction, Read/Edit/Write tools should work normally for all files, including files that were accessed before compaction.
Error Messages/Logs
Error 1 (Edit tool):
"File has been unexpectedly modified. Read it again before attempting to write it."
Error 2 (Write tool after Read):
"File has not been read yet. Read it first before writing to it."
Steps to Reproduce
- Start a new Claude Code session
- Read and edit a file multiple times (File A) until context compaction occurs
- After compaction (session continues with summary), run: Read File A
- Read succeeds and displays file content
- Run: Edit File A (any change)
- ERROR: "File has been unexpectedly modified"
- Run: Read File A again
- Run: Write File A (same or modified content)
- ERROR: "File has not been read yet"
Note: Files created with Write tool AFTER compaction, or files never accessed before compaction, work correctly.
Claude Model
Opus
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.1.1
Platform
Anthropic API
Operating System
Windows
Terminal/Shell
PowerShell
Additional Information
Potentially related issues:
- #18158 (context compaction related)
- #16182 (file state tracking related)
The bug specifically affects files that were accessed (Read/Edit/Write) before context compaction occurred. Files created with Write tool after compaction, or files never accessed before compaction, work correctly.
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗