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:
- Parsing: The source code is read and converted into a Python
astobject. - Dispatch: Every registered
PatternDetectorreceives the full tree. - Specialization: Each detector (e.g.,
MembershipDetector) traverses the tree looking for its specific signature. - 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)"
}