Skip to content

[Part 6/6] feat(cli): add activity monitoring hooks and integrate telemetry documentation#8125

Closed
eLyiN wants to merge 17 commits intogoogle-gemini:mainfrom
eLyiN:split/cli-activity-hooks
Closed

[Part 6/6] feat(cli): add activity monitoring hooks and integrate telemetry documentation#8125
eLyiN wants to merge 17 commits intogoogle-gemini:mainfrom
eLyiN:split/cli-activity-hooks

Conversation

@eLyiN
Copy link
Copy Markdown
Contributor

@eLyiN eLyiN commented Sep 9, 2025

Summary

Completes the enhanced performance monitoring implementation by adding CLI hooks for activity monitoring and integrating them throughout the CLI application. Includes comprehensive telemetry documentation and full AppContainer integration.

Dependencies

⚠️ This PR depends on:

Review Order: Please review Parts 1-5 first, then this PR.

⚠️ STACKED PR: This PR is based on split/activity-monitor and includes all previous changes. Only review the NEW CLI integration components added in this PR.

Related PRs - Enhanced Performance Monitoring Split

This is Part 6 of 6 in the split implementation of #2127:

Changes in This PR (NEW Components Only)

🆕 CLI Files Added:

  • packages/cli/src/ui/hooks/useActivityMonitoring.ts - React hook for activity monitoring (166 lines)
  • packages/cli/src/ui/hooks/useActivityMonitoring.test.ts - Comprehensive hook unit tests (235 lines, 14 tests)
  • packages/cli/src/ui/hooks/useHistoryManagerWithActivityMonitoring.ts - History management with activity tracking (45 lines)

✏️ CLI Files Modified:

  • packages/cli/src/ui/hooks/useHistoryManager.ts - Added callback support (onItemAdded, onItemUpdated, onHistoryCleared, onHistoryLoaded)
  • packages/cli/src/ui/hooks/useGeminiStream.ts - Integrated activity recording in stream flows
  • packages/cli/src/ui/AppContainer.tsx - Wired up useHistoryWithActivityMonitoring
  • packages/cli/src/ui/AppContainer.test.tsx - Updated mocks for new hook

📚 Documentation Added:

  • docs/telemetry.md - Complete telemetry system documentation (217 insertions)

CLI Activity Hooks

useActivityMonitoring Hook:

const {
  recordToolCall,
  recordUserInput,
  recordStreamStart,
  recordStreamEnd,
  recordMessageAdded,
  recordHistoryUpdate
} = useActivityRecorder(config);

Features:

  • React integration: Seamless integration with CLI React components
  • Activity recording: Specific functions for different activity types
  • Feature-flagged: Respects isPerformanceMonitoringActive() setting
  • Memoized: Optimized to prevent unnecessary re-renders

useHistoryWithActivityMonitoring Hook:

const historyManager = useHistoryWithActivityMonitoring(config);

Enhancements:

  • Drop-in replacement for useHistory with zero breaking changes
  • Automatic activity tracking on all history operations
  • Callback architecture: Extends base useHistory with lifecycle callbacks
  • Type-safe: Returns same UseHistoryManagerReturn interface

Stream Integration

Activity Recording in useGeminiStream.ts:

  • Tool scheduling: recordToolCall() after slash command tool scheduling
  • User messages: recordUserInput() when adding normal user messages
  • Stream lifecycle: recordStreamStart() and recordStreamEnd() in submitQuery
  • Non-intrusive: No behavior changes, only activity signal emission

AppContainer Integration

Complete Top-Level Integration:

  • AppContainer.tsx: Replaced useHistory() with useHistoryWithActivityMonitoring(config)
  • Automatic propagation: All history operations now emit activity events
  • Feature-gated: Only active when performance monitoring is enabled
  • Zero breaking changes: Maintains existing behavior and API

History Manager Enhancements

Callback Support in useHistoryManager.ts:

const historyManager = useHistory({
  onItemAdded: (item) => { /* callback */ },
  onItemUpdated: (item) => { /* callback */ },
  onHistoryCleared: () => { /* callback */ },
  onHistoryLoaded: (items) => { /* callback */ }
});

Benefits:

  • Extensibility: Allows hooks to observe history changes
  • Decoupled: Activity monitoring doesn't modify core history logic
  • Optional: Callbacks are optional, maintaining backward compatibility

Telemetry Documentation

Comprehensive Documentation (docs/telemetry.md):

  • Complete system overview: All 6 components and their relationships
  • Usage examples: How to use each monitoring component
  • Configuration guide: Performance monitoring settings and options
  • Integration patterns: Best practices for CLI and UI integration
  • Troubleshooting: Common issues and debugging approaches

Testing (NEW Tests Only)

CLI Hook tests:

  • useActivityMonitoring.test.ts - 14 tests covering hook behavior, lifecycle, activity recording
  • Tests use React Testing Library + vitest for proper hook testing
  • Mock-based testing for telemetry core dependencies

Integration validation:

  • Tests hook integration with core telemetry components
  • Validates activity recording in stream flows
  • Ensures no regressions in existing CLI behavior

