Skip to content

fix: Edit tool always fails with "You must read the file" (case-sensitive name mismatch)#1332

Merged
lewis617 merged 4 commits into
mainfrom
worktree-weak-narrow-wolf
Jul 2, 2026
Merged

fix: Edit tool always fails with "You must read the file" (case-sensitive name mismatch)#1332
lewis617 merged 4 commits into
mainfrom
worktree-weak-narrow-wolf

Conversation

@lewis617

@lewis617 lewis617 commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Root Cause

extractFileReadsFromMessage in messageManager.ts:1037 checked block.name === "read" (lowercase), but the actual tool name stored in message blocks is "Read" (capital R, from READ_TOOL_NAME constant).

This caused recentFileReads Map to never be populated, so hasFileBeenRead() always returned false, and the Edit tool's read-before-edit gate always failed — even after the file was Read multiple times.

The sibling method extractSkillInvocationsFromMessage correctly used block.name === "Skill" (capital S), confirming this was an isolated typo.

Fix

  • Use READ_TOOL_NAME constant instead of hardcoded lowercase "read" string
  • Fix test data that used lowercase "read" (masked the bug)
  • Add reproduction tests for hasFileBeenRead() with production tool name

Test plan

  • pnpm -F wave-agent-sdk test tests/managers/messageManager.coverage.test.ts — 26 passed
  • pnpm -F wave-agent-sdk test tests/managers/ tests/tools/editTool.test.ts tests/tools/readTool.test.ts — 852 passed, 1 skipped
  • Type-check, lint, prettier all pass

Fixes #1331

lewis617 added 4 commits July 2, 2026 20:20
…ache_control

Split buildSystemPrompt to return SystemPromptBlock[] instead of string,
separating static content (base prompt, instructions, tool policy, tone)
from dynamic content (permission mode, language, env info, auto memory,
MEMORY.md). For Claude models, callAgent maps cacheable blocks with
cache_control: {type: 'ephemeral'} and non-cacheable blocks without it,
so dynamic content changes don't invalidate the static cache.
transformMessagesForExplicitCache idempotency check prevents double-marking.

- Add SystemPromptBlock interface to prompts/index.ts
- Widen CallAgentOptions.systemPrompt to string | SystemPromptBlock[]
- Update 9 existing tests across 3 files for array-based assertions
- Add 4 block structure tests in prompts.test.ts
- Add 3 SystemPromptBlock[] input tests in aiService.cacheControl.test.ts
- Update spec 019 with user story 5, FR-008, edge cases 7-8
- All 235 test files pass (3150 tests)
Follow the agent.noParams.test.ts pattern to avoid CI environment issues:
- Add workdir: /tmp/test-system-prompt to isolate from CI filesystem
- Add apiKey: test-key for proper agent initialization
- Re-create AIManager after Agent.create() to pick up mock ToolManager
- Register mock ConfigurationService in container
- Clear mockCallAgent call history before sendMessage to ensure
  mock.calls[0] captures the sendMessage call, not any init call

The CI test process was producing zero output (crash/hang), likely due to
real MemoryService reading actual files from process.cwd() in CI.
Align with Claude Code: permission mode is handled purely at the tool
execution layer (PermissionManager), not in the system prompt. This
prevents dynamic block changes when mode switches, preserving prompt
cache.
…tive name mismatch)

Root cause: extractFileReadsFromMessage in messageManager.ts checked
block.name === "read" (lowercase) but the actual tool name stored in
message blocks is "Read" (capital R, from READ_TOOL_NAME constant).
This caused recentFileReads to never be populated, so hasFileBeenRead()
always returned false and the Edit tool's read-before-edit gate always
failed.

Fix: use READ_TOOL_NAME constant instead of hardcoded lowercase string.
Also fixed test data that used lowercase "read" and added reproduction
tests for hasFileBeenRead with production tool name.

Fixes #1331
@lewis617 lewis617 merged commit b0c6ed0 into main Jul 2, 2026
1 check passed
@lewis617 lewis617 deleted the worktree-weak-narrow-wolf branch July 2, 2026 15:00
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.

Edit tool repeatedly fails with "You must read the file" despite prior Read calls

1 participant