From 5b18619730dba18a8de0021d1010a521126b2c32 Mon Sep 17 00:00:00 2001 From: YashpalLohan Date: Wed, 3 Jun 2026 02:34:55 +0530 Subject: [PATCH] fix(compilation): raise clear error when managed_section file is missing --- src/apm_cli/compilation/agents_compiler.py | 6 ++++- .../unit/compilation/test_managed_section.py | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/apm_cli/compilation/agents_compiler.py b/src/apm_cli/compilation/agents_compiler.py index 35986c63b..963ab3259 100644 --- a/src/apm_cli/compilation/agents_compiler.py +++ b/src/apm_cli/compilation/agents_compiler.py @@ -1249,8 +1249,12 @@ def _write_output_file_with_config( if config.agents_md_mode == "managed_section": target = Path(output_path) - existing = target.read_text(encoding="utf-8") if target.exists() else "" try: + if not target.exists(): + raise ManagedSectionError( + f"{target.name} does not exist yet. Create it with markers first, or use mode: full for initial generation." + ) + existing = target.read_text(encoding="utf-8") content = apply_managed_section( existing, content, diff --git a/tests/unit/compilation/test_managed_section.py b/tests/unit/compilation/test_managed_section.py index 5704a66b6..cc269cab7 100644 --- a/tests/unit/compilation/test_managed_section.py +++ b/tests/unit/compilation/test_managed_section.py @@ -324,3 +324,27 @@ def test_write_reraise_uses_bracket_format(self, tmp_path): # filename must be wrapped in square brackets: [AGENTS.md] ... assert msg.startswith("[") assert "] " in msg + + def test_write_output_file_managed_section_missing_file(self, tmp_path): + """When mode=managed_section and the output file is missing, error is raised with a clear message.""" + from apm_cli.compilation.agents_compiler import AgentsCompiler, CompilationConfig + from apm_cli.compilation.managed_section import ManagedSectionError + + output_file = tmp_path / "AGENTS.md" + # Ensure it does not exist + if output_file.exists(): + output_file.unlink() + + config = CompilationConfig( + output_path=str(output_file), + agents_md_mode="managed_section", + dry_run=False, + ) + + compiler = AgentsCompiler(str(tmp_path)) + with pytest.raises(ManagedSectionError) as exc_info: + compiler._write_output_file_with_config(str(output_file), "New content.\n", config) + + msg = str(exc_info.value) + assert "AGENTS.md does not exist yet. Create it with markers first, or use mode: full for initial generation." in msg + assert msg.startswith("[")