Skip to content

fix: apply compilation.exclude patterns during primitive discovery - #476

Closed
Alejandro Colomina (Coolomina) wants to merge 1 commit into
microsoft:mainfrom
Coolomina:fix/exclude-primitive-discovery
Closed

fix: apply compilation.exclude patterns during primitive discovery#476
Alejandro Colomina (Coolomina) wants to merge 1 commit into
microsoft:mainfrom
Coolomina:fix/exclude-primitive-discovery

Conversation

@Coolomina

Copy link
Copy Markdown
Contributor

Description

Propagate compilation.exclude patterns to the primitive discovery phase so that .instructions.md files inside excluded directories are filtered out before compilation.

Previously, exclude patterns were only applied during context optimization (directory scanning and placement), but not during discovery. This caused files in excluded directories (e.g., docs/**) to leak into compiled AGENTS.md and CLAUDE.md output.

Fixes#475

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

  • discovery.py: Add exclude_patterns parameter to discover_primitives(), discover_primitives_with_dependencies(), and scan_local_primitives(). Add _matches_exclude_patterns() helper that supports dir/**, **/dir/**, and fnmatch-style glob patterns.
  • agents_compiler.py: Pass config.exclude to discovery functions.
  • test_agents_compiler_coverage.py: Update mock assertion to match new call signature.

Verification

# Create test project with docs/ excluded
mkdir -p /tmp/repro/.apm/instructions /tmp/repro/docs/labs/.github/instructions /tmp/repro/src
cat > /tmp/repro/apm.yml << 'YAML'---name: reproversion: 1.0.0compilation: exclude: - "docs/**"YAML
cat > /tmp/repro/.apm/instructions/general.instructions.md << 'MD'---description: General rulesapplyTo: "**"---# General RulesMD
cat > /tmp/repro/docs/labs/.github/instructions/leaked.instructions.md << 'MD'---description: Should not appearapplyTo: "**/*.ts"---# LeakedMD# Before fix: 2 instruction patterns (leaked.instructions.md included)# After fix: 1 instruction pattern (only general.instructions.md)cd /tmp/repro && apm compile --dry-run

Unit tests: 3084 passed (0 failures).

CopilotAI 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 compilation pipeline bug by applying compilation.exclude patterns during primitive discovery, preventing .instructions.md files inside excluded directories from being discovered and included in compiled outputs.

Changes:

  • Extend primitive discovery APIs to accept exclude_patterns and filter discovered files accordingly.
  • Pass CompilationConfig.exclude from AgentsCompiler into discovery (local-only and dependency-aware paths).
  • Update a unit test mock assertion to reflect the new discovery call signature.

Reviewed changes

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

FileDescription
src/apm_cli/primitives/discovery.pyAdds exclude-aware filtering during discovery via a new helper and updated function signatures.
src/apm_cli/compilation/agents_compiler.pyPropagates config.exclude into discovery calls.
tests/unit/compilation/test_agents_compiler_coverage.pyUpdates mocked discovery assertion for the new parameter.

Comment threadsrc/apm_cli/primitives/discovery.py
Comment threadsrc/apm_cli/primitives/discovery.py Outdated
Comment on lines +211 to +217
# "docs/**" style — match anything under the directory
if normalized.endswith('/**'):
prefix = normalized[:-3]
if rel_path_str.startswith(prefix + '/') or rel_path_str == prefix:
return True
# "**/test-fixtures/**" style — match directory at any depth
elif normalized.startswith('**/') and normalized.endswith('/**'):

CopilotAIMar 27, 2026

Copy link

Choose a reason for hiding this comment

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

Non-ASCII characters were added in comments (e.g., the em dash in "style — match"). This violates the project's printable-ASCII-only encoding rule for source files and can break Windows cp1252 terminals. Replace these with plain ASCII (e.g., "-" or "--").

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +205 to +209
) as mock_disc:
result = compiler.compile(config) # no primitives passed → discovers

mock_disc.assert_called_once_with(str(compiler.base_dir))
mock_disc.assert_called_once_with(
str(compiler.base_dir), exclude_patterns=config.exclude

CopilotAIMar 27, 2026

Copy link

Choose a reason for hiding this comment

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

Non-ASCII character in comment (the right arrow in "no primitives passed -> discovers") violates the printable-ASCII-only rule for source files/tests and can cause Windows cp1252 encoding failures. Replace it with ASCII (e.g., "->").

Copilot generated this review using guidance from repository custom instructions.
Propagate exclude patterns from apm.yml to the primitive discovery
phase. Previously, patterns were only applied during context
optimization, allowing files in excluded directories to leak into
compiled output.
Add exclude_patterns parameter to discover_primitives(),
discover_primitives_with_dependencies(), and scan_local_primitives().
Add _matches_exclude_patterns() helper supporting dir/**, **/dir/**,
and fnmatch-style glob patterns.
Fixesmicrosoft#475
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
return False


def _match_glob_recursive(path_parts: list, pattern_parts: list) -> bool:

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Dupped, I know, but extracting it and refactor the code felt out of the scope of this bug.

@Coolomina

Copy link
Copy Markdown
ContributorAuthor

Daniel Meppiel (@danielmeppiel) Closing this in favor of #477

Sign up for freeto 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] compilation.exclude does not filter primitive discovery

3 participants

@Coolomina@kuisathaverat