Skip to content

fix: use readFileState for Edit read-before-edit check, align with Claude Code#1343

Merged
lewis617 merged 1 commit into
mainfrom
worktree-quiet-gentle-valley
Jul 6, 2026
Merged

fix: use readFileState for Edit read-before-edit check, align with Claude Code#1343
lewis617 merged 1 commit into
mainfrom
worktree-quiet-gentle-valley

Conversation

@lewis617

@lewis617 lewis617 commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Problem

Edit's read-before-edit check used messageManager.hasFileBeenRead() which only tracked Read tool results. The Write tool only updates context.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) to context.readFileState?.has(resolvedPath).

readFileState is populated by Read, Write, and Edit tools — a single source of truth, matching Claude Code's readFileState approach where all three tools write to the same Map.

Changes

  • packages/agent-sdk/src/tools/editTool.ts: Moved resolvedPath computation before the check; replaced messageManager.hasFileBeenRead() with context.readFileState?.has(resolvedPath)
  • packages/agent-sdk/tests/tools/editTool.test.ts:
    • Pre-populated readFileState in beforeEach for happy-path tests
    • Added default stat mock returning matching mtime to prevent staleness check crashes
    • Updated rejection test to use empty readFileState instead of mocking messageManager
    • Updated allow test to use path-specific readFileState
    • Renamed "skip staleness check" test to "reject edit when readFileState is not provided" (now expects failure)

Test Results

  • 24/24 editTool tests pass
  • 65/65 related tool tests pass (writeTool, readTool, editToolSerialization)
  • Build and type-check pass

Notes

messageManager.hasFileBeenRead() and recentFileReads are left intact — still used by getRecentFileReads() for compaction context attachment.

…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.
@lewis617 lewis617 merged commit fa90e6f into main Jul 6, 2026
1 check passed
@lewis617 lewis617 deleted the worktree-quiet-gentle-valley branch July 6, 2026 06:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant