Skip to content

fix: null-guard intelligence display to prevent TypeError crashes (upstream #1086)#4

Open
blackms wants to merge 1 commit intomainfrom
codex/recreate-pr-1086-null-guard
Open

fix: null-guard intelligence display to prevent TypeError crashes (upstream #1086)#4
blackms wants to merge 1 commit intomainfrom
codex/recreate-pr-1086-null-guard

Conversation

@blackms
Copy link
Owner

@blackms blackms commented Feb 6, 2026

Replica upstream

Questa PR ricrea nel fork la fix upstream:

Contenuto

  • Aggiunte null-guard nella visualizzazione hooks intelligence
  • Prevenuto il crash TypeError quando campi opzionali MCP sono assenti
  • Copertura sezioni: SONA, MoE, HNSW, Embeddings, V3 Performance

Metodo

Cherry-pick del commit upstream 5ceec067591bd693511a4b5dcb6a958c9d2b185f (con -x).

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of CLI command outputs to gracefully handle incomplete data
    • Enhanced consistency in performance metrics and telemetry displays
    • Fixed potential formatting errors in intelligence metrics reporting

…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)
@coderabbitai
Copy link

coderabbitai bot commented Feb 6, 2026

📝 Walkthrough

Walkthrough

The 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 (??) with safe fallback values (0 or 'N/A') to ensure robust output when data fields are missing or undefined.

Changes

Cohort / File(s) Summary
Intelligence Hooks Telemetry Hardening
v3/@claude-flow/cli/src/commands/hooks.ts
Added nullish coalescing (??) operators and optional chaining (?.) throughout intelligence metrics display (Learning Time, Adaptation Time, Trajectories, Patterns Learned, Avg Quality) and MoE/HNSW/Embeddings blocks. Dimension fields fall back to alternative sources or 'N/A'. Ensures well-formed output when nested telemetry structures are absent or incomplete without altering control flow.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Hop, hop, through the null-check gates,
Where missing data no longer breaks our fates!
With ?? and ?. we stand tall and proud,
Defensive defaults in a data crowd! 🌟

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding null-guards to prevent TypeError crashes in the intelligence display, which aligns with the substantive changes shown in the diff.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/recreate-pr-1086-null-guard

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

github-actions bot commented Feb 6, 2026

🔗 Integration Test Results

🔗 Cross-Agent Integration Test Report

Session ID: integration-20260206-223913-8966b48efd895186a5148402ecd893851d8e158a
Timestamp: 2026-02-06T22:41:01.504Z
Overall Status: ✅ PASSED

Summary

  • Total Tests: 4
  • Passed: 4
  • Failed: 0
  • Success Rate: 100.0%

Test Results

Component Status Details
Agent Coordination Multi-agent communication and task distribution
Memory Integration Shared memory operations and synchronization
Fault Tolerance Failure recovery and system resilience
Performance Multi-agent performance and scalability

Recommendations

  • All integration tests passed successfully!

Next Steps

  1. Review detailed test artifacts
  2. Address any failed test scenarios
  3. Monitor integration performance in production

Generated by Cross-Agent Integration Test Pipeline

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +2118 to +2121
{ 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' }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 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=ts

Repository: 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 -n

Repository: 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 -n

Repository: 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 -100

Repository: 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 -20

Repository: 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 -20

Repository: 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 -80

Repository: 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 -n

Repository: 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 -n

Repository: 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants