Skip to content

Compilation

Solving the AI agent scalability problem through constraint satisfaction optimization

APM’s compilation system implements a mathematically rigorous solution to the context pollution problem that degrades AI agent performance as projects grow. Through constraint satisfaction optimization and hierarchical coverage guarantees, apm compile transforms scattered primitives into optimized context files for every major AI coding agent.

APM compiles your primitives into native formats for each major AI coding agent. Target selection is automatic based on your project structure.

When you run apm compile without specifying a target, APM automatically detects:

Project StructureTargetWhat Gets Generated
.github/ folder onlycopilotAGENTS.md (instructions only)
.claude/ folder onlyclaudeCLAUDE.md (instructions only)
Both folders existallBoth AGENTS.md and CLAUDE.md
Neither folder existsminimalAGENTS.md only (universal format)
Terminal window
apm compile # Auto-detects target from project structure
apm compile --target copilot # Force GitHub Copilot, Cursor, Codex, Gemini
apm compile --target claude # Force Claude Code, Claude Desktop

You can set a persistent target in apm.yml:

name: my-project
version: 1.0.0
target: copilot # or vscode, claude, or all
TargetFiles GeneratedConsumers
copilotAGENTS.mdGitHub Copilot, Cursor, Codex, Gemini
claudeCLAUDE.mdClaude Code, Claude Desktop
allBoth AGENTS.md and CLAUDE.mdUniversal compatibility
minimalAGENTS.md onlyWorks everywhere, no folder integration

Aliases: vscode and agents are accepted as aliases for copilot.

Note: AGENTS.md and CLAUDE.md contain only instructions (grouped by applyTo patterns). Prompts, agents, commands, hooks, and skills are integrated by apm install, not apm compile. See the Integrations Guide for details on how apm install populates .github/prompts/, .github/agents/, .github/skills/, and .claude/commands/.

  1. Primitives Discovery: Scans .apm/ and .github/ directories for instructions, prompts, and agents
  2. Dependency Merging: Incorporates primitives from installed packages in apm_modules/
  3. Optimization: Applies mathematical context optimization (see below)
  4. Format Generation: Outputs native files for each target agent format

After apm compile:

my-project/
├── AGENTS.md # Instructions only (for Copilot, Cursor, etc.)
└── CLAUDE.md # Instructions only (for Claude)

After apm install (folder integration):

my-project/
├── .github/
│ ├── prompts/ # Prompts from installed packages
│ └── agents/ # Agents from installed packages
└── .claude/
├── commands/ # Claude slash commands from packages
└── skills/ # Skills from packages with SKILL.md

In traditional monolithic AGENTS.md approaches, AI agents face a fundamental efficiency problem: context pollution. As projects grow, agents must process increasingly large amounts of irrelevant instructions, degrading performance and overwhelming context windows.

The Mathematical Challenge:

Context_Efficiency = Relevant_Instructions / Total_Instructions_Inherited

Without optimization, context efficiency degrades quadratically with project size, creating an unsustainable burden on AI agents working in specific directories.

APM implements the AGENTS.md standard for hierarchical context files:

  • Recursive Discovery: Agents read AGENTS.md files from current directory up to project root
  • Proximity Priority: Closest AGENTS.md to the edited file takes precedence
  • Inheritance Model: Child directories inherit and can override parent instructions
  • Universal Compatibility: Works with GitHub Copilot, Cursor, Claude, and all AGENTS.md-compliant tools

APM treats instruction placement as a constrained optimization problem:

Objective: minimize Σ(pollution[d] × files[d])
d∈directories
Subject to: ∀f ∈ matching_files(pattern) →
∃p ∈ placements : f.can_inherit_from(p)
Variables: placement_matrix ∈ {0,1}^(directories × instructions)

This mathematical formulation guarantees:

  1. Complete Coverage: Every file can access its applicable instructions
  2. Minimal Pollution: Irrelevant context is systematically minimized
  3. Hierarchical Validity: Inheritance chains remain consistent

APM employs sophisticated distribution scoring with mathematical thresholds:

# From context_optimizer.py
Distribution_Score = (matching_directories / total_directories) × diversity_factor
Where:
diversity_factor = 1.0 + (depth_variance × DIVERSITY_FACTOR_BASE)
DIVERSITY_FACTOR_BASE = 0.5 # Mathematical constant

Strategy Selection:

Distribution ScoreStrategyMathematical Logic
< 0.3Single-Point_optimize_single_point_placement()
0.3 - 0.7Selective Multi_optimize_selective_placement()
> 0.7Distributed_optimize_distributed_placement()

The optimization engine uses mathematically calibrated weights:

# Mathematical optimization parameters from the source
COVERAGE_EFFICIENCY_WEIGHT = 1.0 # Mandatory coverage priority
POLLUTION_MINIMIZATION_WEIGHT = 0.8 # Strong pollution penalty
MAINTENANCE_LOCALITY_WEIGHT = 0.3 # Moderate locality preference
DEPTH_PENALTY_FACTOR = 0.1 # Excessive nesting penalty

The primary performance indicator for AI agent effectiveness:

def get_efficiency_ratio(self) -> float:
"""Calculate context efficiency ratio."""
if self.total_context_load == 0:
return 1.0
return self.relevant_context_load / self.total_context_load

Interpretation Guide:

Efficiency RangeAssessmentOptimization Quality
80-100%ExcellentNear-perfect instruction locality
60-80%GoodWell-optimized with minimal conflicts
40-60%FairAcceptable coverage/efficiency balance
20-40%PoorSignificant cross-cutting concerns
0-20%CriticalArchitecture requires refactoring

Important: Low efficiency can be mathematically optimal when coverage constraints force root placement. The optimizer always prioritizes complete coverage over efficiency.

Measures pattern spread across the directory structure:

def _calculate_distribution_score(self, matching_directories: Set[Path]) -> float:
"""Calculate distribution score with diversity factor."""
total_dirs_with_files = len([d for d in self._directory_cache.values() if d.total_files > 0])
base_ratio = len(matching_directories) / total_dirs_with_files
# Account for depth diversity
depths = [self._directory_cache[d].depth for d in matching_directories]
depth_variance = sum((d - sum(depths)/len(depths))**2 for d in depths) / len(depths)
diversity_factor = 1.0 + (depth_variance * self.DIVERSITY_FACTOR_BASE)
return base_ratio * diversity_factor

Mathematical guarantee that no instruction is lost:

