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.
Python agent-evaluationaiai-alignmentai-evaluationai-governanceai-safetyclidiagnostic-tooleu-ai-acthallucination-detectioniso-42001llm-evaluationllm-securitynist-ai-rmfowasp-llmprompt-injectionpythonresponsible-airisk-assessmentrisk-management
fix(scaffold): retain unmanaged AGENTS.md content (#212)
Rudy Mizrahi Celeklid5cc936af9e2
diff --git a/ifixai/cli/scaffold.py b/ifixai/cli/scaffold.pyindex 68c7447..0072822 100644--- a/ifixai/cli/scaffold.py+++ b/ifixai/cli/scaffold.py@@ -144,9 +144,9 @@ def _splice_agents_md(existing: str, body: str) -> str:if has_begin and has_end:pre = existing.split(MD_BEGIN, 1)[0]post = existing.split(MD_END, 1)[1]- return f"{pre.rstrip()}\n\n{block}\n{post.lstrip()}".strip() + "\n"- sep = "" if not existing.strip() else existing.rstrip() + "\n\n"- return f"{sep}{block}\n"+ return pre + block + post+ separator = "" if not existing or existing.endswith("\n\n") else "\n" if existing.endswith("\n") else "\n\n"+ return existing + separator + block + "\n"def _strip_agents_md(existing: str) -> str:@@ -154,7 +154,11 @@ def _strip_agents_md(existing: str) -> str:return existingpre = existing.split(MD_BEGIN, 1)[0]post = existing.split(MD_END, 1)[1]- return (pre.rstrip() + "\n" + post.lstrip()).strip() + "\n" if (pre.strip() or post.strip()) else ""+ if post.startswith("\r\n"):+ post = post[2:]+ elif post.startswith("\n"):+ post = post[1:]+ return pre + postdef _resolve_agents(agents: str | None, project_root: Path, name: str) -> list[AgentTarget]:@@ -233,8 +237,13 @@ def install(agents: str | None, name: str, project_dir: str, revert: bool, list_path.parent.mkdir(parents=True, exist_ok=True)if t.fmt == "agents_md":- existing = path.read_text(encoding="utf-8") if path.is_file() else ""- path.write_text(_splice_agents_md(existing, body), encoding="utf-8")+ existing = ""+ if path.is_file():+ with path.open(encoding="utf-8", newline="") as handle:+ existing = handle.read()+ updated = _splice_agents_md(existing, body)+ with path.open("w", encoding="utf-8", newline="") as handle:+ handle.write(updated)click.echo(f" {click.style('updated', fg='green')} {t.label}: {path}")written += 1continue@@ -266,9 +275,12 @@ def _do_revert(targets: list[AgentTarget], project_root: Path, name: str) -> Nontext = path.read_text(encoding="utf-8")if t.fmt == "agents_md":+ with path.open(encoding="utf-8", newline="") as handle:+ text = handle.read()stripped = _strip_agents_md(text)- if stripped.strip():- path.write_text(stripped, encoding="utf-8")+ if stripped:+ with path.open("w", encoding="utf-8", newline="") as handle:+ handle.write(stripped)else:path.unlink()click.echo(f" {click.style('reverted', fg='yellow')} {t.label}: {path}")diff --git a/ifixai/tests/test_scaffold_content.py b/ifixai/tests/test_scaffold_content.pynew file mode 100644index 0000000..4e09879--- /dev/null+++ b/ifixai/tests/test_scaffold_content.py@@ -0,0 +1,63 @@+"""The public installer must retain unmanaged AGENTS.md text."""++import pytest+from click.testing import CliRunner++from ifixai.cli.main import ifixai_cli+from ifixai.cli.scaffold import MD_BEGIN, MD_END+++def _install(tmp_path, *extra):+ result = CliRunner().invoke(+ ifixai_cli, ["install", "--agents", "agents", "--dir", str(tmp_path), *extra]+ )+ assert result.exit_code == 0, result.output++[email protected]("newline", ["\n", "\r\n"])+def test_install_keeps_existing_user_text(tmp_path, newline):+ path = tmp_path / "AGENTS.md"+ original = (" # Owned instructions" + newline + newline+ + "Keep this Markdown hard break. " + newline * 3)+ path.write_bytes(original.encode())+ _install(tmp_path)+ assert path.read_bytes().startswith(original.encode())++[email protected]("newline", ["\n", "\r\n"])[email protected]("revert", [False, True])+def test_replace_or_revert_keeps_unmanaged_prefix_and_suffix(tmp_path, newline, revert):+ path = tmp_path / "AGENTS.md"+ prefix = " # Owned instructions " + newline * 3+ suffix = newline + ' echo "this remains indented code"' + newline * 2+ path.write_bytes((prefix + MD_BEGIN + "\nold body\n" + MD_END + "\n" + suffix).encode())+ _install(tmp_path, *(["--revert"] if revert else []))+ contents = path.read_bytes()+ assert contents.startswith(prefix.encode())+ assert contents.endswith(suffix.encode())+ if revert:+ assert contents == (prefix + suffix).encode()+ else:+ assert "old body" not in contents.decode()+ assert contents.count(MD_BEGIN.encode()) == 1+++def test_install_is_idempotent_and_revert_removes_generated_file(tmp_path):+ path = tmp_path / "AGENTS.md"+ _install(tmp_path)+ first = path.read_bytes()+ _install(tmp_path)+ assert path.read_bytes() == first+ _install(tmp_path, "--revert")+ assert not path.exists()+++def test_unbalanced_managed_marker_keeps_owned_file(tmp_path):+ path = tmp_path / "AGENTS.md"+ original = "# Owned\n" + MD_BEGIN + "\nIncomplete\n"+ path.write_text(original, encoding="utf-8")+ result = CliRunner().invoke(+ ifixai_cli, ["install", "--agents", "agents", "--dir", str(tmp_path)]+ )+ assert result.exit_code != 0+ assert path.read_text(encoding="utf-8") == original