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
Let me start by exploring the repository structure to understand the codebase.
Assistant
Now let me look at how other agents handle the backup parameter and the
backupFileutility:Assistant
Let me look at how other agents handle the backup parameter and which ones use it:
Assistant
Let me check how
backupis handled and look at existing tests:Assistant
Now let me look at the backup test to understand how backup tests are structured:
Assistant
Now let me look at how the apply loop in the codebase calls
applyRulerConfigto understand the call flow:Assistant
Let me check the FirebenderAgent and AugmentCodeAgent for more examples:
Assistant
Now I have a good understanding. Let me check existing tests for
backupbehavior, particularly ones related to OpenCode and Crush:Assistant
Now let me check the revert.ts to understand how backups are managed:
Assistant
Now I have a good understanding of the code. Let me also look at how other agents with custom apply logic use backup:
Assistant
Now let me look at the existing OpenCode test mock to understand its structure:
Assistant
Now I have a comprehensive understanding. Let me plan the fix:
Plan
-
Fix
OpenCodeAgent.ts:- Accept
backupparameter (default true) - For instructions: call
backupFilebeforewriteFileif backup is enabled - For MCP config: call
backupFilebeforewriteFileif backup is enabled
- Accept
-
Fix
CrushAgent.ts:- Accept
backupparameter (default true) - Resolve paths against
projectRootusingpath.resolve - Create parent directories (currently does NOT do this for both files!)
- Call
backupFilebefore each write
- Accept
-
Add tests for backup enabled/disabled behaviour.
Let me first check the existing structure to understand the path resolution:
-
Assistant
Now let me also look at how
path.resolveis being applied for the test ofCrushAgent. Let me see ifpath.resolvewill handle relative paths correctly:Assistant
Now let me also examine how
applyRulerConfigis called for the CrushAgent with the agent config option ofoutputPath:Assistant
Now let me run the existing tests to confirm the current state:
Assistant
Tests pass. Now let me also look at what happens with
apply-engine.tsregarding thebackupargument passing:Assistant
Now I have enough understanding. Let me start implementing the fixes.
Step 1: Fix
OpenCodeAgent.tsAssistant
Step 2: Fix
CrushAgent.tsAssistant
Now let me run the existing tests to confirm nothing is broken:
Assistant
Now let me check the existing tests that use real fs (
CrushAgent.test.tsuses real fs, whileOpenCodeAgent.test.tsuses mocked fs). Let me also check the existing integration tests to ensure they work:Assistant
Now let me check if there are MCP tests for crush/opencode that might be affected:
Assistant
Now let me add tests for backup behaviour. First let me check the test patterns used by other agents:
Assistant
Now let me add tests to the existing
OpenCodeAgent.test.ts:Assistant
Let me look at how AmpAgent tests its backup behavior since they use spy on FileSystemUtils:
Assistant
Now let me add tests to OpenCodeAgent.test.ts to test backup behaviour:
Assistant
Let me run the tests and see if there are issues with mixing mocked and real fs:
Assistant
The issue is that jest is auto-mocking modules. Let me check the test setup:
Assistant
Let me split the backup tests into a separate file to avoid mocking complications:
Assistant
Now let me create a separate test file for backup behavior:
Assistant
Now let me run the tests to verify:
Assistant
Let me fix the test syntax:
Assistant
All OpenCodeAgent tests pass. Now let me add similar tests to the CrushAgent test file:
Assistant
Let me add backup enabled/disabled tests to the CrushAgent test file:
Assistant
Now let me add backup tests. I'll insert them within the existing "backup and revert functionality" describe block:
Assistant
Now let me add tests within the "backup and revert functionality" describe block:
Assistant
Now let me find the right place to insert these tests. I want to insert them before the closing
});of thedescribe('backup and revert functionality':Assistant
Let me use Node.js instead:
Assistant
Let me check what the actual content looks like:
Assistant
Let me use a simpler approach with awk:
Assistant
Let me write a small Node script to do the insertion:
Assistant
Let me run the tests to see how they go:
Assistant
Let me look at the failing test more carefully and fix it:
Assistant
Let me update this test to reflect the actual behavior - when an existing .crush.json exists with content and no MCP is provided, the existing servers are preserved:
Assistant
Let me use a different approach to fix the test - use a temporary file to do the replacement:
Assistant
All 19 tests pass. Now let me run the full backup-option tests as well:
Assistant
Let me run the integration test for custom paths:
Assistant
Now let me run a broader set of tests to ensure nothing is broken:
Assistant
All 753 unit tests pass! Let me also run the integration tests:
Assistant
Excellent! All 67 integration tests pass! Let me now also run the rest of the test suite:
Assistant
Let me investigate the failing test:
Assistant
Let me check if this test was failing before my changes by looking at the test code:
Assistant
Good - this is a pre-existing test failure unrelated to my changes. Let me restore my changes:
Assistant
My changes are restored. Now let me verify the changes look correct:
Assistant
Now let me also look at the test for OpenCodeAgent.backup.test.ts:
Assistant
Now let me verify all my changes work together by running the tests one more time:
Assistant
All 33 tests pass. Let me also verify that the build works correctly:
Assistant
Build is clean. Now let me also create a quick end-to-end reproduce script to verify the original issue is fixed:
Assistant
Let me investigate the opencode.json.bak issue - it seems it's because there's no MCP config:
Assistant
Excellent! Now both
AGENTS.md.bakandopencode.json.bakare created as expected when there is MCP config. Let me also clean up:Assistant
Now let me run a broader test to ensure my changes don't break anything else:
Assistant
All 971 tests pass (excluding the pre-existing skills-propagation failure which is unrelated to my changes).
Let me also run lint to ensure no style issues:
Assistant
Lint passes. Let me also check the format:
Assistant
All formatted correctly. Let me run the tests again after format:
Assistant
All 33 tests pass. Let me also verify the final state of the changes:
Assistant
Let me also check the test file final state:
Assistant
Everything looks great! Let me also test the revert functionality for OpenCode with backup enabled to ensure end-to-end correctness:
Assistant
Excellent! The end-to-end test confirms that:
- The OpenCode agent creates
.bakfiles when backup is enabled - After applying, revert correctly restores the user's original content from the backup
Let me also verify the Crush agent end-to-end:
- The OpenCode agent creates
Assistant
Both OpenCode and Crush end-to-end backup+restore work correctly.
Let me now run the full test suite one more time to verify everything is in order:
Assistant
All 971 tests pass. Let me also verify that my code is consistent with the conventions used by other agents:
Assistant
My implementation follows the same pattern as other agents:
- Uses
backup = truedefault - Calls
backupFilebefore writing whenbackupis true - Uses
ensureDirExiststo create parent directories - Resolves paths with
path.resolve(projectRoot, ...)
Let me run the lint and format check one final time:
- Uses
Assistant
Everything passes. Let me also do a final summary check:
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-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →