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.
Assistant
Let me start by understanding the repository structure and the issue at hand. The issue is about respecting backups for OpenCode and Crush generated files. I need to:
- First explore the repository structure
- Find the relevant files mentioned:
src/agents/OpenCodeAgent.tsandsrc/agents/CrushAgent.ts - Understand the
backupFilefunction and how it's used elsewhere - Fix the backup parameter handling in both agent implementations
- Test the changes
Let me start by exploring the repository.
Assistant
Let me explore the source directory structure and find the files mentioned in the issue.
Assistant
Let me open the two files mentioned in the issue.
Assistant
The open command doesn't take a --file parameter. Let me check the syntax.
I need to use:
open [OPTIONS] [FILE] [LINE_NUMBER]So the syntax should be just
open /ruler/src/agents/OpenCodeAgent.tswithout the --file flag.Assistant
Let me try using bash to cat the file instead.
Assistant
Now let me look at the CrushAgent.ts file.
Assistant
Now I need to understand how other agents handle backups. Let me look at the
backupFilefunction and how it's used in other agents.Assistant
Let me look at the
backupFilefunction and understand how it's used.Assistant
Now I understand the
backupFilefunction. Let me also look at an agent that correctly implements backup to understand the pattern.Assistant
Now let me also check the
IAgentinterface to understand the signature ofapplyRulerConfig.Assistant
I can see that the
IAgentinterface already definesapplyRulerConfigwith abackup?: booleanparameter. Now let me look at how other agents that handle multiple files implement backup. Let me checkAiderAgent.tssince it was mentioned in the grep results.Assistant
Now let me also look at the
FirebenderAgent.tssince it also handles multiple output paths with backups:Assistant
Now let me look at the apply-engine.ts to understand how
backupis passed to agents:Assistant
Now let me also look at the existing tests to understand the test patterns used:
Assistant
Let me look at the backup-option.test.ts to understand the test patterns for backup testing:
Assistant
Let me also look at the test harness to understand how tests are structured:
Assistant
Now I have a good understanding of the codebase. Let me now look at how the
backupparameter is passed from the apply engine to the agent implementations:Assistant
Good, so the apply engine is already passing the
backupparameter toapplyRulerConfig. Now let me understand the issue:OpenCodeAgent.tsdoesn't accept or use thebackupparameterCrushAgent.tsdoesn't accept or use thebackupparameter
Both agents write files directly without calling
backupFilebefore overwriting.Now let me create a reproduction script to confirm the issue first:
Assistant
I've confirmed the issue. Now let me fix both agents. Let me start with
OpenCodeAgent.ts:Assistant
Now I'll fix the
OpenCodeAgent.tsfile to:- Accept the
backupparameter - Import
backupFileandensureDirExists - Call
backupFilebefore writes when backup is enabled
- Accept the
Assistant
Now let me fix the
CrushAgent.tsfile similarly:Assistant
Now I need to rebuild the project and test the fix:
Assistant
Build succeeded. Now let me run my reproduction script again to confirm the fix:
Assistant
The basic fix works. Now let me also test the
--no-backupflag to make sure it doesn't create backups when disabled:Assistant
Both backup and no-backup scenarios work correctly. Now let me also verify that the MCP config files (
.crush.jsonandopencode.json) are backed up when they exist:Assistant
Interesting. The OpenCode MCP config backup failed. Let me investigate why. The issue is that the MCP configuration for OpenCode might be handled separately by the apply engine (not by the agent itself). Let me check how MCP is propagated for OpenCode:
Assistant
I see now. For OpenCode, the MCP configuration is handled by
applyOpenCodeMcpConfigurationin the apply engine. Let me check that function:Assistant
I see that OpenCode MCP config is handled by
propagateMcpToOpenCode. Let me check that function:Assistant
The
propagateMcpToOpenCodefunction already handles backups correctly. The issue is that the test runs the apply command without MCP servers defined, so the MCP path isn't triggered. Let me update my test to include MCP configuration:Actually, looking at the code more carefully, I see that the
shouldUseEngineManagedMcpfunction returnstruefor OpenCode, which means when MCP config exists, it bypasses the agent's internal MCP handling and uses the engine's managed MCP propagation. But when no MCP config exists, the agent still writes an empty MCP config. Let me trace through the flow again:Assistant
I see now! For OpenCode,
mcpForAgentApplyis set tonullbecauseshouldUseEngineManagedMcpreturnstruefor it. So the agent'sapplyRulerConfigreceivesnullfor MCP and the MCP is handled separately byhandleMcpConfiguration.Looking at my test, I didn't configure any MCP servers, so
propagateMcpToOpenCodewon't be called. The OpenCode agent itself writesopencode.jsonin itsapplyRulerConfigwhenrulerMcpJsonis not null, but since the engine passesnull, the agent doesn't write the MCP config file. The MCP file is only written when there are MCP servers defined.So the behavior is correct for the OpenCode MCP file - it's handled by the engine's
propagateMcpToOpenCodewhich already supports backups. Let me update my test to verify this correctly:Assistant
Let me debug this more:
Assistant
I notice there's no
opencode.json.bakfile. Let me check the.gitignoreto see what paths are being tracked:Assistant
I see! The verbose output says "No compatible MCP servers found for OpenCode - skipping MCP configuration". This means the MCP server defined in the TOML isn't being recognized as compatible. Let me investigate:
Assistant
The issue is that my test MCP server config uses
type = "stdio"but maybe OpenCode doesn't support stdio. Let me check thefilterMcpConfigForAgentfunction:Assistant
I see - the issue is that the MCP server config in TOML is parsed and the
commandkey needs to be present for stdio servers. The test definescommand = "test"but it's probably being loaded as an array. Let me trace through the actual issue - the MCP configuration is loaded differently. But actually, the important thing to test is whether my fix to the agents works correctly. The MCP backup is handled separately and correctly by the engine.Let me clean up and run the existing tests to make sure I haven't broken anything:
Assistant
All 14 backup tests pass. The coverage warnings are expected (running just one test file). Now let me also run all the tests to make sure I haven't broken anything:
Assistant
There's one pre-existing test failure in
skills-propagation.test.tsthat is not related to my changes. Let me verify by checking if this test was failing before my changes:Assistant
The test failure is pre-existing (not caused by my changes). Let me restore my changes and clean up the test files I created:
Assistant
Let me clean up the test scripts I created:
Assistant
Now let me verify my changes one more time by looking at the diff:
Assistant
My changes look correct. Let me also run the tests one more time to make sure everything is working:
Assistant
All backup tests pass. Let me verify the build compiles correctly:
Assistant
} }
/**
- Returns whether this agent has native subagent support (like Claude Code,
- Cursor, Codex CLI, GitHub Copilot). When true, subagent definitions from
.ruler/agents/are propagated to the agent's native subagent location.- Defaults to false if not implemented. */ supportsNativeSubagents?(): boolean; }
Assistant
I need to rebuild the project to check for errors properly.
Assistant
The build succeeded (the grep command returned exit code 1 because there were no "error" strings in the output - which means no compilation errors).
Let me now verify the final state of my changes:
Assistant
Let me now verify my changes by reviewing the final state of both modified files to ensure they are correct before submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.