From d1bc25a6ce2c7a3f74a88a3baa6a5f8d45c4dd51 Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Mon, 27 Apr 2026 17:14:00 +0900 Subject: [PATCH] fix(posthog): guard CPU telemetry collection Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- src/shared/posthog.test.ts | 45 ++++++++++++++++++++++++++++++++++++++ src/shared/posthog.ts | 15 +++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/shared/posthog.test.ts b/src/shared/posthog.test.ts index c2f278add..824774343 100644 --- a/src/shared/posthog.test.ts +++ b/src/shared/posthog.test.ts @@ -54,4 +54,49 @@ describe("posthog client creation", () => { expect(() => pluginPostHog.trackActive("plugin", "plugin_loaded")).not.toThrow() await expect(pluginPostHog.shutdown()).resolves.toBeUndefined() }) + + it("creates a plugin client when os.cpus throws", async () => { + // given + process.env.OMO_DISABLE_POSTHOG = "0" + process.env.OMO_SEND_ANONYMOUS_TELEMETRY = "1" + process.env.POSTHOG_API_KEY = "test-api-key" + + mock.module("os", () => ({ + default: { + arch: () => "x64", + cpus: () => { + throw new Error("Failed to get CPU information") + }, + hostname: () => "test-host", + platform: () => "linux", + release: () => "6.8.0-arch1-1", + totalmem: () => 8 * 1024 * 1024 * 1024, + type: () => "Linux", + }, + })) + + mock.module("posthog-node", () => ({ + PostHog: class { + capture() {} + captureException() {} + async shutdown() {} + }, + })) + + const { createPluginPostHog } = await importPostHogModule() + + // when + const pluginPostHog = createPluginPostHog() + + // then + expect(() => + pluginPostHog.capture({ + distinctId: "plugin", + event: "plugin_loaded", + }), + ).not.toThrow() + expect(() => pluginPostHog.captureException(new Error("plugin failure"), "plugin")).not.toThrow() + expect(() => pluginPostHog.trackActive("plugin", "plugin_loaded")).not.toThrow() + await expect(pluginPostHog.shutdown()).resolves.toBeUndefined() + }) }) diff --git a/src/shared/posthog.ts b/src/shared/posthog.ts index 6e96853d0..1e0eea6ae 100644 --- a/src/shared/posthog.ts +++ b/src/shared/posthog.ts @@ -55,7 +55,18 @@ function getPostHogHost(): string { return process.env.POSTHOG_HOST?.trim() || DEFAULT_POSTHOG_HOST } +function safeCpus(): { length: number; model: string | undefined } { + try { + const cpus = os.cpus() + return { length: cpus.length, model: cpus[0]?.model } + } catch { + return { length: 0, model: undefined } + } +} + function getSharedProperties(source: PostHogSource): NonNullable { + const cpus = safeCpus() + return { platform: "oh-my-opencode", package_name: PUBLISHED_PACKAGE_NAME, @@ -68,8 +79,8 @@ function getSharedProperties(source: PostHogSource): NonNullable