From c59dcc9d0cdfe4fe4e74f8ddc85533275892ba8b Mon Sep 17 00:00:00 2001 From: Ting-Wai To <40179554+tingwai@users.noreply.github.com> Date: Fri, 8 Aug 2025 14:39:52 -0700 Subject: [PATCH] fix: Posthog user ID use Continue userId or machine ID --- package-lock.json | 7 ++ package.json | 1 + spec/telemtry.md | 10 +++ src/telemetry/posthogService.test.ts | 113 +++++++++++++++++++++++++++ src/telemetry/posthogService.ts | 22 +++++- 5 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 spec/telemtry.md create mode 100644 src/telemetry/posthogService.test.ts diff --git a/package-lock.json b/package-lock.json index af7b90423..f90ef6dad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -40,6 +40,7 @@ "lowlight": "^3.3.0", "minimatch": "^10.0.3", "node-fetch": "^3.3.2", + "node-machine-id": "^1.1.12", "open": "^10.1.0", "posthog-node": "^5.6.0", "react": "^19.1.0", @@ -12581,6 +12582,12 @@ "url": "https://opencollective.com/node-fetch" } }, + "node_modules/node-machine-id": { + "version": "1.1.12", + "resolved": "https://registry.npmjs.org/node-machine-id/-/node-machine-id-1.1.12.tgz", + "integrity": "sha512-QNABxbrPa3qEIfrE6GOJ7BYIuignnJw7iQ2YPbc3Nla1HzRJjXzZOiikfF8m7eAMfichLt3M4VgLOetqgDmgGQ==", + "license": "MIT" + }, "node_modules/normalize-package-data": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-6.0.2.tgz", diff --git a/package.json b/package.json index a48b0e044..83c1657bb 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "lowlight": "^3.3.0", "minimatch": "^10.0.3", "node-fetch": "^3.3.2", + "node-machine-id": "^1.1.12", "open": "^10.1.0", "posthog-node": "^5.6.0", "react": "^19.1.0", diff --git a/spec/telemtry.md b/spec/telemtry.md new file mode 100644 index 000000000..0ece80739 --- /dev/null +++ b/spec/telemtry.md @@ -0,0 +1,10 @@ +# Continue anonymous Posthog telemetry + +## Behavior + +- Used by Continue for product metrics (not used by customers) +- uses public posthog key in repo +- CONTINUE_CLI_ENABLE_TELEMETRY can be set to false to disable +- non-anonymous and private data like code is never sent to posthog +- Event user ids are the Continue user id is signed in, or a unique machine id if not +- Current events are slash command usage and chat calls diff --git a/src/telemetry/posthogService.test.ts b/src/telemetry/posthogService.test.ts new file mode 100644 index 000000000..b82cceebe --- /dev/null +++ b/src/telemetry/posthogService.test.ts @@ -0,0 +1,113 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +import { machineIdSync } from "node-machine-id"; +import { vi, describe, it, beforeEach, afterEach, expect } from "vitest"; + +// Mock the auth module and node-machine-id +vi.mock("../auth/workos.js", () => ({ + loadAuthConfig: vi.fn(), + isAuthenticatedConfig: vi.fn(), +})); + +vi.mock("node-machine-id", () => ({ + machineIdSync: vi.fn(() => "test-machine-id"), +})); + +// Import after mocks are set up +import { loadAuthConfig, isAuthenticatedConfig } from "../auth/workos.js"; + +import { PosthogService } from "./posthogService.js"; + + +describe("PosthogService", () => { + let tempDir: string; + + beforeEach(() => { + // Create a temporary directory for each test + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "posthog-test-")); + + // Mock os.homedir to return our temp directory + vi.spyOn(os, "homedir").mockReturnValue(tempDir); + + // Clear environment variable + delete process.env.CONTINUE_CLI_ENABLE_TELEMETRY; + + // Reset all mocks + vi.clearAllMocks(); + }); + + afterEach(() => { + // Clean up temp directory + fs.rmSync(tempDir, { recursive: true, force: true }); + + // Restore mocks + vi.restoreAllMocks(); + }); + + describe("uniqueId generation", () => { + it("should use Continue user id when signed in", () => { + const mockUserId = "user123"; + + vi.mocked(loadAuthConfig).mockReturnValue({ userId: mockUserId } as any); + vi.mocked(isAuthenticatedConfig).mockReturnValue(true); + + const service = new PosthogService(); + + // Access private field for testing + const uniqueId = (service as any).uniqueId; + + expect(uniqueId).toBe(mockUserId); + expect(loadAuthConfig).toHaveBeenCalled(); + expect(isAuthenticatedConfig).toHaveBeenCalled(); + }); + + it("should use machine id when not signed in", () => { + const mockMachineId = "test-machine-id"; + + vi.mocked(loadAuthConfig).mockReturnValue(null); + vi.mocked(isAuthenticatedConfig).mockReturnValue(false); + vi.mocked(machineIdSync).mockReturnValue(mockMachineId); + + const service = new PosthogService(); + + // Access private field for testing + const uniqueId = (service as any).uniqueId; + + expect(uniqueId).toBe(mockMachineId); + expect(loadAuthConfig).toHaveBeenCalled(); + expect(isAuthenticatedConfig).toHaveBeenCalled(); + expect(machineIdSync).toHaveBeenCalled(); + }); + + it("should use machine id when auth config is environment-based", () => { + const mockMachineId = "test-machine-id"; + + vi.mocked(loadAuthConfig).mockReturnValue({ accessToken: "token" } as any); + vi.mocked(isAuthenticatedConfig).mockReturnValue(false); + vi.mocked(machineIdSync).mockReturnValue(mockMachineId); + + const service = new PosthogService(); + + // Access private field for testing + const uniqueId = (service as any).uniqueId; + + expect(uniqueId).toBe(mockMachineId); + expect(machineIdSync).toHaveBeenCalled(); + }); + }); + + describe("telemetry enabled/disabled", () => { + it("should be enabled by default", () => { + const service = new PosthogService(); + expect(service.isEnabled).toBe(true); + }); + + it("should be disabled when CONTINUE_CLI_ENABLE_TELEMETRY is 0", () => { + process.env.CONTINUE_CLI_ENABLE_TELEMETRY = "0"; + const service = new PosthogService(); + expect(service.isEnabled).toBe(false); + }); + }); +}); \ No newline at end of file diff --git a/src/telemetry/posthogService.ts b/src/telemetry/posthogService.ts index 8ea9484f7..2f4a7fd7e 100644 --- a/src/telemetry/posthogService.ts +++ b/src/telemetry/posthogService.ts @@ -1,14 +1,17 @@ import os from "node:os"; +import * as node_machine_id from 'node-machine-id'; import type { PostHog as PostHogType } from "posthog-node"; -export class PosthogService { +import { loadAuthConfig, isAuthenticatedConfig } from "../auth/workos.js"; + + export class PosthogService { private os: string | undefined; private uniqueId: string; constructor() { this.os = os.platform(); - this.uniqueId = "NOT_UNIQUE"; + this.uniqueId = this.getEventUserId(); } get isEnabled() { @@ -33,6 +36,21 @@ export class PosthogService { return this._client; } + /** + * - Continue user id if signed in + * - Unique machine id if not signed in + */ + private getEventUserId(): string { + const authConfig = loadAuthConfig(); + + if (isAuthenticatedConfig(authConfig)) { + return authConfig.userId; + } + + // Fall back to unique machine id if not signed in + return node_machine_id.machineIdSync(); + } + async capture(event: string, properties: { [key: string]: any }) { const client = await this.getClient(); if (!client) {