Skip to content

Pattern Registry (alnoms.patterns)

The Pattern Registry is the "Library of Anti-Patterns" used by the Alnoms Heuristics Engine. It consists of specialized detectors that scan your code's Abstract Syntax Tree (AST) to identify structural inefficiencies before they ever run.


🧭 1. Philosophy: Static Intent Detection

Traditional profilers wait for code to run slow. Alnoms identifies Intent. By analyzing the AST, the engine can see that you intended to check membership inside a loop, which mathematically implies a specific complexity class (\(O(N^2)\)), regardless of how fast your current CPU is.


πŸ› οΈ 2. The Heuristics Engine

The HeuristicsEngine orchestrates the analysis by dispatching the parsed AST to a modular registry of detectors. This process is: * Stateless: The engine does not store user code. * Extensible: Support for PRO and ENTERPRISE tiers is loaded dynamically via environment feature flags. * Fail-Safe: Syntax errors are captured as file-level findings, allowing empirical tests to proceed even if static analysis fails.


🧩 3. Active Pattern Catalog (OSS Tier)

The Open Source tier includes detectors for the most common "Silent Traps" in Python development:

Pattern ID Description Common Cause
nested_loops Detects deep iteration nesting. Forgetting asymptotic growth in small datasets.
inefficient_membership Membership tests (in) inside loops. Using a list instead of a set for lookups.
redundant_sort Frequent sorting of already-ordered data. Lack of state management in data pipelines.
inplace_concat String or list concatenation in a loop. Creating new objects in memory for every iteration.
expensive_calls Redundant calls to heavy functions. Missing memoization or caching layers.
high_freq_io Unbuffered or excessive disk/network IO. Writing to a file inside a tight loop.

βš™οΈ 4. Internal Mechanics: The Registry Dispatcher

The analysis follows a deterministic dispatch pattern:

  1. Parsing: The source code is read and converted into a Python ast object.
  2. Dispatch: Every registered PatternDetector receives the full tree.
  3. Specialization: Each detector (e.g., MembershipDetector) traverses the tree looking for its specific signature.
  4. Aggregation: All findingsβ€”including line numbers and function namesβ€”are flattened into a single JSON-compatible report for the Decision Engine.

🏒 5. Tiered Capabilities

The registry automatically expands based on your environment keys:

  • OSS Tier: Foundational DSA patterns (Default).
  • PRO Tier: Advanced algorithmic patterns and domain-specific optimizations.
  • ENTERPRISE Tier: Custom organizational patterns and security-focused performance audits.

πŸ§ͺ 6. Example Finding

If the engine detects a membership trap, it returns a structured finding:

{
    "function": "process_records",
    "pattern_id": "inefficient_membership",
    "issue": "O(N) lookup inside O(N) loop detected.",
    "line": 42,
    "complexity": "O(N^2)"
}
πŸ‘‰ Next Step: See how these static findings are validated in the Benchmarks section.