hermes-agent/apps/desktop/electron/desktop-shared-metrics.ts
teknium1 aa83040ca4 fix(telemetry): renderer-crash consent and pending crashes are per window and profile
M11. RendererCrashRecorder held one install-wide `enabled` flag that every window
overwrote with its own profile's switch, so an opted-out window's crash was
written to disk and reported into another profile, and an opted-out window
wiped the opted-in profile's pending crash.

- Consent is kept per webContents id (the IPC sender), with a hash of the
  window's focused profile key; set-enabled now carries that key.
- A crash is recorded only when the crashed window's own profile collects;
  each pending entry is tagged with the profile hash (file v2: {entries:[{profile,
  reason}]}; an untagged v1 file is ignored — whose it was is unknown).
- take() returns only the calling window's profile's reasons; one claim per
  profile (the same window may re-claim after its renderer reloaded); ack drops
  only the claimed count. Turning a profile off drops only that profile's entries.
- main.ts passes the window's webContents id to recordRendererGone.

Tests: renderer-crash-metrics.test.ts moves to the per-window API (contract
change: every call names its window, set-enabled names the profile); one new
invariant test (opted-out window records nothing, another profile neither
drains nor purges) — the base API cannot express it, so RED is shown by the
reviewer's probe instead.
Probe (review/desktop/jsrun/.../renderer-crash-metrics.probe.test.ts, adapted to
window ids):
  before: after opted-out window crash, file exists: true {"v":1,"reasons":["crash"]}
          take by window 2: {"reasons":["crash"]}; A crash record after B-window off: false
  after:  file exists: false; take by window 2: null; A crash record after B-window off: true
2026-09-28 12:43:03 -07:00

48 lines
2 KiB
TypeScript

// Main-process shared-metrics wiring, kept out of main.ts: the once-per-launch startup-latency
// claim, the packaged self-update recorder and the consent-gated renderer-crash recorder the
// renderer drains once a backend is attached.
import { app, BrowserWindow, ipcMain } from 'electron'
import { INSTALL_STAMP } from './install-stamp'
import { registerRendererCrashIpc, RendererCrashRecorder } from './renderer-crash-metrics'
import { registerStartupLatencyIpc } from './startup-latency-ipc'
import type { UpdaterApplyResultWire, UpdaterStrategy } from './updater/index'
import { registerUpdateMetricsIpc, UpdateRunRecorder } from './updater/update-metrics'
export interface DesktopSharedMetrics {
noteUpdateProgress(stage: string): void
/** Run `strategy.apply()`; recorded only when `packaged` (a checkout hand-off is counted by
* `hermes update`'s own receipt). */
trackUpdateApply(packaged: UpdaterStrategy | null, strategy: UpdaterStrategy): Promise<UpdaterApplyResultWire>
/** `installWindowRendererLifecycle` hook: counts a live window's renderer loss (no-op unless that
* window's focused profile collects). */
recordRendererGone(windowId: number, reason: unknown): void
}
export function registerDesktopSharedMetrics(): DesktopSharedMetrics {
registerStartupLatencyIpc()
const recorder = new UpdateRunRecorder({
dir: () => app.getPath('userData'),
appVersion: () => app.getVersion(),
onRecorded: () => {
for (const window of BrowserWindow.getAllWindows()) {
window.webContents.send('hermes:updates:metric:pending')
}
}
})
registerUpdateMetricsIpc(ipcMain, recorder)
const crashes = new RendererCrashRecorder({ dir: app.getPath('userData') })
registerRendererCrashIpc(ipcMain, crashes)
return {
recordRendererGone: (windowId, reason) => crashes.record(windowId, reason),
noteUpdateProgress: stage => recorder.noteProgress(stage),
trackUpdateApply: (packaged, strategy) =>
recorder.track(packaged?.mechanism, INSTALL_STAMP?.commitDate, () => strategy.apply())
}
}