Skip to content

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

Closed
Coolomina wants to merge 1 commit intomicrosoft:mainfrom
Coolomina:fix/exclude-primitive-discovery
Closed

fix: apply compilation.exclude patterns during primitive discovery#476
Coolomina wants to merge 1 commit intomicrosoft: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: repro
version: 1.0.0
compilation:
  exclude:
    - "docs/**"
YAML

cat > /tmp/repro/.apm/instructions/general.instructions.md << 'MD'
---
description: General rules
applyTo: "**"
---
# General Rules
MD

cat > /tmp/repro/docs/labs/.github/instructions/leaked.instructions.md << 'MD'
---
description: Should not appear
applyTo: "**/*.ts"
---
# Leaked
MD

# 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).

Copilot AI review requested due to automatic review settings March 27, 2026 10:59
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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.

File Description
src/apm_cli/primitives/discovery.py Adds exclude-aware filtering during discovery via a new helper and updated function signatures.
src/apm_cli/compilation/agents_compiler.py Propagates config.exclude into discovery calls.
tests/unit/compilation/test_agents_compiler_coverage.py Updates mocked discovery assertion for the new parameter.

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('/**'):
Copy link

Copilot AI Mar 27, 2026

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
Copy link

Copilot AI Mar 27, 2026

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.
@Coolomina Coolomina force-pushed the fix/exclude-primitive-discovery branch from f163986 to 9ab7408 Compare March 27, 2026 11:53
@Coolomina Coolomina marked this pull request as draft March 27, 2026 11:55
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.

Fixes microsoft#475

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Coolomina Coolomina force-pushed the fix/exclude-primitive-discovery branch from 9ab7408 to d1f1abf Compare March 27, 2026 11:58
return False


def _match_glob_recursive(path_parts: list, pattern_parts: list) -> bool:
Copy link
Copy Markdown
Contributor Author

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
Contributor Author

@danielmeppiel Closing this in favor of #477

@Coolomina Coolomina closed this Mar 29, 2026
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] compilation.exclude does not filter primitive discovery

3 participants