Build verification:

  • CLI builds successfully with new hooks
  • Linting passes
  • Activity monitoring tests pass (14/14)

Technical Implementation

React Integration Architecture:

  1. useActivityMonitoring → Provides React interface to ActivityMonitor (Part 5)
  2. useHistoryWithActivityMonitoring → Wraps history with activity emission
  3. Stream integration → Records activity during key user interactions
  4. AppContainer integration → Wires up monitoring at application root
  5. Documentation → Explains entire telemetry system for developers

Zero Breaking Changes:

  • All new functionality is opt-in
  • Activity monitoring feature-flagged via isPerformanceMonitoringActive()
  • Existing CLI behavior unchanged when monitoring is disabled
  • Stream activity recording is transparent and non-intrusive
  • Documentation provides clear migration path

Benefits

  1. Complete CLI integration: Activity monitoring fully wired throughout the application
  2. Minimal footprint: Only adds activity signals to existing flows, no behavior changes
  3. Developer-friendly: Comprehensive documentation for future maintenance
  4. Production-ready: Tested, documented, and ready for gradual rollout
  5. Extensible: Hook and callback architecture allows easy addition of more monitoring features
  6. Feature-gated: Safe to merge, only activates when explicitly enabled

Addresses Original Issues

Issue #1685 (startup/exit problems):

  • Memory monitoring during startup provides diagnostic data
  • Activity-aware recording reduces noise in startup telemetry

Issue #1687 (rate limiting + memory issues):

  • Complete memory monitoring with intelligent rate limiting
  • Activity-driven snapshots prevent memory analysis noise

Performance optimization:

  • Data-driven memory optimization capabilities
  • Event-contextualized performance metrics
  • Baseline comparison and regression detection

🎉 COMPLETE IMPLEMENTATION: This completes the enhanced performance monitoring implementation from #2127, providing production-ready monitoring with full CLI integration and comprehensive documentation.

Ready for Production: All components tested, documented, and integrated. Activity monitoring is feature-flagged and safe to activate in CLI workflows.

@jacob314
Copy link
Copy Markdown
Contributor

Please let me know when this is ready to review. Would be great to start tracking these metrics.

@eLyiN
Copy link
Copy Markdown
Contributor Author

eLyiN commented Oct 15, 2025

Please let me know when this is ready to review. Would be great to start tracking these metrics.

Hi @jacob314 still waiting the pr #8124 , ci failed due some test introduced a few days ago, it should be me ready for review again.

@eLyiN eLyiN force-pushed the split/cli-activity-hooks branch 2 times, most recently from c07d364 to f6a4972 Compare October 20, 2025 19:35
@eLyiN eLyiN marked this pull request as ready for review October 20, 2025 19:35
@eLyiN eLyiN requested a review from a team as a code owner October 20, 2025 19:35
@eLyiN
Copy link
Copy Markdown
Contributor Author

eLyiN commented Oct 20, 2025

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request completes the performance monitoring implementation by adding React hooks for activity monitoring and integrating them into the CLI. The changes include new hooks useActivityMonitoring, useActivityRecorder, and useHistoryWithActivity, updates to useGeminiStream to record user activities, and comprehensive documentation.

My review identifies a few critical issues. The useActivityRecorder hook incorrectly logs all activities as the same type. In useGeminiStream, a generic activity recording function is used instead of the new specific recorders, which prevents detailed activity tracking. Additionally, useHistoryWithActivity doesn't correctly propagate the enableActivityMonitoring flag. These issues should be addressed to ensure the new monitoring system functions as intended.

@eLyiN eLyiN force-pushed the split/cli-activity-hooks branch from f6a4972 to 27c3fc6 Compare October 20, 2025 19:53
@eLyiN
Copy link
Copy Markdown
Contributor Author

eLyiN commented Oct 20, 2025

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request completes the performance monitoring implementation by adding React hooks for activity monitoring and integrating them into the CLI. It also includes comprehensive documentation for the new telemetry system.

My review focuses on improving maintainability and type safety in the new hooks.

  • I've pointed out that useHistoryWithActivity duplicates logic from useHistory instead of wrapping it, which could lead to maintenance issues.
  • I've also suggested strengthening the type safety of the useActivityMonitoring hook to prevent potential bugs with invalid activity types.

The changes in useGeminiStream.ts to record activities at various points seem correct and well-placed. The new tests and documentation are also valuable additions.

@eLyiN
Copy link
Copy Markdown
Contributor Author

eLyiN commented Oct 20, 2025

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request completes the performance monitoring implementation by adding React hooks for activity monitoring and integrating them into the CLI. The changes are well-structured, introducing useActivityMonitoring and useHistoryWithActivity hooks, and correctly instrumenting key user interactions in useGeminiStream. However, there is a critical bug in useHistoryManager where the new callbacks for history changes will never be triggered due to an incorrect pattern for handling side effects with asynchronous state updates. Additionally, a test for useActivityMonitoring is flawed and misleading. My review includes comments to address these critical and high-severity issues.