def _calculate_hierarchical_coverage(self, placements: List[Path], target_directories: Set[Path]) -> Set[Path]:
"""Verify hierarchical coverage through inheritance chains."""
covered = set()
for target in target_directories:
for placement in placements:
if self._is_hierarchically_covered(target, placement):
covered.add(target)
break
return covered
Terminal window
# Intelligent distributed optimization
apm compile
# Example output:
📊 Analyzing 247 files across 12 directories...
🎯 Optimizing instruction placement...
Generated 4 AGENTS.md files with guaranteed coverage
Terminal window
# Show optimization reasoning
apm compile --verbose
# Example detailed output:
🔬 Mathematical Analysis:
├─ Distribution Scores:
├─ **/*.py: 0.23 Single-Point Strategy
├─ **/*.tsx: 0.67 Selective Multi Strategy
└─ **/*.md: 0.81 Distributed Strategy
├─ Coverage Verification: Complete (100%)
├─ Constraint Satisfaction: All 8 constraints satisfied
└─ Generation Time: 127ms
Terminal window
# Preview placement without writing files
apm compile --dry-run
# Timing instrumentation
apm compile --verbose
# Shows: ⏱️ Project Analysis: 45.2ms
# ⏱️ Instruction Processing: 82.1ms
apm.yml
compilation:
strategy: "distributed" # Default: mathematical optimization
exclude:
# Directory exclusion patterns (glob syntax)
- "apm_modules/**" # Exclude installed packages
- "tmp/**" # Exclude temporary files
- "coverage/**" # Exclude test coverage
- "**/test-fixtures/**" # Exclude test fixtures everywhere
placement:
min_instructions_per_file: 1 # Minimal context principle
clean_orphaned: true # Remove outdated files
optimization:
# Mathematical weights (advanced users)
coverage_weight: 1.0 # Coverage priority (mandatory)
pollution_weight: 0.8 # Pollution minimization
locality_weight: 0.3 # Maintenance locality

Use the exclude field to skip directories during compilation, improving performance in large monorepos:

Pattern Syntax:

  • tmp - Matches directory named “tmp” at any depth
  • tmp/ - Same as above (trailing slash optional)
  • projects/packages/apm - Matches specific nested path
  • **/node_modules - Matches “node_modules” at any depth
  • coverage/** - Matches “coverage” and all subdirectories
  • projects/**/apm/** - Complex nested matching

Use Cases:

  • Exclude source package development directories in monorepos
  • Skip temporary directories and build artifacts
  • Improve compilation performance by avoiding unnecessary scans
  • Prevent duplicate instruction discovery

Default Exclusions: APM always excludes directories whose path contains an exact component matching one of these names (no configuration needed). A directory named rebuild/ is not excluded just because it contains build as a substring.

  • node_modules
  • __pycache__
  • .git
  • dist
  • build
  • apm_modules
  • Hidden directories (starting with .)

The mathematical coverage constraint ensures no instruction is ever lost:

project/
├── AGENTS.md # Global standards
├── src/
│ ├── AGENTS.md # Source code patterns
│ └── components/
│ ├── AGENTS.md # Component-specific
│ └── Button.tsx # Inherits: global + src + components

Coverage Verification Algorithm:

def verify_coverage(placements, matching_files):
"""Ensure every file can inherit its instructions"""
for file in matching_files:
chain = get_inheritance_chain(file)
if not any(p in chain for p in placements):
raise CoverageViolation(file) # Mathematical guarantee
return True

Multi-layer caching system for sub-second compilation:

# From context_optimizer.py
self._directory_cache: Dict[Path, DirectoryAnalysis] = {}
self._pattern_cache: Dict[str, Set[Path]] = {}
self._glob_cache: Dict[str, List[str]] = {}

Typical performance: < 500ms for projects with 10,000+ files

Compilation is completely reproducible:

  • Sorted iteration order prevents randomness
  • Stable optimization algorithm
  • Consistent Build IDs across machines
  • Cache-friendly for CI/CD systems

Project governance automatically injected at AGENTS.md top:

<!-- SPEC-KIT CONSTITUTION: BEGIN -->
hash: 34c5812dafc9 path: memory/constitution.md
[Project principles and governance]
<!-- SPEC-KIT CONSTITUTION: END -->

Project Characteristics:

  • 15,000+ lines of code
  • 127 component files
  • 8 instruction patterns
  • 3 team-specific standards

Optimization Results:

  • 7 strategically placed AGENTS.md files
  • Complete coverage mathematically verified
  • Context efficiency: 67.3% (Good rating)
  • Generation time: 89ms

Compared to Monolithic Approach:

  • Single 847-line AGENTS.md file
  • Universal context pollution
  • No mathematical optimization
  • Manual maintenance required

APM implements complete coverage with minimal pollution:

  1. Coverage Constraint: Mathematical guarantee every file accesses applicable instructions
  2. Pollution Minimization: Systematic reduction of irrelevant context
  3. Hierarchical Validation: Inheritance chain verification
  4. Performance Optimization: Sub-second compilation with caching
# Actual implementation from context_optimizer.py
if distribution_score < self.LOW_DISTRIBUTION_THRESHOLD:
strategy = PlacementStrategy.SINGLE_POINT
placements = self._optimize_single_point_placement(matching_directories, instruction)
elif distribution_score > self.HIGH_DISTRIBUTION_THRESHOLD:
strategy = PlacementStrategy.DISTRIBUTED
placements = self._optimize_distributed_placement(matching_directories, instruction)
else:
strategy = PlacementStrategy.SELECTIVE_MULTI
placements = self._optimize_selective_placement(matching_directories, instruction)

The optimization engine implements:

  • Variance-weighted distribution scoring
  • Hierarchical coverage verification
  • Constraint satisfaction with fallback guarantees
  • Performance-optimized caching strategies
  • Deterministic reproducible results

Generated AGENTS.md files work seamlessly across all major coding agents:

  • GitHub Copilot (All variations)
  • Cursor (Native AGENTS.md support)
  • Continue (VS Code & JetBrains)
  • Codeium (Universal compatibility)
  • Claude (Anthropic’s implementation)
  • Any AGENTS.md standard compliant tool
  • Time Complexity: O(n·m·log(d))

    • n = number of instructions
    • m = number of directories
    • d = maximum directory depth
  • Space Complexity: O(n·m)

    • Placement matrix storage

Theoretical maximum efficiency:

Max_Efficiency = 1 - (cross_cutting_patterns / total_patterns)

Most well-structured projects achieve 60-85% of theoretical maximum through mathematical optimization.

Machine Learning Enhancement: Neural network to predict optimal placement based on:

  • Historical agent query patterns
  • File change frequency analysis
  • Team-specific access patterns

Dynamic Recompilation: File watcher with targeted optimization:

Terminal window
apm compile --watch # Auto-recompile on changes

Context Budget Optimization: Token-aware instruction prioritization:

compilation:
optimization:
max_tokens_per_file: 4000
priority_scoring: true

APM’s Context Optimization Engine represents a fundamental advancement in AI-assisted development infrastructure. By treating instruction distribution as a mathematical optimization problem with guaranteed coverage constraints, APM creates:

  1. Mathematically optimal context loading for AI agents
  2. Complete coverage guarantee through constraint satisfaction
  3. Linear scalability with project size
  4. Universal compatibility with the AGENTS.md standard
  5. Performance engineering with sub-second compilation

The result: AI agents that work efficiently and reliably, regardless of project size or complexity.


Ready to optimize your AI agent performance?

Terminal window
# See the mathematics in action
apm compile --verbose
# Experience optimized AI development
apm init my-project && cd my-project && apm compile

Technical Implementation: src/apm_cli/compilation/
Mathematical Core: context_optimizer.py