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.
Assistant
Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me look at how other agents properly handle the backup argument:
Assistant
Now let me look at how other agents like CursorAgent or CodexCliAgent handle backups:
Assistant
Now let me look at how agents like ZedAgent.ts or RooCodeAgent.ts handle this:
Assistant
Now let me look at other agents like CopilotAgent or AiderAgent that handle backup:
Assistant
Now let me look at MistralVibeAgent.ts since it likely has backup handling:
Assistant
Now let me check the existing tests structure:
Assistant
Now let me check the
unittests folder for unit tests:Assistant
Now let me look at the harness.ts for tests:
Assistant
Now let me look at
revert.tsto understand how the backup path is consumed:Assistant
Now I have a good understanding. Let me run existing tests for OpenCode and Crush to confirm they pass before fixing:
Assistant
Now let me check how the backup argument is normally passed:
Assistant
Now I have a complete understanding. Let me look at the IAgent.ts backup signature to understand what backup support means:
Assistant
Now I have enough context. Let me check what helpers are exported:
Assistant
Now I'll create a script to reproduce the issue first:
Assistant
The issue is reproduced - no
.bakfiles were created. Let me now verify with a config that hasbackup = false:Assistant
Now I confirmed the issue. With backup enabled (default), AGENTS.md and .crush.json were overwritten without creating .bak files. Let me reset the test setup and verify the expected behavior:
Assistant
Now let me fix the OpenCodeAgent to properly accept and use the backup parameter:
Assistant
Wait, I introduced a
requirecall inadvertently. Let me fix that properly:Assistant
Now let me update the CrushAgent:
Assistant
Let me verify the files now look correct:
Assistant
Now let me build and run the tests to verify the fix:
Assistant
Now
.bakfiles are being created when backup is enabled. Let me also verify that they have the correct content:Assistant
The backups contain the original content. Let me also test with --no-backup to confirm no backup files are created:
Assistant
With
--no-backup, no.bakfiles are created. Let me now run the existing tests to make sure I didn't break anything:Assistant
Let me check the import in FileSystemUtils:
Assistant
Let me check how the OpenCodeAgent test mocks fs:
Assistant
Let me look at AiderAgent tests to see how they handle the same pattern:
Assistant
Let me test if
fs/promisesandfs.promisesare actually the same module by running a quick verification:Assistant
Let me check the current OpenCodeAgent source more carefully:
Assistant
Let me add some debug output to understand what's happening:
Assistant
Let me check how the tests run with jest's verbose outputand find the root cause:
Assistant
Let me check the test setup. Maybe there's something in jest.setup.js:
Assistant
Let me write a quick test to verify the issue:
Assistant
Let me clean up and verify the behavior:
Assistant
Now I understand the issue. The existing OpenCodeAgent tests use
jest.mock('fs/promises')which doesn't affectfs.promisesaccessed viaimport { promises as fs } from 'fs'. Let me rewrite both files to usefs/promisesdirectly so the existing tests continue to work:Assistant
All 25 tests pass. Now let me also verify the actual functionality works:
Assistant
Backups are being created. Now let me verify the backup content is correct and that --no-backup works:
Assistant
With
--no-backup, no.bakfiles are created. Now let me also test that the revert functionality still works correctly:Assistant
Hmm, revert didn't actually revert anything. Let me check why - perhaps the gitignore fileis needed:
Assistant
The revert isn't working. Let me check the revert-engine tosee why:
Assistant
Let me look at the mainrevert function to understand what it does:
Assistant
Let me check the getAgentOutputPaths function:
Assistant
Let me check what
restoreFromBackupdoes:Assistant
Let me debug the revert:
Assistant
Let me trace through more carefully. Let me re-do the test:
Assistant
The revert works correctly now:
- AGENTS.md was restored from backup (the user's original content)
- .crush.json was restored from backup (the user's original content)
- CRUSH.md doesn't have a backup (because it didn't exist before, so no backup was created)
- The backups (.bak files) are removed after restore
Let me verify the restored content:
Assistant
Excellent! Now let me clean up and add tests for the new backup behavior. The issue says I should add tests for backup enabled/disabled behaviour:
Assistant
Now let me add unit tests for the OpenCodeAgent and CrushAgent backup behavior:
Assistant
Looking at the system prompt again: I should not add new tests to the repository. Let me skip adding new tests and instead verify my fix works using temporary scripts. Let me run all existing tests to make sure nothing is broken:
Assistant
All 362 tests in the agents folder pass. Let me also run the backup-related tests:
Assistant
All tests pass. Now let me think about edge cases:
Assistant
Let me create a temporary script to verify the fix works correctly for both agents:
Assistant
All scenarios pass. Let me check the rest of the output to confirm:
Assistant
All tests pass:
- Test 1: With backup enabled (default),
.bakfiles are created for both AGENTS.md and .crush.json - Test 2: With
--no-backup, no.bakfiles are created - Test 3: Revert properly restores original content from the
.bakfiles - Test 4: Backup files contain the correct original content
Let me also test edge cases:
- Test 1: With backup enabled (default),
Assistant
Let me debug the edge case manually:
Assistant
Let me check how the apply-engine handles the output paths:
Assistant
The
getAgentOutputPathsfunction usesagentConfig?.outputPath ?? agentConfig?.outputPathInstructions ?? defaultPaths.instructions. But the verbose log shows the default paths are being used, not the custom ones.Let me checkif the agent config is being properly loaded. Let me look at how config.agentConfigs is populated:
Assistant
Let me check how config.agentConfigs is loaded:
Assistant
Let me look at apply-engine more carefully:
Assistant
Let me check what's happening - the verbose log says default paths but the file is being written to custom paths. Let me add some debug logging:
Assistant
Let me check the ConfigLoader to understand why the config isn't being parsed correctly:
Assistant
The config keys use
output_path_instructions(snake_case in TOML), notoutputPathInstructions! Let me check:Assistant
So the config uses
output_path_instructionsin TOML. Let me fix my test:Assistant
I see - the OpenCodeAgent only writes MCP config if there are MCP servers. Let me update the test to include MCP servers for OpenCode:
Assistant
Let me fix the test - the original config needs to be valid JSON:
Assistant
All edge cases pass. Let me clean up and run the full test suite one more time:
Assistant
The failing test is unrelated to my changes. Let me verify by checking git status:
Assistant
Confirmed - the skills-propagation test failure is pre-existing, not caused by my changes. Let me restore my changes:
Assistant
The pre-existing failure is unchanged. Now let me view my final changes:
Assistant
Let me check if AiderAgent (which uses
backupFilefrom FileSystemUtils) has tests that mock fs/promises:Assistant
Let me run all agent tests one more time to confirm:
Assistant
Only the pre-existing test failure (skills-propagation) is failing. Let me runthe tests/integration/opencode-agent.test.ts to see if there's any specific test for OpenCodeAgent integration:
Assistant
Integration tests for OpenCode pass. Let me also make sure the Crush agent integration test exists and passes:
Assistant
All tests pass. Let me verify by running tests one more time:
Assistant
All 449 tests pass. Let me also run a final reproduction test to make sure the issue is fixed:
Assistant
The fix works perfectly:
- Before apply: User has their own AGENTS.md and .crush.json
- After apply:
- AGENTS.md.bak contains user's original content
- .crush.json.bak contains user's original content
- AGENTS.md now has Ruler rules
- .crush.json has Ruler config (merged with existing)
- After revert: Files are restored from backup
Let me clean up the test script and review the final state of the fix:
Assistant
Let me reconsider the design - maybe I should check if there's a cleaner approach. Let me look at how AiderAgent uses backupFile:
Assistant
Let me do one final comprehensive test run:
Assistant
All 449 tests pass. Now let me run the full test suite one more time to confirm only the pre-existing failure remains:
Assistant
Only 1 pre-existing test failure (skills-propagation.test.ts) is present, which is unrelated to my changes (as confirmed earlier).
Now let me go through the checklist:
Checklist
-
If an error was reported in the issue description, I have successfully reproduced it.
- YES: I created a test scenario where AGENTS.md and .crush.json already existed, ran
ruler apply, and confirmed no.bakfiles were created.
- YES: I created a test scenario where AGENTS.md and .crush.json already existed, ran
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- YES: After my fix, applying with backup enabled creates files containing the original user content. Reverting properly restores the original content from these backups.
-
Assistant
All the changes look good. Let me do a final summary of what I've done:
Changes to
src/agents/OpenCodeAgent.ts:- Added
backup = trueparameter toapplyRulerConfig - Added
backupFileprivate method (mirrors behavior ofFileSystemUtils.backupFile) - Called
backupFilebefore writing bothAGENTS.mdandopencode.json - Path resolution against
projectRootwas already in place - Parent directory creation was already in place
Changes to
src/agents/CrushAgent.ts:- Added
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.