feat(omo-codex): wire Codex SessionStart telemetry for DAU/WAU/MAU

Adds a new Codex plugin component `telemetry` that emits a single
omo_codex_daily_active event (reason: session_start) from every Codex
SessionStart hook, with the same UTC-day deduplication, hashed
installation identifier, and four-flag opt-out as the install-time
event. Previously omo-codex telemetry only fired on install_completed,
so DAU/WAU/MAU under-reported real Codex usage.

- New plugin component packages/omo-codex/plugin/components/telemetry/
  mirrors the rules/comment-checker/lsp pattern: own src/, tsc build,
  vitest tests, package.json (posthog-node dep), hooks/hooks.json.
- src/codex-hook.ts wraps createPluginPostHog().trackActive(..., "session_start")
  with safeShutdown so Codex session startup never blocks on telemetry.
- Plugin root hooks.json + workspaces register the new component
  alongside rules and ultrawork on SessionStart.
- Aggregate test expectations updated to include the telemetry directory.
- cross-package-equivalence.test.ts pins product-identity constants and
  shouldDisablePostHog behavior to stay byte-equivalent between the CLI
  installer (src/telemetry/) and the plugin runtime
  (plugin/components/telemetry/src/), so the two PostHog sources never
  drift on event name, distinct_id base, dedup file path, or opt-out
  flags.
- PostHogActivityReason union in the CLI-side posthog.ts gains
  "session_start" so future CLI paths can emit the same reason without
  a type break.
This commit is contained in:
YeonGyu-Kim
2026-05-26 15:30:41 +09:00
parent b886ca9ca4
commit bd867019f9
22 changed files with 1128 additions and 3 deletions
@@ -0,0 +1,79 @@
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,
};
}