feat(omo-claude): fork telemetry with distinct identity and derived literals
omo_claude_daily_active event + omo-claude: distinct-id salt; PostHog key/host/ LEGACY_PARENT_PACKAGE kept identical (shared project). platform + tmp-fallback derived from constants (no literals). 4-flag opt-out incl inherited OMO_*. cross-package-equivalence + cross-product distinctness tests (RED->GREEN). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import { renameSync, unlinkSync, writeFileSync } from "node:fs"
|
||||
|
||||
export function writeFileAtomically(filePath: string, content: string): void {
|
||||
const tempPath = `${filePath}.tmp`
|
||||
writeFileSync(tempPath, content, "utf-8")
|
||||
|
||||
try {
|
||||
renameSync(tempPath, filePath)
|
||||
} catch (error) {
|
||||
const isPermissionError =
|
||||
error instanceof Error &&
|
||||
(error.message.includes("EPERM") || error.message.includes("EACCES"))
|
||||
|
||||
if (process.platform === "win32" && isPermissionError) {
|
||||
unlinkSync(filePath)
|
||||
renameSync(tempPath, filePath)
|
||||
return
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { accessSync, constants, mkdirSync } from "node:fs"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
|
||||
import { CACHE_DIR_NAME } from "./product-identity.js"
|
||||
|
||||
type OsProvider = Pick<typeof os, "homedir" | "tmpdir">
|
||||
|
||||
let osProviderOverride: OsProvider | null = null
|
||||
|
||||
export function getOsProvider(): OsProvider {
|
||||
return osProviderOverride ?? os
|
||||
}
|
||||
|
||||
/** @internal test-only */
|
||||
export function __setOsProviderForTesting(provider: OsProvider): void {
|
||||
osProviderOverride = provider
|
||||
}
|
||||
|
||||
/** @internal test-only */
|
||||
export function __resetOsProviderForTesting(): void {
|
||||
osProviderOverride = null
|
||||
}
|
||||
|
||||
function resolveWritableDirectory(preferredDir: string, fallbackSuffix: string): string {
|
||||
try {
|
||||
mkdirSync(preferredDir, { recursive: true })
|
||||
accessSync(preferredDir, constants.W_OK)
|
||||
return preferredDir
|
||||
} catch {
|
||||
const fallbackDir = path.join(getOsProvider().tmpdir(), fallbackSuffix)
|
||||
mkdirSync(fallbackDir, { recursive: true })
|
||||
return fallbackDir
|
||||
}
|
||||
}
|
||||
|
||||
export function getDataDir(): string {
|
||||
const preferredDataDir =
|
||||
process.env["XDG_DATA_HOME"] ?? path.join(getOsProvider().homedir(), ".local", "share")
|
||||
return resolveWritableDirectory(preferredDataDir, `${CACHE_DIR_NAME}-data`)
|
||||
}
|
||||
|
||||
export function getActivityStateDir(): string {
|
||||
return path.join(getDataDir(), CACHE_DIR_NAME)
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import {
|
||||
DEFAULT_POSTHOG_API_KEY,
|
||||
DEFAULT_POSTHOG_HOST,
|
||||
} from "./product-identity.js"
|
||||
|
||||
function normalizeEnvValue(value: string | undefined): string | undefined {
|
||||
return value?.trim().toLowerCase()
|
||||
}
|
||||
|
||||
function isDisableFlag(value: string | undefined): boolean {
|
||||
const normalized = normalizeEnvValue(value)
|
||||
return normalized === "1" || normalized === "true"
|
||||
}
|
||||
|
||||
function isTelemetryOptOutFlag(value: string | undefined): boolean {
|
||||
const normalized = normalizeEnvValue(value)
|
||||
return normalized === "0" || normalized === "false" || normalized === "no"
|
||||
}
|
||||
|
||||
export function shouldDisablePostHog(): boolean {
|
||||
return (
|
||||
isDisableFlag(process.env["OMO_DISABLE_POSTHOG"]) ||
|
||||
isTelemetryOptOutFlag(process.env["OMO_SEND_ANONYMOUS_TELEMETRY"]) ||
|
||||
isDisableFlag(process.env["OMO_CLAUDE_DISABLE_POSTHOG"]) ||
|
||||
isTelemetryOptOutFlag(process.env["OMO_CLAUDE_SEND_ANONYMOUS_TELEMETRY"])
|
||||
)
|
||||
}
|
||||
|
||||
export function getPostHogApiKey(): string {
|
||||
const explicit = process.env["POSTHOG_API_KEY"]
|
||||
if (explicit === undefined) {
|
||||
return DEFAULT_POSTHOG_API_KEY
|
||||
}
|
||||
return explicit.trim()
|
||||
}
|
||||
|
||||
export function hasPostHogApiKey(): boolean {
|
||||
return getPostHogApiKey().length > 0
|
||||
}
|
||||
|
||||
export function getPostHogHost(): string {
|
||||
return process.env["POSTHOG_HOST"]?.trim() || DEFAULT_POSTHOG_HOST
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import { existsSync, mkdirSync, readFileSync } from "node:fs"
|
||||
import { join } from "node:path"
|
||||
|
||||
import { writeFileAtomically } from "./atomic-write.js"
|
||||
import { getActivityStateDir } from "./data-path.js"
|
||||
|
||||
export type PostHogActivityState = {
|
||||
readonly lastActiveDayUTC?: string
|
||||
}
|
||||
|
||||
export type PostHogActivityCaptureState = {
|
||||
readonly dayUTC: string
|
||||
readonly captureDaily: boolean
|
||||
}
|
||||
|
||||
const POSTHOG_ACTIVITY_STATE_FILE = "posthog-activity.json"
|
||||
|
||||
function getPostHogActivityStateFilePath(): string {
|
||||
return join(getActivityStateDir(), POSTHOG_ACTIVITY_STATE_FILE)
|
||||
}
|
||||
|
||||
function getUtcDayString(date: Date): string {
|
||||
return date.toISOString().slice(0, 10)
|
||||
}
|
||||
|
||||
function isPostHogActivityState(value: unknown): value is PostHogActivityState {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value)
|
||||
}
|
||||
|
||||
function readPostHogActivityState(): PostHogActivityState {
|
||||
const stateFilePath = getPostHogActivityStateFilePath()
|
||||
|
||||
if (!existsSync(stateFilePath)) {
|
||||
return {}
|
||||
}
|
||||
|
||||
try {
|
||||
const stateContent = readFileSync(stateFilePath, "utf-8")
|
||||
const stateJson: unknown = JSON.parse(stateContent)
|
||||
|
||||
if (!isPostHogActivityState(stateJson)) {
|
||||
return {}
|
||||
}
|
||||
|
||||
return stateJson
|
||||
} catch {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
function writePostHogActivityState(nextState: PostHogActivityState): void {
|
||||
const stateDir = getActivityStateDir()
|
||||
const stateFilePath = getPostHogActivityStateFilePath()
|
||||
|
||||
try {
|
||||
mkdirSync(stateDir, { recursive: true })
|
||||
writeFileAtomically(stateFilePath, `${JSON.stringify(nextState, null, 2)}\n`)
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
export function getPostHogActivityCaptureState(
|
||||
now: Date = new Date(),
|
||||
): PostHogActivityCaptureState {
|
||||
const state = readPostHogActivityState()
|
||||
const dayUTC = getUtcDayString(now)
|
||||
const captureDaily = state.lastActiveDayUTC !== dayUTC
|
||||
|
||||
if (captureDaily) {
|
||||
writePostHogActivityState({
|
||||
...state,
|
||||
lastActiveDayUTC: dayUTC,
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
dayUTC,
|
||||
captureDaily,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
export const PRODUCT_NAME = "omo-claude";
|
||||
export const PACKAGE_NAME = "@oh-my-opencode/omo-claude";
|
||||
export const CACHE_DIR_NAME = "omo-claude";
|
||||
export const EVENT_NAME = "omo_claude_daily_active";
|
||||
export const LEGACY_PARENT_PACKAGE = "oh-my-opencode";
|
||||
export const DEFAULT_POSTHOG_HOST = "https://us.i.posthog.com";
|
||||
export const DEFAULT_POSTHOG_API_KEY = "phc_CFJhj5HyvA62QPhvyaUCtaq23aUfznnijg5VaaGkNk74";
|
||||
|
||||
type ComponentPackageManifest = { readonly version?: string };
|
||||
|
||||
function isComponentPackageManifest(value: unknown): value is ComponentPackageManifest {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function readComponentVersionFromManifest(): string {
|
||||
try {
|
||||
const manifestUrl = new URL("../package.json", import.meta.url);
|
||||
const manifestText = readFileSync(manifestUrl, "utf-8");
|
||||
const parsed: unknown = JSON.parse(manifestText);
|
||||
if (isComponentPackageManifest(parsed) && typeof parsed.version === "string") {
|
||||
return parsed.version;
|
||||
}
|
||||
} catch {
|
||||
return "0.0.0";
|
||||
}
|
||||
return "0.0.0";
|
||||
}
|
||||
|
||||
const COMPONENT_VERSION_CACHE = readComponentVersionFromManifest();
|
||||
|
||||
export function getComponentVersion(): string {
|
||||
return COMPONENT_VERSION_CACHE;
|
||||
}
|
||||
Reference in New Issue
Block a user