Fix AGENTS.md patch replacing CLAUDE.md content instead of falling back#579
Conversation
actual minified code in CC is ensureServerStarted:$,sendRequest:H
The injection was at the wrong location — before `return { content }`,
which runs after CLAUDE.md is successfully read, unconditionally replacing
its content with AGENTS.md. Moved injection to the early `return null`
(after existsSync/statSync check) so alternatives are only tried when
CLAUDE.md doesn't exist.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThis PR adds a comprehensive test suite for the agentsMd module and refactors its routing logic. The implementation introduces a Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (3)
src/patches/agentsMd.ts (2)
107-107: Use the intermediate buffer asshowDiff’s old content in step 2.Passing
fileagain in the secondshowDiffmixes step 1 + step 2 changes and
can offset context around the replacement site.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/patches/agentsMd.ts` at line 107, The second call to showDiff currently reuses `file` as the "old" content which mixes step-1 and step-2 changes; replace the first-steps result with the intermediate buffer (the variable holding the file content after step 1 — e.g., `newFile` or the actual intermediate buffer name used in this scope) as the oldContent argument when calling showDiff(file, newFile, newStr, earlyReturnStart, earlyReturnStart) so the second diff shows only step-2 changes; update the second invocation of showDiff to pass that intermediate buffer instead of `file` (refer to the showDiff call and the intermediate buffer/newFile variable in this file).
78-84: Remove the inline “Step” comments to match repository TS rules.The function body is clear without these comments, and the added comments violate
the repo convention for TS/JS files.As per coding guidelines, “Do not add comments unless explicitly requested.”
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/patches/agentsMd.ts` around lines 78 - 84, Remove the inline "Step" comments added in this patch: delete the comment lines starting with "// Step 1: Add didReroute parameter to function signature" and "// Step 2: Inject fallback at the early return null (when file doesn't exist)" so only the actual code mutations remain (the insertion of ',didReroute' via sigIndex/newFile and the showDiff call); ensure no other inline "Step" comments remain in the same block around the symbols sigIndex, newFile, file.slice, or showDiff so the file conforms to the repository TS/JS comment policy.src/patches/agentsMd.test.ts (1)
44-47: Test title is misleading for what this assertion actually validates.This test currently checks that generated code contains a
return nullbranch,
not runtime behavior when no alternatives are found.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/patches/agentsMd.test.ts` around lines 44 - 47, The test title is misleading: the spec for writeAgentsMd(mockFunction, altNames) is asserting generated source contains a "return null" branch, not actual runtime behavior. Update the test either by renaming it to something like "should generate code with a return null branch when no alternatives are found" to reflect the string/assertion, or change the assertion to execute the generated function and assert it returns null at runtime (invoke the code produced by writeAgentsMd and call the resulting function); reference writeAgentsMd and the existing mockFunction/altNames symbols when making the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/patches/agentsMd.test.ts`:
- Around line 44-47: The test title is misleading: the spec for
writeAgentsMd(mockFunction, altNames) is asserting generated source contains a
"return null" branch, not actual runtime behavior. Update the test either by
renaming it to something like "should generate code with a return null branch
when no alternatives are found" to reflect the string/assertion, or change the
assertion to execute the generated function and assert it returns null at
runtime (invoke the code produced by writeAgentsMd and call the resulting
function); reference writeAgentsMd and the existing mockFunction/altNames
symbols when making the change.
In `@src/patches/agentsMd.ts`:
- Line 107: The second call to showDiff currently reuses `file` as the "old"
content which mixes step-1 and step-2 changes; replace the first-steps result
with the intermediate buffer (the variable holding the file content after step 1
— e.g., `newFile` or the actual intermediate buffer name used in this scope) as
the oldContent argument when calling showDiff(file, newFile, newStr,
earlyReturnStart, earlyReturnStart) so the second diff shows only step-2
changes; update the second invocation of showDiff to pass that intermediate
buffer instead of `file` (refer to the showDiff call and the intermediate
buffer/newFile variable in this file).
- Around line 78-84: Remove the inline "Step" comments added in this patch:
delete the comment lines starting with "// Step 1: Add didReroute parameter to
function signature" and "// Step 2: Inject fallback at the early return null
(when file doesn't exist)" so only the actual code mutations remain (the
insertion of ',didReroute' via sigIndex/newFile and the showDiff call); ensure
no other inline "Step" comments remain in the same block around the symbols
sigIndex, newFile, file.slice, or showDiff so the file conforms to the
repository TS/JS comment policy.
Summary
agentsMdpatch injected fallback logic at the wrong location — beforereturn { content }, which runs after CLAUDE.md is successfully read, unconditionally replacing its content with AGENTS.mdreturn null(afterexistsSync/statSynccheck) so alternatives are only tried when CLAUDE.md doesn't existTest plan
pnpm buildpasses (no type errors)pnpm testpasses (169 tests, including 7 new ones)CLAUDE.mdandAGENTS.md, theCLAUDE.mdcontent is used (not replaced)🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests