Bugfix: ReflectionService.reflectOnTask crashes with 'TypeError: act...

Documentation for Sessions

🐛 Bugfix: ReflectionService.reflectOnTask crashes with 'TypeError: act...

Date: 2026-02-25
Time: 20:23:59 UTC
Session: gspace1-141.14
Agent: backend
Confidence: 97%
Status: ✅ Documented


📋 Decision Context

Question:

ReflectionService.reflectOnTask crashes with 'TypeError: actions is not iterable' when MCP intelligence reflect endpoint is called without an actions array

Task: Fix ReflectionService TypeError on intelligence reflect


🎯 Decision Made

Chosen:

Add defensive Array.isArray() guard in reflectOnTask before iterating

Options Considered:

  1. Add class-validator decorators to ReflectOnTaskDto for runtime validation
  2. Add defensive Array.isArray() guard in reflectOnTask before iterating
  3. Add both DTO validation and service-level guards

💭 Reasoning

ROOT CAUSE: The ReflectOnTaskDto class has no class-validator decorators, so NestJS passes the raw body without validation. When the MCP server calls POST /ai-api/super-intelligence/reflect (via the intelligence({ action: 'reflect' }) tool), it constructs the payload from user-provided lessonsLearned and outcome fields. If lessonsLearned is empty or the mapping produces no actions array, or if the endpoint is called directly with a minimal payload, actions is undefined. The for...of loop at line 216 then throws TypeError: actions is not iterable. FIX: Defensive default in reflectOnTask: const actions = Array.isArray(rawActions) ? rawActions : []. Same treatment for finalOutcome and predictedConfidence. This is preferable to DTO validation because: (1) it's fail-safe — the service works with partial input instead of rejecting it, (2) reflection is a learning mechanism that should gracefully degrade, not crash, (3) adding class-validator would require installing/configuring ValidationPipe globally which could break other endpoints. WHAT WOULD MAKE THIS WRONG: If callers depend on a 400 validation error to know their input is malformed. But for an internal service called by the MCP server, graceful degradation is more valuable than strict validation.


📁 Files Involved


🔗 Related


Auto-generated by MCP Smart Consolidation