fix: null-guard intelligence display to prevent TypeError crashes (upstream #1086)#4
fix: null-guard intelligence display to prevent TypeError crashes (upstream #1086)#4
Conversation
…shes The hooks intelligence command crashes with 'TypeError: Cannot read properties of undefined (reading toFixed)' when the MCP tool response is missing optional fields (learningTimeMs, adaptationTimeMs, avgQuality, expertsActive, routingAccuracy, loadBalance, searchSpeedup, memoryUsage, dimension, cacheHitRate, performance section). Added 15 null-guards using ?? operator across 5 display sections (SONA, MoE, HNSW, Embeddings, V3 Performance) to handle absent fields gracefully. Ref: ADR-015 Co-Authored-By: claude-flow <ruv@ruv.net> (cherry picked from commit 5ceec06)
📝 WalkthroughWalkthroughThe change adds defensive nullish handling and optional chaining throughout the hooks telemetry command output. Intelligence metrics, MoE/HNSW/Embeddings blocks, and dimension fields now use null coalescing operators ( Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🔗 Integration Test Results🔗 Cross-Agent Integration Test ReportSession ID: integration-20260206-223913-8966b48efd895186a5148402ecd893851d8e158a Summary
Test Results
Recommendations
Next Steps
Generated by Cross-Agent Integration Test Pipeline |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@v3/`@claude-flow/cli/src/commands/hooks.ts:
- Around line 2118-2121: The HNSW object returned by callMCPTool is missing the
optional memorySizeBytes property in its type declaration, causing TS2339 when
accessing result.components.hnsw.memorySizeBytes; update the type parameter
passed to callMCPTool (the HNSW subtype used around lines where callMCPTool is
defined) to include memorySizeBytes?: number alongside the existing fields
(enabled, status, indexSize, searchSpeedup, memoryUsage, dimension) so the
access to result.components.hnsw.memorySizeBytes compiles safely.
🧹 Nitpick comments (1)
v3/@claude-flow/cli/src/commands/hooks.ts (1)
1988-2039: Consider making the guarded fields optional in the type definition to match runtime reality.The generic type parameter declares fields like
learningTimeMs,routingAccuracy,cacheHitRate, etc. as required, yet the entire purpose of this PR is to guard against them being absent at runtime. Making these fields optional (e.g.,learningTimeMs?: number) would make the type honest about the actual MCP contract and let TypeScript flag any unguarded access in the future.
| { metric: 'Index Size', value: (result.components.hnsw.indexSize ?? 0).toLocaleString() }, | ||
| { metric: 'Search Speedup', value: output.success(result.components.hnsw.searchSpeedup ?? 'N/A') }, | ||
| { metric: 'Memory Usage', value: result.components.hnsw.memoryUsage ?? (result.components.hnsw.memorySizeBytes ? `${Math.round(result.components.hnsw.memorySizeBytes / 1024)}KB` : 'N/A') }, | ||
| { metric: 'Dimension', value: result.components.hnsw.dimension ?? result.components.embeddings?.dimension ?? 'N/A' } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
# First, let's check if the file exists and get basic info
head -1 "v3/@claude-flow/cli/src/commands/hooks.ts" 2>/dev/null && echo "File found" || echo "File not found or checking path"
wc -l "v3/@claude-flow/cli/src/commands/hooks.ts"Repository: blackms/claude-flow
Length of output: 124
🏁 Script executed:
# Verify memorySizeBytes usage in the codebase as requested
rg -n 'memorySizeBytes' --type=tsRepository: blackms/claude-flow
Length of output: 971
🏁 Script executed:
# Read the type definition around lines 2008-2015
sed -n '2000,2030p' "v3/@claude-flow/cli/src/commands/hooks.ts" | cat -nRepository: blackms/claude-flow
Length of output: 1114
🏁 Script executed:
# Read the code snippet around lines 2118-2121 to verify context
sed -n '2110,2130p' "v3/@claude-flow/cli/src/commands/hooks.ts" | cat -nRepository: blackms/claude-flow
Length of output: 1311
🏁 Script executed:
# Search for where the HNSW response object is being constructed
rg -n -B 5 -A 5 'hnsw.*:.*{' "v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts" | head -100Repository: blackms/claude-flow
Length of output: 146
🏁 Script executed:
# Look for where the response with hnsw component is returned
rg -n 'components.*hnsw' "v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts" | head -20Repository: blackms/claude-flow
Length of output: 45
🏁 Script executed:
# Search for where hnsw response is being constructed in hooks-tools.ts
rg -n "hnsw" "v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts" | grep -i "return\|response\|components" | head -20Repository: blackms/claude-flow
Length of output: 45
🏁 Script executed:
# Look at the entire function that returns the response with hnsw
# First find all hnsw assignments
rg -n -A 15 "hnsw:" "v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts" | head -80Repository: blackms/claude-flow
Length of output: 2038
🏁 Script executed:
# Let's find which function/tool returns the response printed at line 2120
# Look at context around line 2120 to see the function name
sed -n '2070,2130p' "v3/@claude-flow/cli/src/commands/hooks.ts" | cat -nRepository: blackms/claude-flow
Length of output: 3510
🏁 Script executed:
# Also search for the function that contains the type definition at lines 2008-2015
sed -n '1995,2045p' "v3/@claude-flow/cli/src/commands/hooks.ts" | cat -nRepository: blackms/claude-flow
Length of output: 1839
Add memorySizeBytes to the HNSW type definition — accessing an undeclared field will cause TS2339.
On line 2120, the code accesses result.components.hnsw.memorySizeBytes, but the type parameter for callMCPTool (lines 2008–2021) does not include this field. The MCP backend returns memorySizeBytes (visible in hooks-tools.ts), so add it to the type definition to resolve the compilation error:
hnsw: {
enabled: boolean;
status: string;
indexSize: number;
searchSpeedup: string;
memoryUsage: string;
dimension: number;
memorySizeBytes?: number;
};🤖 Prompt for AI Agents
In `@v3/`@claude-flow/cli/src/commands/hooks.ts around lines 2118 - 2121, The HNSW
object returned by callMCPTool is missing the optional memorySizeBytes property
in its type declaration, causing TS2339 when accessing
result.components.hnsw.memorySizeBytes; update the type parameter passed to
callMCPTool (the HNSW subtype used around lines where callMCPTool is defined) to
include memorySizeBytes?: number alongside the existing fields (enabled, status,
indexSize, searchSpeedup, memoryUsage, dimension) so the access to
result.components.hnsw.memorySizeBytes compiles safely.
Replica upstream
Questa PR ricrea nel fork la fix upstream:
Contenuto
hooks intelligenceTypeErrorquando campi opzionali MCP sono assentiMetodo
Cherry-pick del commit upstream
5ceec067591bd693511a4b5dcb6a958c9d2b185f(con-x).Summary by CodeRabbit