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: reject malformed scaffold marker regions before modifying files (#245)
Rudy Mizrahi Celeklicf4384d71829
diff --git a/ifixai/cli/scaffold.py b/ifixai/cli/scaffold.pyindex 0072822..3295b88 100644--- a/ifixai/cli/scaffold.py+++ b/ifixai/cli/scaffold.py@@ -131,9 +131,21 @@ def _is_managed(text: str, target: AgentTarget) -> bool:return begin in text+def _validate_agents_markers(existing: str) -> None:+ begin_count, end_count = existing.count(MD_BEGIN), existing.count(MD_END)+ if not begin_count and not end_count:+ return+ if begin_count != 1 or end_count != 1 or existing.index(MD_BEGIN) > existing.index(MD_END):+ raise click.ClickException(+ "AGENTS.md has an invalid iFixAi marker block; expected one BEGIN before one END. "+ "Fix or remove the markers by hand, then re-run."+ )++def _splice_agents_md(existing: str, body: str) -> str:"""Insert or replace our marker block inside an existing AGENTS.md, leaving therest of the user's file untouched."""+ _validate_agents_markers(existing)has_begin, has_end = MD_BEGIN in existing, MD_END in existingif has_begin != has_end:raise click.ClickException(@@ -150,6 +162,7 @@ def _splice_agents_md(existing: str, body: str) -> str:def _strip_agents_md(existing: str) -> str:+ _validate_agents_markers(existing)if MD_BEGIN not in existing or MD_END not in existing:return existingpre = existing.split(MD_BEGIN, 1)[0]diff --git a/ifixai/tests/test_scaffold_marker_order.py b/ifixai/tests/test_scaffold_marker_order.pynew file mode 100644index 0000000..5429cbb--- /dev/null+++ b/ifixai/tests/test_scaffold_marker_order.py@@ -0,0 +1,18 @@+import click+import pytest++from ifixai.cli.scaffold import MD_BEGIN, MD_END, _splice_agents_md, _strip_agents_md++[email protected]('operation', [_splice_agents_md, _strip_agents_md])[email protected]('text', [MD_END + '\nkeep this\n' + MD_BEGIN, MD_BEGIN + MD_BEGIN + MD_END, MD_BEGIN + MD_END + MD_END])+def test_malformed_managed_regions_are_rejected(operation, text):+ with pytest.raises(click.ClickException, match='marker'):+ operation(text, 'replacement') if operation is _splice_agents_md else operation(text)+++def test_valid_region_preserves_surrounding_content():+ text = 'before\n' + MD_BEGIN + '\nold\n' + MD_END + '\nafter\n'+ updated = _splice_agents_md(text, 'new')+ assert updated == 'before\n' + MD_BEGIN + '\nnew\n' + MD_END + '\nafter\n'+ assert _strip_agents_md(updated) == 'before\nafter\n'