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 what we're working with.
Assistant
Let me look at the AbstractAgent.ts to understand the pattern of how agents use backupFile:
Assistant
Let me look at existing tests to understand the patterns:
Assistant
Now let me check the existing backup-option.test.ts to understandthe conventions for backup tests:
Assistant
Now let me look at the AgentBackupForwarding.test.ts to understand the pattern for theagent-level tests:
Assistant
Let me look at the test harness to understand setupTestProject:
Assistant
Let me also check the GeminiCliAgent.ts to see what an already-working agent with custom applyRulerConfig looks like:
Assistant
Let me check how OpenCode and Crush propagate in the apply-engine.ts and revert.ts:
Assistant
Now I understand the entire structure. Let me also check the AbstractAgent test to confirm patterns:
Assistant
Let me look at the apply-engine.ts file to understand how the apply function uses backup:
Assistant
Let me also check
RooCodeAgent.tsto understand the patterns for an agent with multiple files:Assistant
Let me also check the AmazonQCliAgent.ts as another example:
Assistant
Now let me look at the related test file to confirm:
Assistant
Now let me check the existing build:
Assistant
Let me reproduce the issue first by creating a test script:
Assistant
I've reproduced the issue. The
.bakfiles are not created forAGENTS.md(OpenCode's instructions),CRUSH.md, and.crush.json. Now let me look at the existing tests for OpenCodeAgent more carefully to understand the patterns:Assistant
Now I have enough understanding. Let me now fix the
OpenCodeAgent.ts:Assistant
Now let me fix the
CrushAgent.ts:Assistant
Let me build to ensure no compile errors:
Assistant
The fix works. Now let me verify with
--no-backupflag:Assistant
Both
--backupand--no-backupwork correctly now. Let me run the existing tests:Assistant
Let me check what tests are failing in OpenCodeAgent.test.ts:
Assistant
The tests are failing because
writeGeneratedFileuses'fs/promises'which the test mocks. Let me check this issue:Assistant
Let me check the AmazonQCliAgent test to see how the mock works there:
Assistant
Let me run a simple debug to understand the mock:
Assistant
Let me check if the mock actually applies when
writeGeneratedFileis used. Looking at AmazonQCliAgent which also useswriteGeneratedFile:Assistant
Let me look at the dist to understand if there's some compilation issue:
Assistant
I see! The dist uses
require("fs")notrequire("fs/promises"). So even though the source usesimport { promises as fs } from 'fs';, when compiled, it becomesconst fs_1 = require("fs");and thenfs_1.promises.writeFile(...). Thejest.mock('fs/promises')mock doesn't interceptrequire('fs')calls.But wait - I'm looking at the test. The tests use
import * as fs from 'fs/promises';. Let me check the Jest config to see how it'sset up:Assistant
Let me check the test more carefully - it uses
fs/promises:Assistant
Let me check if the tests pass for old behavior, by reverting my changes temporarily:
Assistant
Let me check how the existing test approach works in
AgentBackupForwarding.test.ts:Assistant
Let me check the WindsurfAgent.test.ts:
Assistant
Now let me understand - the existing OpenCodeAgent tests will fail. Let me verify they were designed for the previous behavior. The issue is the tests use
jest.mock('fs/promises')but the agent now usesFileSystemUtilshelpers. According to the instructions, I shouldn't modify existing tests but they can break. Let me create a new test file for the backup behavior. First, let me check the existingAgentBackupForwarding.test.tsonce more to understand its setup:Assistant
Let me check if my code is correct by examining the actual node_modules path Jest uses for
fs/promises:Assistant
Let me check the CrushAgent tests:
Assistant
The CrushAgent tests use real fs, so they should still work. Let me run the CrushAgent tests:
Assistant
The CrushAgent tests pass. Now I need to handle OpenCodeAgent tests. Let me revise OpenCodeAgent to use
fs.writeFiledirectly so the existing tests still work:Assistant
Let me also verify the CrushAgent still works:
Assistant
Let me verify the fix still works correctly:
Assistant
Now let me run all the unit agent tests to make sure nothing else broke:
Assistant
All 362 agent tests pass. Let me run all tests:
Assistant
Let me check if this failing test is related to my changes:
Assistant
Good - that test failure is pre-existing and not related to my fix. Let me create new tests for the backup behavior:
Assistant
Let me fix the property naming issue:
Assistant
The issue is that when
rulerMcpJsonis null, the OpenCodeAgent returns early before writing the MCP file. This is by design (the engine handles opencode.json MCP separately). Let me adjust the test to pass a non-null rulerMcpJson:Assistant
All new tests pass. Let me run the full test suite to verify nothing else broke:
Assistant
The only failing test is the pre-existing failure that was already failing before my changes. Let me run the backup-related tests:
Assistant
Let me also look at the existing
mcp-backup-prevention.test.tsto see how backup integration is tested:Assistant
Let me also create an integration test for the OpenCode and Crush backup behavior through the full apply flow:
Assistant
Let me also extend the integration tests by creating a new file. First let me check the integration test patterns:
Assistant
Let me run all the tests once more to confirm everything works:
Assistant
Let me run the OpenCode integration test to make sure nothing broke:
Assistant
Let me look at my final agent files to verify everything is correct:
Assistant
Let me also check the existing tests pass (besides the pre-existing skills propagation failure):
Assistant
Let me see the full diff of changes:
Assistant
Let me also verify the new test file:
Assistant
Let me run the lint check:
Assistant
Let me also run the e2e tests:
Assistant
All e2e tests pass too. Let me run the full test suite to verify:
Assistant
Let me confirm the failure is pre-existing:
Assistant
Let me clean up the temporary test scripts:
Assistant
Let me do a final verification of my changes:
Assistant
Let me verify the final dist build:
Assistant
Let me run a comprehensive check of all the related tests one more time:
Assistant
All 50 related tests pass. Let me run a final lint and format check:
Assistant
Let me also verify the final git status:
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 →