File size: 2,609 Bytes
52efc7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { stat } from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import { debugLogger } from '@google/gemini-cli-core';
import {
  type CommandContext,
  type SlashCommand,
  CommandKind,
} from './types.js';
import { MessageType } from '../types.js';
import { formatBytes } from '../utils/formatters.js';
import { captureHeapSnapshot } from '../utils/memorySnapshot.js';

export const bugMemoryCommand: SlashCommand = {
  name: 'bug-memory',
  description: 'Capture a V8 heap snapshot to disk to attach to a bug report',
  kind: CommandKind.BUILT_IN,
  autoExecute: false,
  action: async (context: CommandContext): Promise<void> => {
    const tempDir =
      context.services.agentContext?.config?.storage?.getProjectTempDir();
    if (!tempDir) {
      context.ui.addItem(
        {
          type: MessageType.ERROR,
          text: 'Cannot capture heap snapshot: project temp directory is unavailable.',
        },
        Date.now(),
      );
      return;
    }

    const filePath = path.join(
      tempDir,
      `bug-memory-${Date.now()}.heapsnapshot`,
    );
    const rss = process.memoryUsage().rss;

    context.ui.addItem(
      {
        type: MessageType.INFO,
        text: `Capturing V8 heap snapshot (current RSS: ${formatBytes(rss)}).\nThis can take 20+ seconds and the CLI may be temporarily unresponsive — please do not exit.\nDestination: ${filePath}`,
      },
      Date.now(),
    );

    const startedAt = Date.now();
    try {
      await captureHeapSnapshot(filePath);
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error);
      debugLogger.error(`Failed to capture heap snapshot: ${message}`);
      context.ui.addItem(
        {
          type: MessageType.ERROR,
          text: `Failed to capture heap snapshot: ${message}`,
        },
        Date.now(),
      );
      return;
    }

    const durationMs = Date.now() - startedAt;
    let sizeText = '';
    try {
      const { size } = await stat(filePath);
      sizeText = ` (${formatBytes(size)})`;
    } catch {
      // Size reporting is best-effort; the snapshot itself was captured successfully.
    }

    context.ui.addItem(
      {
        type: MessageType.INFO,
        text: `Heap snapshot saved${sizeText} in ${durationMs}ms:\n${filePath}\n\nLoad it in Chrome DevTools → Memory → "Load" to analyze. Attach it to your bug report only if it does not contain sensitive information.`,
      },
      Date.now(),
    );
  },
};