This document summarizes the critical architectural improvements made to the packages/core module to address stability, performance, and correctness issues.
Problem: Service factories were receiving an empty object {} instead of the real PluginContext.
Fix:
- Updated
PluginLoader.createServiceInstanceto ensurethis.contextis passed to factories. - Added
PluginLoader.setContextmethod to inject the context from the Kernel. - Kernel now properly injects itself and the loader into the context before initialization.
Problem: Services created asynchronously (via awaitFactory) were not accessible synchronously immediately after initialization, breaking code that expected getService to return an instance if the plugin was loaded.
Fix:
- Implemented L2 caching via
PluginLoader.getServiceInstance<T>(name: string). - Kernel's
getServicenow checks this synchronous cache first before falling back to the async path. - Ensured singleton instances are stored in
serviceInstancesimmediately upon creation.
Problem: Complex service graphs could deadlock or crash the stack if factories recursively requested each other. Static analysis was insufficient for dynamic factories. Fix:
- Added a
creatingSet toPluginLoader. createServiceInstancenow tracks which services are currently being built.- Throws a descriptive error if a loop is detected (e.g.,
Circular dependency detected: serviceA -> serviceB -> serviceA).
Problem: The Kernel swallowed errors from service factories (like database connection failures) and threw a generic "Service not found" error, making debugging impossible. Fix:
- Refined
Kernel.getServiceto distinguish between "service registration missing" and "factory execution failed". - Factory errors are now re-thrown with their original stack trace and message.
Problem: Configuration validation was a scaffold without implementation. Fix:
- Integrated
PluginConfigValidator(Zod-based) intoPluginLoader. validatePluginConfignow performs actual schema validation againstplugin.configSchema.
- Build: Clean build of
distartifacts. - Tests: 100% Pass rate (380/380 tests) across 22 test suites.