Bugfix: What happens when @Optional() NestJS services are promoted t...
Documentation for Sessions
🐛 Bugfix: What happens when @Optional() NestJS services are promoted t...
Date: 2026-03-27
Time: 18:54:55 UTC
Session: gspace1-276.13
Agent: backend
Confidence: 95%
Status: ✅ Documented
📋 Decision Context
Question:
What happens when @Optional() NestJS services are promoted to required injection without verifying module imports?
Task: Wire 8: Guarantee @Optional() services
🎯 Decision Made
Chosen:
Always verify the provider's source module is imported by the consumer's module before removing @Optional(). AwarenessModule was missing from EngineModule — the app would crash at bootstrap.
Options Considered:
- Trust that modules are already imported (failed)
- Add runtime onModuleInit check (insufficient — crashes before init)
- Verify module import chain before removing @Optional() (chosen)
💭 Reasoning
Root cause: @Optional() services silently resolve to null when their module isn't imported. The code's null guards (ternary checks, ?. operators) were load-bearing — they weren't defensive coding, they were compensating for a missing module import. TypeScript compilation does NOT catch NestJS DI scope errors — those only manifest at runtime. The plan assumed AwarenessModule was imported by EngineModule; it wasn't. Code review caught this before runtime. Early warning: any @Optional() → required promotion should trigger a grep for the service's module in the consumer module's imports array.
📁 Files Involved
APIs/backend-vitto-api/src/app/engine/engine.module.tsAPIs/backend-vitto-api/src/app/engine/context-assembly/channel-prompt-builder.service.tsAPIs/backend-vitto-api/src/app/awareness/awareness.module.ts
🔗 Related
- Decision ID:
dev_1774637695492_u16ubjt - Session: 276
- Workspace: gspace1
Auto-generated by MCP Smart Consolidation