Skip to content

Fix managed_section ignored in distributed and single-agents compile paths#1768

Open
sergio-sisternes-epam wants to merge 1 commit into
mainfrom
sergio-sisternes-epam/fix-managed-section-distributed-compile
Open

Fix managed_section ignored in distributed and single-agents compile paths#1768
sergio-sisternes-epam wants to merge 1 commit into
mainfrom
sergio-sisternes-epam/fix-managed-section-distributed-compile

Conversation

@sergio-sisternes-epam

Copy link
Copy Markdown
Collaborator

Description

apply_managed_section() exists and works correctly, but neither the distributed compilation path (default since Task 7) nor the --single-agents CLI path routes through it. Users who configure mode: managed_section in apm.yml experience silent full-file overwrite of AGENTS.md instead of selective marker-bounded replacement -- erasing hand-written content without warning.

The fix routes root AGENTS.md writes through _write_output_file_with_config() when agents_md_mode is managed_section, so the existing apply_managed_section() logic is honoured. Sub-directory AGENTS.md files in distributed mode remain fully overwritten since they are entirely APM-generated.

Fixes #1764

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

Changes

_write_distributed_file (agents_compiler.py): Before writing, checks whether the target is the root AGENTS.md (agents_path.parent == self.base_dir) and agents_md_mode == "managed_section". If both hold, delegates to _write_output_file_with_config() which preserves content outside the markers. Sub-directory files skip this check and remain fully overwritten.

--single-agents CLI path (commands/compile/cli.py): After constitution injection, checks config.agents_md_mode and routes through compiler._write_output_file_with_config() instead of direct CompiledOutputWriter.write(). Also catches ValueError (parent of ManagedSectionError) for clean error reporting.

Regression tests (test_managed_section.py): 3 new tests:

  • Root AGENTS.md preserves human content with managed_section mode
  • Sub-directory AGENTS.md is fully overwritten even with managed_section
  • Root AGENTS.md is fully overwritten when mode is full (negative test)

Spec conformance (OpenAPM v0.1)

  • N/A -- this PR does not change OpenAPM-observable behaviour.

Route root AGENTS.md writes through _write_output_file_with_config()
when agents_md_mode is managed_section, so hand-written content outside
the APM markers is preserved instead of silently overwritten.

- _write_distributed_file: check if agents_path is the root AGENTS.md
  and config is managed_section before choosing the write path.
  Sub-directory files remain fully overwritten (APM-generated).
- --single-agents CLI path: after constitution injection, honour
  managed_section via _write_output_file_with_config instead of
  direct CompiledOutputWriter.write.
- Add 3 regression tests covering distributed+managed_section (root
  preserved, non-root overwritten, full mode overwritten).

Fixes #1764

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 13, 2026 14:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a regression where compilation.agents_md.mode: managed_section was ignored in the active compilation paths (distributed default and --single-agents), causing silent full overwrite of AGENTS.md instead of marker-bounded replacement.

Changes:

  • Route distributed compilation root AGENTS.md writes through _write_output_file_with_config() when agents_md_mode == "managed_section".
  • Route the --single-agents CLI write path through _write_output_file_with_config() when managed_section is configured, and broaden error handling.
  • Add unit regression tests covering root vs subdirectory behavior for managed_section and full modes.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/apm_cli/compilation/agents_compiler.py Adds a root-file-only managed-section write path in distributed compilation.
src/apm_cli/commands/compile/cli.py Ensures --single-agents honors managed-section mode when writing final output.
tests/unit/compilation/test_managed_section.py Adds regression tests for distributed root/subdir semantics and full overwrite behavior.

Comment on lines +1556 to +1561
# Honour managed_section mode for the root AGENTS.md (issue #1764).
# Sub-directory files are fully APM-generated and always overwritten.
is_root = agents_path.parent.resolve() == self.base_dir.resolve()
if is_root and config.agents_md_mode == "managed_section":
self._write_output_file_with_config(str(agents_path), final_content, config)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] managed_section ignored when compilation.strategy=distributed (default)

2 participants