Skip to content

Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.

fix(scaffold): preserve custom commands when backups conflict (#308)

Rudy Mizrahi Celekli84ae26755f4b
diff --git a/ifixai/cli/scaffold.py b/ifixai/cli/scaffold.py
index 3295b88..86afb09 100644
--- a/ifixai/cli/scaffold.py
+++ b/ifixai/cli/scaffold.py
@@ -266,6 +266,13 @@ def install(agents: str | None, name: str, project_dir: str, revert: bool, list_
if not _is_managed(existing, t) and not force:
bak = path.with_suffix(path.suffix + ".bak")
if bak.exists():
+ if not bak.is_file() or bak.read_text(encoding="utf-8") != existing:
+ raise click.ClickException(
+ f"Cannot back up {path}: {bak} already exists and "
+ "does not match the current custom command. Move "
+ "the backup aside before installing, or use --force "
+ "to explicitly overwrite without saving this command."
+ )
click.echo(f" {click.style('kept', fg='yellow')} existing backup {bak.name} (not overwritten)")
else:
bak.write_text(existing, encoding="utf-8")
diff --git a/ifixai/tests/test_scaffold_backup_collision.py b/ifixai/tests/test_scaffold_backup_collision.py
new file mode 100644
index 0000000..a0a9aaf
--- /dev/null
+++ b/ifixai/tests/test_scaffold_backup_collision.py
@@ -0,0 +1,61 @@
+"""A stale installer backup must not hide loss of a newer custom command."""
+
+import pytest
+from click.testing import CliRunner
+
+from ifixai.cli.main import ifixai_cli
+
+
+def _install(root, agent, *extra):
+ return CliRunner().invoke(ifixai_cli, ["install", "--agents", agent, "--dir", str(root), *extra])
+
+
[email protected]("agent,relative", [("claude", ".claude/commands/ifixai-skill.md"), ("gemini", ".gemini/commands/ifixai-skill.toml")])
+def test_reinstall_preserves_new_custom_command_when_backup_is_stale(tmp_path, agent, relative):
+ path = tmp_path / relative
+ path.parent.mkdir(parents=True)
+ original = "Original custom instructions.\n"
+ newest = "New organization-specific audit instructions.\n"
+ path.write_text(original)
+ assert _install(tmp_path, agent).exit_code == 0
+ backup = path.with_suffix(path.suffix + ".bak")
+ assert backup.read_text() == original
+ # The user replaces the generated command between installs. Its old backup
+ # still exists, but cannot recover this new custom command if overwritten.
+ path.write_text(newest)
+ result = _install(tmp_path, agent)
+ assert result.exit_code != 0, result.output
+ assert "backup" in result.output.lower()
+ assert path.read_text() == newest
+ assert backup.read_text() == original
+
+
[email protected]("preexisting_backup", [False, True])
+def test_matching_backup_and_managed_reinstall_remain_revertible(tmp_path, preexisting_backup):
+ path = tmp_path / ".claude/commands/ifixai-skill.md"
+ path.parent.mkdir(parents=True)
+ original = "Custom audit instructions.\n"
+ path.write_text(original)
+ backup = path.with_suffix(".md.bak")
+ if preexisting_backup:
+ backup.write_text(original)
+ assert _install(tmp_path, "claude").exit_code == 0
+ first = path.read_text()
+ assert _install(tmp_path, "claude").exit_code == 0
+ assert path.read_text() == first
+ assert backup.read_text() == original
+ assert _install(tmp_path, "claude", "--revert").exit_code == 0
+ assert path.read_text() == original
+ assert not backup.exists()
+
+
+def test_explicit_force_retains_its_overwrite_behavior(tmp_path):
+ path = tmp_path / ".claude/commands/ifixai-skill.md"
+ path.parent.mkdir(parents=True)
+ path.write_text("Current custom command.\n")
+ backup = path.with_suffix(".md.bak")
+ backup.write_text("Older command.\n")
+ result = _install(tmp_path, "claude", "--force")
+ assert result.exit_code == 0, result.output
+ assert "managed by `ifixai install`" in path.read_text()
+ assert backup.read_text() == "Older command.\n"