refactor(telemetry): drop plugin_loaded capture from plugin entry
This commit is contained in:
@@ -34,8 +34,6 @@ const mockCreatePluginPostHog = mock(() => ({
|
|||||||
trackActive: () => {
|
trackActive: () => {
|
||||||
throw new Error("telemetry failed")
|
throw new Error("telemetry failed")
|
||||||
},
|
},
|
||||||
capture: mock(() => {}),
|
|
||||||
captureException: mock(() => {}),
|
|
||||||
shutdown: mock(async () => {}),
|
shutdown: mock(async () => {}),
|
||||||
}))
|
}))
|
||||||
const mockGetPostHogDistinctId = mock(() => "plugin-distinct-id")
|
const mockGetPostHogDistinctId = mock(() => "plugin-distinct-id")
|
||||||
@@ -104,12 +102,6 @@ function installModuleMocks(): void {
|
|||||||
createPluginPostHog: mockCreatePluginPostHog,
|
createPluginPostHog: mockCreatePluginPostHog,
|
||||||
getPostHogDistinctId: mockGetPostHogDistinctId,
|
getPostHogDistinctId: mockGetPostHogDistinctId,
|
||||||
}))
|
}))
|
||||||
mock.module("./shared/posthog-activity-state", () => ({
|
|
||||||
getPluginLoadedCaptureState: () => ({
|
|
||||||
dayUTC: "2026-04-18",
|
|
||||||
capturePluginLoaded: true,
|
|
||||||
}),
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("oh-my-openagent telemetry isolation", () => {
|
describe("oh-my-openagent telemetry isolation", () => {
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import { installAgentSortShim } from "./shared/agent-sort-shim"
|
|||||||
import { detectExternalSkillPlugin, getSkillPluginConflictWarning } from "./shared/external-plugin-detector"
|
import { detectExternalSkillPlugin, getSkillPluginConflictWarning } from "./shared/external-plugin-detector"
|
||||||
import { startBackgroundCheck as startTmuxCheck } from "./tools/interactive-bash"
|
import { startBackgroundCheck as startTmuxCheck } from "./tools/interactive-bash"
|
||||||
import { createPluginPostHog, getPostHogDistinctId } from "./shared/posthog"
|
import { createPluginPostHog, getPostHogDistinctId } from "./shared/posthog"
|
||||||
import { getPluginLoadedCaptureState } from "./shared/posthog-activity-state"
|
|
||||||
|
|
||||||
const serverPlugin: Plugin = async (input, _options): Promise<Hooks> => {
|
const serverPlugin: Plugin = async (input, _options): Promise<Hooks> => {
|
||||||
installAgentSortShim()
|
installAgentSortShim()
|
||||||
@@ -44,27 +43,6 @@ const serverPlugin: Plugin = async (input, _options): Promise<Hooks> => {
|
|||||||
} catch {
|
} catch {
|
||||||
// telemetry failure is non-fatal, silently ignore
|
// telemetry failure is non-fatal, silently ignore
|
||||||
}
|
}
|
||||||
let pluginLoadedCaptureState: ReturnType<typeof getPluginLoadedCaptureState> | null = null
|
|
||||||
try {
|
|
||||||
pluginLoadedCaptureState = getPluginLoadedCaptureState()
|
|
||||||
} catch {
|
|
||||||
// telemetry failure is non-fatal, silently ignore
|
|
||||||
}
|
|
||||||
if (pluginLoadedCaptureState?.capturePluginLoaded) {
|
|
||||||
try {
|
|
||||||
posthog.capture({
|
|
||||||
distinctId,
|
|
||||||
event: "plugin_loaded",
|
|
||||||
properties: {
|
|
||||||
entry_point: "plugin",
|
|
||||||
has_openclaw: !!pluginConfig.openclaw,
|
|
||||||
tmux_enabled: isTmuxIntegrationEnabled(pluginConfig),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
} catch {
|
|
||||||
// telemetry failure is non-fatal, silently ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (pluginConfig.openclaw) {
|
if (pluginConfig.openclaw) {
|
||||||
await initializeOpenClaw(pluginConfig.openclaw)
|
await initializeOpenClaw(pluginConfig.openclaw)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ describe("getPostHogActivityCaptureState", () => {
|
|||||||
rmSync(dataHomePath, { recursive: true, force: true })
|
rmSync(dataHomePath, { recursive: true, force: true })
|
||||||
})
|
})
|
||||||
|
|
||||||
it("preserves lastPluginLoadedDayUTC when writing lastActiveDayUTC", async () => {
|
it("preserves unrelated state fields when writing lastActiveDayUTC", async () => {
|
||||||
// given
|
// given
|
||||||
const dataHomePath = createDataHomePath()
|
const dataHomePath = createDataHomePath()
|
||||||
const cachePath = join(dataHomePath, "oh-my-opencode")
|
const cachePath = join(dataHomePath, "oh-my-opencode")
|
||||||
@@ -168,128 +168,3 @@ describe("getPostHogActivityCaptureState", () => {
|
|||||||
rmSync(dataHomePath, { recursive: true, force: true })
|
rmSync(dataHomePath, { recursive: true, force: true })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("getPluginLoadedCaptureState", () => {
|
|
||||||
it("returns capturePluginLoaded=true when activity file does not exist", async () => {
|
|
||||||
// given
|
|
||||||
const dataHomePath = createDataHomePath()
|
|
||||||
process.env.XDG_DATA_HOME = dataHomePath
|
|
||||||
const { getPluginLoadedCaptureState } = await importPostHogActivityStateModule()
|
|
||||||
|
|
||||||
// when
|
|
||||||
const result = getPluginLoadedCaptureState(new Date("2026-04-11T10:15:00.000Z"))
|
|
||||||
|
|
||||||
// then
|
|
||||||
expect(result).toEqual({
|
|
||||||
dayUTC: "2026-04-11",
|
|
||||||
capturePluginLoaded: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
rmSync(dataHomePath, { recursive: true, force: true })
|
|
||||||
})
|
|
||||||
|
|
||||||
it("returns capturePluginLoaded=false when lastPluginLoadedDayUTC matches today", async () => {
|
|
||||||
// given
|
|
||||||
const dataHomePath = createDataHomePath()
|
|
||||||
const cachePath = join(dataHomePath, "oh-my-opencode")
|
|
||||||
mkdirSync(cachePath, { recursive: true })
|
|
||||||
writeFileSync(
|
|
||||||
join(cachePath, "posthog-activity.json"),
|
|
||||||
`${JSON.stringify({
|
|
||||||
lastPluginLoadedDayUTC: "2026-04-11",
|
|
||||||
})}\n`,
|
|
||||||
)
|
|
||||||
process.env.XDG_DATA_HOME = dataHomePath
|
|
||||||
const { getPluginLoadedCaptureState } = await importPostHogActivityStateModule()
|
|
||||||
|
|
||||||
// when
|
|
||||||
const result = getPluginLoadedCaptureState(new Date("2026-04-11T10:15:00.000Z"))
|
|
||||||
|
|
||||||
// then
|
|
||||||
expect(result).toEqual({
|
|
||||||
dayUTC: "2026-04-11",
|
|
||||||
capturePluginLoaded: false,
|
|
||||||
})
|
|
||||||
|
|
||||||
rmSync(dataHomePath, { recursive: true, force: true })
|
|
||||||
})
|
|
||||||
|
|
||||||
it("returns capturePluginLoaded=true when lastPluginLoadedDayUTC is from a previous day", async () => {
|
|
||||||
// given
|
|
||||||
const dataHomePath = createDataHomePath()
|
|
||||||
const cachePath = join(dataHomePath, "oh-my-opencode")
|
|
||||||
mkdirSync(cachePath, { recursive: true })
|
|
||||||
writeFileSync(
|
|
||||||
join(cachePath, "posthog-activity.json"),
|
|
||||||
`${JSON.stringify({
|
|
||||||
lastPluginLoadedDayUTC: "2026-04-10",
|
|
||||||
})}\n`,
|
|
||||||
)
|
|
||||||
process.env.XDG_DATA_HOME = dataHomePath
|
|
||||||
const { getPluginLoadedCaptureState } = await importPostHogActivityStateModule()
|
|
||||||
|
|
||||||
// when
|
|
||||||
const result = getPluginLoadedCaptureState(new Date("2026-04-11T10:15:00.000Z"))
|
|
||||||
|
|
||||||
// then
|
|
||||||
expect(result).toEqual({
|
|
||||||
dayUTC: "2026-04-11",
|
|
||||||
capturePluginLoaded: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
rmSync(dataHomePath, { recursive: true, force: true })
|
|
||||||
})
|
|
||||||
|
|
||||||
it("preserves lastActiveDayUTC when writing lastPluginLoadedDayUTC", async () => {
|
|
||||||
// given
|
|
||||||
const dataHomePath = createDataHomePath()
|
|
||||||
const cachePath = join(dataHomePath, "oh-my-opencode")
|
|
||||||
mkdirSync(cachePath, { recursive: true })
|
|
||||||
writeFileSync(
|
|
||||||
join(cachePath, "posthog-activity.json"),
|
|
||||||
`${JSON.stringify({
|
|
||||||
lastActiveDayUTC: "2026-04-11",
|
|
||||||
lastPluginLoadedDayUTC: "2026-04-10",
|
|
||||||
})}\n`,
|
|
||||||
)
|
|
||||||
process.env.XDG_DATA_HOME = dataHomePath
|
|
||||||
const { getPluginLoadedCaptureState } = await importPostHogActivityStateModule()
|
|
||||||
|
|
||||||
// when
|
|
||||||
getPluginLoadedCaptureState(new Date("2026-04-11T10:15:00.000Z"))
|
|
||||||
|
|
||||||
// then
|
|
||||||
const persistedState = JSON.parse(
|
|
||||||
readFileSync(join(cachePath, "posthog-activity.json"), "utf-8"),
|
|
||||||
)
|
|
||||||
expect(persistedState).toEqual({
|
|
||||||
lastActiveDayUTC: "2026-04-11",
|
|
||||||
lastPluginLoadedDayUTC: "2026-04-11",
|
|
||||||
})
|
|
||||||
|
|
||||||
rmSync(dataHomePath, { recursive: true, force: true })
|
|
||||||
})
|
|
||||||
|
|
||||||
it("does not rewrite state when lastPluginLoadedDayUTC matches today", async () => {
|
|
||||||
// given
|
|
||||||
const dataHomePath = createDataHomePath()
|
|
||||||
const cachePath = join(dataHomePath, "oh-my-opencode")
|
|
||||||
mkdirSync(cachePath, { recursive: true })
|
|
||||||
const initialPayload = `${JSON.stringify({
|
|
||||||
lastActiveDayUTC: "2026-04-10",
|
|
||||||
lastPluginLoadedDayUTC: "2026-04-11",
|
|
||||||
})}\n`
|
|
||||||
writeFileSync(join(cachePath, "posthog-activity.json"), initialPayload)
|
|
||||||
process.env.XDG_DATA_HOME = dataHomePath
|
|
||||||
const { getPluginLoadedCaptureState } = await importPostHogActivityStateModule()
|
|
||||||
|
|
||||||
// when
|
|
||||||
getPluginLoadedCaptureState(new Date("2026-04-11T10:15:00.000Z"))
|
|
||||||
|
|
||||||
// then
|
|
||||||
const persistedPayload = readFileSync(join(cachePath, "posthog-activity.json"), "utf-8")
|
|
||||||
expect(persistedPayload).toBe(initialPayload)
|
|
||||||
|
|
||||||
rmSync(dataHomePath, { recursive: true, force: true })
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import { writeFileAtomically } from "./write-file-atomically"
|
|||||||
|
|
||||||
type PostHogActivityState = {
|
type PostHogActivityState = {
|
||||||
lastActiveDayUTC?: string
|
lastActiveDayUTC?: string
|
||||||
lastPluginLoadedDayUTC?: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostHogActivityCaptureState = {
|
type PostHogActivityCaptureState = {
|
||||||
@@ -16,11 +15,6 @@ type PostHogActivityCaptureState = {
|
|||||||
captureDaily: boolean
|
captureDaily: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
type PluginLoadedCaptureState = {
|
|
||||||
dayUTC: string
|
|
||||||
capturePluginLoaded: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
const POSTHOG_ACTIVITY_STATE_FILE = "posthog-activity.json"
|
const POSTHOG_ACTIVITY_STATE_FILE = "posthog-activity.json"
|
||||||
|
|
||||||
function getPostHogActivityStateFilePath(): string {
|
function getPostHogActivityStateFilePath(): string {
|
||||||
@@ -89,22 +83,3 @@ export function getPostHogActivityCaptureState(now: Date = new Date()): PostHogA
|
|||||||
captureDaily,
|
captureDaily,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPluginLoadedCaptureState(now: Date = new Date()): PluginLoadedCaptureState {
|
|
||||||
const state = readPostHogActivityState()
|
|
||||||
const dayUTC = getUtcDayString(now)
|
|
||||||
|
|
||||||
const capturePluginLoaded = state.lastPluginLoadedDayUTC !== dayUTC
|
|
||||||
|
|
||||||
if (capturePluginLoaded) {
|
|
||||||
writePostHogActivityState({
|
|
||||||
...state,
|
|
||||||
lastPluginLoadedDayUTC: dayUTC,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
dayUTC,
|
|
||||||
capturePluginLoaded,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user