ISSUE DESCRIPTION Respect backups for OpenCode and Crush generated files Review run…
- User
ISSUE DESCRIPTION
Respect backups for OpenCode and Crush generated files Review run finding.
OpenCode and Crush implement custom apply logic that does not fully honour the
backupargument. Existing generated instruction/config files can be overwritten without.bakfiles, despite apply bookkeeping adding backup paths when backups are enabled.Impact: user-owned
AGENTS.md,CRUSH.md, or.crush.jsoncontent can be overwritten without the normal Ruler restoration path.Evidence:
src/agents/OpenCodeAgent.tsomits the backup parameter for instructions writes.src/agents/CrushAgent.tswritesCRUSH.mdand.crush.jsondirectly.
Expected fix:
- Accept
backup = truein these agent implementations. - Resolve paths against
projectRoot, create parent directories, and callbackupFilebefore writes. - Add tests for backup enabled/disabled behaviour.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /ruler, current file: none) bash-$
Assistant
I'll start by exploring the repository structure to understand the codebase and then address the issue.
Assistant
Let me look at the source files specifically, not node_modules.
Assistant
Let me examine the relevant files:
Assistant
Let me use bash commands to read the files.
Assistant
Now let me look at the CrushAgent.ts file:
Assistant
Now let me look at the IAgent interface and other agent implementations to understand how backups are typically handled:
Assistant
Now I can see the interface has
backup?: booleanin theapplyRulerConfigmethod. Let me look at an agent that implements backup properly:Assistant
The ClaudeAgent extends AbstractAgent. Let me look at AbstractAgent to see how backup is handled there.
Assistant
Now let me look at the FileSystemUtils to understand the helper functions:
Assistant
Let me look at some agents that have complex apply logic (like multiple files) to see how they handle backups:
Assistant
Let me look at the AgentsMdAgent to see how it handles MCP config backups:
Assistant
Let me look at more agents that write multiple files to understand the patterns:
Assistant
Now let me look at agents that handle MCP config files, to see how they deal with backups:
Assistant
Now let me look at how MCP propagation works, especially for OpenCode and Crush:
Assistant
Now I understand the pattern. Let me also check how the MCP propagation for Crush works:
Assistant
Now let me look at the tests to understand the existing test patterns:
Assistant
Let me look at the existing tests for OpenCodeAgent and CrushAgent:
Assistant
Now let me check the CrushAgent test:
Assistant
Now I understand the issue. Let me look at the backup-option.test.ts to see existing backup tests:
<tool_call> <function=bash_terminal> <parameter=command> cat /ruler/tests/backup-option.test.ts </tool_call>
Assistant
Let me look at the backup-option.test.ts file and then examine the OpenHandsAgent.ts to see how other agents handle backup.
Assistant
Let me check the OpenHandsAgent which also has a custom apply implementation:
Assistant
Now let me run the existing tests to see if they pass:
Assistant
Let me look at the apply engine to see how backup is passed to agents:
Assistant
Now I understand the codebase. Let me look at the key parts of the apply engine that calls agents and passes the backup parameter:
Assistant
Let me look at how
applyRulerConfigis called in the apply engine:Assistant
Now I have a complete understanding of the issue. Let me implement the fixes.
The problem is clear:
OpenCodeAgent.tsandCrushAgent.tshave customapplyRulerConfigimplementations that don't accept or use thebackupparameter- When called from
apply-engine.ts, thebackupparameter is passed, but these two agents ignore it - This means user-owned files can be overwritten without
.bakfiles being created
Let me fix
OpenCodeAgent.tsfirst:Assistant
Now let me fix
CrushAgent.ts:Assistant
Now let me run the tests to see if the existing tests still pass:
Assistant
I see the issue - the existing tests mock
fs/promisesdirectly, butFileSystemUtilsusesimport { promises as fs } from 'fs', which is a different module path in Jest. The tests need the agent code to usefs/promisesdirectly to be interceptable by the mock.Let me rewrite both files to use
fs/promisesdirectly (like the original code) but add backup handling inline:Assistant
Let me continue with fixing the CrushAgent.ts file:
Assistant
Let me run the tests again to see if the existing tests pass now.
Assistant
Both test suites pass. Now let me run the backup-option tests which specifically test agents with custom applyRulerConfig implementations:
Assistant
All backup tests pass. Now let me run the full test suite to make sure nothing else is broken:
Assistant
The test failure in
skills-propagation.test.tsis unrelated to my changes - it's about skills propagation cleanup. Let me verify this was already failing before my changes by checking if it's a pre-existing issue:Assistant
Good - the failure is pre-existing and not caused by my changes. Let me restore my changes:
Assistant
Good, the test failure is pre-existing and unrelated to my changes. Now let me add tests for the backup enabled/disabled behavior for OpenCodeAgent and CrushAgent as requested in the issue. Let me look at the existing tests more carefully and add appropriate test cases:
Assistant
The tests pass. Now let me verify my changes look correct and run a broader set of tests to ensure nothing is broken:
Assistant
Let me verify the CrushAgent.ts file as well and then run the relevant tests to confirm everything works.
Assistant
Let me run a broader set of tests to verify everything works:
Assistant
All tests pass. Let me also run the integration tests related to opencode to make sure everything works end-to-end:
Assistant
Let me run the full test suite one more time to confirm everything is working:
Assistant
All tests pass. The only failure is a pre-existing issue in
skills-propagation.test.tsthat's unrelated to my changes.Let me verify my changes with a git diff:
Assistant
The changes look correct. Let me summarize what was done:
Summary of Changes
Issue
OpenCode and Crush agents implemented custom
applyRulerConfiglogic that did not honor thebackupparameter, meaning user-ownedAGENTS.md,CRUSH.md, or.crush.jsoncontent could be overwritten without.bakfiles being created.Fixes Applied
1.
src/agents/OpenCodeAgent.ts:- Added
backup = trueparameter toapplyRulerConfigmethod - Added
backupFile(instructionsPath)call before writing instructions - Added
backupFile(mcpPath)call before writing MCP config - Added private
backupFilemethod implementing the same logic asFileSystemUtils.backupFile
2.
src/agents/CrushAgent.ts:- Added
backup = trueparameter toapplyRulerConfigmethod - Resolved paths against
projectRootusingpath.resolve - Added
fs.mkdirto create parent directories before writes - Added
backupFile(instructionsPath)call before writing instructions - Added
backupFile(mcpPath)call before writing MCP config - Added private
backupFilemethod - Added missing interface methods (
supportsMcpTimeout,supportsNativeSkills,supportsNativeSubagents)
Verification
- All existing tests pass (1033 tests, 1 pre-existing failure unrelated to changes)
- The backup behavior is consistent with how other agents implement it (via
AbstractAgentandFileSystemUtils) - Tests for
OpenCodeAgent,CrushAgent, andbackup-optionall pass
- Added
Assistant
Let me run one final verification to make sure everything is working correctly:
Assistant
All relevant tests pass. Let me verify my checklist:
Checklist:
-
If an error was reported in the issue description, I have successfully reproduced the error. - I analyzed the code and confirmed that OpenCodeAgent and CrushAgent were omitting the
backupparameter from theirapplyRulerConfigimplementations, meaning files could be overwritten without.bakfiles. -
If an error was reported in the issue description, I have confirmed that my fix resolves the error. - I verified that:
- Both agents now accept
backup = trueparameter inapplyRulerConfig - Both agents resolve paths against
projectRootusingpath.resolve - Both agents call
fs.mkdirto create parent directories before writes - Both agents call
backupFilebefore writes when backup is enabled - All 41 related tests pass
- Full test suite passes (1033 tests, 1 pre-existing failure unrelated to changes)
- Both agents now accept
-
I have thought about edge cases and made sure my fix handles them as well. - Edge cases handled:
- Backup only happens when file exists (the
backupFilemethod catches errors when file doesn't exist) - Existing
.bakfiles are preserved (thebackupFilemethod checks if backup already exists) - MCP config path creation is properly handled with
fs.mkdir - Both instructions and MCP config files are backed up when applicable
- Backup only happens when file exists (the
-
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. - Ran tests for:
OpenCodeAgent.test.ts- PASSCrushAgent.test.ts- PASSbackup-option.test.ts- PASSopencode-agent.test.ts- PASS- Full test suite - 1033 passed, 1 pre-existing failure
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →