Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { InstallationManager } from '../../utils/installationManager.js';

import si from 'systeminformation';
import type { Systeminformation } from 'systeminformation';
import * as os from 'node:os';

interface CustomMatchers<R = unknown> {
toHaveMetadataValue: ([key, value]: [EventMetadataKey, string]) => R;
Expand Down Expand Up @@ -120,6 +121,7 @@ vi.mock('node:os', async (importOriginal) => {
return {
...actual,
cpus: vi.fn(() => [{ model: 'Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz' }]),
availableParallelism: vi.fn(() => 8),
totalmem: vi.fn(() => 32 * 1024 * 1024 * 1024),
};
});
Expand Down Expand Up @@ -438,6 +440,26 @@ describe('ClearcutLogger', () => {
});
});

it('handles empty os.cpus() gracefully', async () => {
const { logger, loggerConfig } = setup({});
vi.mocked(os.cpus).mockReturnValueOnce([]);

await logger?.logStartSessionEvent(new StartSessionEvent(loggerConfig));

const event = logger?.createLogEvent(EventNames.API_ERROR, []);
const metadata = event?.event_metadata[0];

const cpuInfoEntry = metadata?.find(
(m) => m.gemini_cli_key === EventMetadataKey.GEMINI_CLI_CPU_INFO,
);
expect(cpuInfoEntry).toBeUndefined();

const cpuCoresEntry = metadata?.find(
(m) => m.gemini_cli_key === EventMetadataKey.GEMINI_CLI_CPU_CORES,
);
expect(cpuCoresEntry?.value).toBe('8');
});

type SurfaceDetectionTestCase = {
name: string;
env: Record<string, string | undefined>;
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/telemetry/clearcut-logger/clearcut-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,14 +615,17 @@ export class ClearcutLogger {

// Add hardware information only to the start session event
const cpus = os.cpus();
data.push(
{
if (cpus && cpus.length > 0) {
data.push({
gemini_cli_key: EventMetadataKey.GEMINI_CLI_CPU_INFO,
value: cpus[0].model,
},
});
Comment on lines +618 to +622
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The conditional check if (cpus && cpus.length > 0) is good for preventing crashes when os.cpus() returns an empty array. This directly addresses the critical crash described in the PR summary.

}

data.push(
{
gemini_cli_key: EventMetadataKey.GEMINI_CLI_CPU_CORES,
value: cpus.length.toString(),
value: os.availableParallelism().toString(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Using os.availableParallelism() instead of cpus.length is a good improvement for determining the number of CPU cores. It's a more modern and reliable API, especially since the project requires Node.js >= v20.0.0, where this function is available.

},
{
gemini_cli_key: EventMetadataKey.GEMINI_CLI_RAM_TOTAL_GB,
Expand Down
Loading