Principle:AI Northstar Tech Vector io Dynamic Module Discovery
| Knowledge Sources | |
|---|---|
| Domains | Software_Architecture, Plugin_System, CLI |
| Last Updated | 2026-10-15 00:00 GMT |
Overview
A software architecture pattern that enables automatic registration of plugin modules at runtime by scanning a package directory, importing each module, and collecting classes that subclass a known base class.
Description
Dynamic Module Discovery eliminates the need for manual plugin registration. Instead of maintaining a hardcoded list of supported databases, the system scans a package directory using pkgutil.walk_packages, imports every non-package module via importlib.import_module, and checks each class in the module's namespace against a target base class. Classes that match are registered by a key (e.g. a DB_NAME_SLUG class variable) into a dispatch table.
This pattern solves the problem of tightly-coupled plugin systems where adding a new component requires modifying a central registry. With dynamic discovery, a new vector database export or import module is automatically registered simply by placing a file in the package directory and defining the required class variable (DB_NAME_SLUG).
The __init_subclass__ hook on the base class enforces the contract: any subclass that lacks DB_NAME_SLUG raises a TypeError at import time, providing early validation.
Usage
Use this principle when designing a plugin-based system where new components should be auto-registered without modifying central configuration files. It is particularly suited for:
- CLI tools with multiple backends (database connectors, format converters)
- Systems where new integrations are added frequently
- Open-source projects where contributors can add support for new systems by simply adding a module file
Theoretical Basis
The discovery algorithm operates as follows:
Pseudo-code Logic:
# Abstract algorithm description (NOT real implementation)
def discover_plugins(base_package_dir, base_class, registry_key_attr):
package = importlib.import_module(base_package_dir)
registry = {}
for module_name in pkgutil.walk_packages(package.__path__):
module = importlib.import_module(f"{base_package_dir}.{module_name}")
for name, cls in module.__dict__.items():
if isinstance(cls, type) and issubclass(cls, base_class) and cls is not base_class:
key = getattr(cls, registry_key_attr) # e.g. DB_NAME_SLUG
registry[key] = cls
return registry
The __init_subclass__ enforcement:
# Contract enforcement (abstract)
class BasePlugin(ABC):
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
if not hasattr(cls, "REGISTRY_KEY"):
raise TypeError(f"{cls.__name__} lacks required 'REGISTRY_KEY'")
Practical Guide
To add a new plugin (e.g. a new vector database export):
- Create a new module file in the package directory (e.g. mydb_export.py)
- Define a class that subclasses the base class (e.g. ExportVDB)
- Set the required class variable (e.g. DB_NAME_SLUG = "mydb")
- Implement all abstract methods
- The module is automatically discovered and registered on next CLI invocation
No changes to any central registry or configuration file are needed.