@eLyiN eLyiN force-pushed the split/cli-activity-hooks branch from b2905c5 to c4f61f0 Compare October 20, 2025 20:27
@eLyiN
Copy link
Copy Markdown
Contributor Author

eLyiN commented Oct 20, 2025

@jacob314, finally, the last PR is ready for review (let me know if you have any requests or thoughts).

@eLyiN eLyiN force-pushed the split/cli-activity-hooks branch from c4f61f0 to fcb0665 Compare October 22, 2025 10:38
@eLyiN
Copy link
Copy Markdown
Contributor Author

eLyiN commented Oct 27, 2025

Heads up. Looks like there are some legitimate test failures. Once the failures are addressed let me know and I will review.

Tests passing, ready for re-review.

@eLyiN eLyiN force-pushed the split/cli-activity-hooks branch 2 times, most recently from eecc1e9 to 7a9ef72 Compare October 27, 2025 21:44
eLyiN added 17 commits October 28, 2025 20:06
…ithActivity

Introduce optional UseHistoryOptions to useHistory hook for extensibility,
allowing callbacks on item addition, update, history clearing, and loading.
Refactor useHistoryWithActivity to leverage the base hook with activity
recording callbacks, eliminating code duplication and reducing overall
lines by 131.
Replace useHistory with useHistoryWithActivityMonitoring to complete
the integration of activity tracking hooks at the top level.

Changes:
- AppContainer.tsx: Replace useHistory() with useHistoryWithActivityMonitoring(config)
- AppContainer.test.tsx: Update mock to use the new hook name

This completes the activity monitoring integration. Activity recording is now
automatically enabled when isPerformanceMonitoringActive() returns true,
providing complete activity tracking for all history operations
(add/update/clear/load) combined with existing stream events.
- Fix mock path for useHistoryManagerWithActivity in AppContainer test
- Add error handling to prevent activity recording crashes
- Remove unused config parameter from useActivityRecorder
- Fix test environment to enable jsdom for React hooks testing

All 14 activity monitoring tests now pass. Changes improve code
robustness and eliminate unused parameters across the codebase.
Add concrete configuration examples and verification steps for
enabling activity monitoring. Include:
- JSON configuration example with local/google targets
- Verification steps for checking monitoring is active
- Complete list of hardcoded defaults (timeouts, thresholds)

Makes it easier for users to understand how to enable and verify
activity monitoring in their telemetry setup.
@eLyiN eLyiN force-pushed the split/cli-activity-hooks branch from 7a9ef72 to eda7499 Compare October 28, 2025 19:20
@eLyiN
Copy link
Copy Markdown
Contributor Author

eLyiN commented Oct 28, 2025

Hey @jacob314 ! I rebased the branch onto origin/main and resolved the conflicts in useGeminiStream.ts.

The conflicts occurred because main added a runInDevTraceSpan wrapper around submitQuery. I preserved your evolved approach with typed recording functions (recordStreamStart/End) and integrated them with the new tracing wrapper.

FYI: Your branch has evolved significantly from the original PR-6 plan:

  • Original: Simple recordUserActivity() calls (~546 LOC)
  • Current: Typed functions with useActivityRecorder hook (17 commits)

The evolution is great for code quality (type safety, semantic names), but it's a larger scope than originally planned. Let me know if you want to:

  1. Keep this evolved version as-is
  2. Simplify back to match original PR-6 scope
  3. Split into 2 smaller PRs

The rebase is done and tests should pass. Please review the conflict resolution in useGeminiStream.ts (lines 821-984) to ensure I preserved your intent correctly.

@bdmorgan
Copy link
Copy Markdown
Collaborator

Hi @eLyiN, thank you so much for your contribution to Gemini CLI! We really appreciate the time and effort you've put into this.

We're making some updates to our contribution process to improve how we track and review changes. Please take a moment to review our recent discussion post: Improving Our Contribution Process & Introducing New Guidelines.

Key Update: Starting January 26, 2026, the Gemini CLI project will require all pull requests to be associated with an existing issue. Any pull requests not linked to an issue by that date will be automatically closed.

Thank you for your understanding and for being a part of our community!

@sehoon38 sehoon38 requested a review from a team as a code owner January 21, 2026 04:12
@bdmorgan
Copy link
Copy Markdown
Collaborator

Thank you for submission to the Gemini CLI project. At this time, we are closing this pull request in order to allow us to better triage and support more recent pull requests against the latest code changes. If you feel like this pull request is a critical contribution to the Gemini CLI project, please associate the pull request with an existing GitHub issue (instructions here: https://docs.github.com/en/issues/tracking-your-work-with-issues/using-issues/linking-a-pull-request-to-an-issue) before reopening. After Monday January 26 2026, any pull requests submitted by contributors without an associated issue will be automatically closed (more information here: #16706). If you do choose to reopen and submit this pull request, please ensure you rebase your changes onto the current main branch before resubmitting. This will help avoid merge conflicts and ensure your contribution is compatible with the latest codebase.

@bdmorgan bdmorgan closed this Jan 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority/p2 Important but can be addressed in a future release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants