fix: use readFileState for Edit read-before-edit check, align with Claude Code#1343
Merged
Conversation
…aude Code Edit's read-before-edit check used messageManager.hasFileBeenRead() which only tracked Read tool results. Write tool only updates context.readFileState, so Write → Edit failed without an intermediate Read. Changed check to context.readFileState?.has(resolvedPath) — single source of truth populated by Read, Write, and Edit tools, matching Claude Code's readFileState approach.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Edit's read-before-edit check used
messageManager.hasFileBeenRead()which only tracked Read tool results. The Write tool only updatescontext.readFileState, so Write → Edit failed without an intermediate Read — the agent had to Read a file it just wrote.Fix
Changed Edit's read-before-edit check from
messageManager.hasFileBeenRead(filePath)tocontext.readFileState?.has(resolvedPath).readFileStateis populated by Read, Write, and Edit tools — a single source of truth, matching Claude Code'sreadFileStateapproach where all three tools write to the same Map.Changes
packages/agent-sdk/src/tools/editTool.ts: MovedresolvedPathcomputation before the check; replacedmessageManager.hasFileBeenRead()withcontext.readFileState?.has(resolvedPath)packages/agent-sdk/tests/tools/editTool.test.ts:readFileStateinbeforeEachfor happy-path testsstatmock returning matchingmtimeto prevent staleness check crashesreadFileStateinstead of mockingmessageManagerreadFileStateTest Results
Notes
messageManager.hasFileBeenRead()andrecentFileReadsare left intact — still used bygetRecentFileReads()for compaction context attachment.