fix(core): resolve crash in ClearcutLogger when os.cpus() is empty (#19555)

This commit is contained in:
Adib234
2026-02-19 15:13:28 -05:00
committed by GitHub
parent 3408542a66
commit 264c7aceaa
2 changed files with 29 additions and 4 deletions

View File

@@ -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;
@@ -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),
};
});
@@ -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>;

View File

@@ -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,
},
});
}
data.push(
{
gemini_cli_key: EventMetadataKey.GEMINI_CLI_CPU_CORES,
value: cpus.length.toString(),
value: os.availableParallelism().toString(),
},
{
gemini_cli_key: EventMetadataKey.GEMINI_CLI_RAM_TOTAL_GB,