From c293151cecc9982f8262dbad0d0b4e032f97816e Mon Sep 17 00:00:00 2001 From: Alexey Karimov Date: Wed, 28 Oct 2026 15:10:11 +0500 Subject: [PATCH 1/2] docs(electron),test(trackers): thermal/cpu-speed-limit README, rounding boundary test, stale comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to #125: README lists thermal_state/cpu_speed_limit and the details-object listener shape (no getCurrentCPUSpeedLimit in Electron); pin Math.round at 62.5/99.6/99.4 boundaries (floor/ceil mutants killed); relaunch comment lists the new traces; reword the subscribe-first comment. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- packages/electron/README.md | 3 ++- packages/electron/src/launch-main.ts | 4 ++-- packages/electron/src/system-traces.ts | 3 ++- packages/trackers/src/electron/power-tracker.test.ts | 9 +++++++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/electron/README.md b/packages/electron/README.md index 4310f12f..5a8af019 100644 --- a/packages/electron/README.md +++ b/packages/electron/README.md @@ -76,7 +76,7 @@ If you pass `app` to `launchMain`, the main process also reports what Electron c - A `child_process_gone` system event (`{ type, reason, exitCode, name, serviceName }`, the last two when Electron supplies them; `serviceName` is e.g. `network.mojom.NetworkService`) when a GPU or utility process exits, including a normal exit (`reason: 'clean-exit'`), so filter on `reason` if you only want crashes. It is recorded only while `captureSystemEvents` is on, and the `child-process-gone` listener is removed again by `client.stop()`. - `environment.hardware.gpu` from `app.getGPUInfo(gpuInfoType)` (`'basic'` by default). A usable result is kept; a failed read, or a different `gpuInfoType` on a later launch, asks again. -A `relaunch()` from `@bugsee/node` or `@bugsee/bugsee` is not Electron-aware: after it `hardware.gpu` is still served, but `app_metrics`, `child_process_gone` and the power, display and window telemetry below stop until `launchMain` runs again. +A `relaunch()` from `@bugsee/node` or `@bugsee/bugsee` is not Electron-aware: after it `hardware.gpu` is still served, but `app_metrics`, `child_process_gone` and the power (including `thermal_state` and `cpu_speed_limit`), display and window telemetry below stop until `launchMain` runs again. ## Power, display and window telemetry @@ -85,6 +85,7 @@ Pass any of `powerMonitor`, `screen` and `BrowserWindow` (the Electron objects, **Pass `screen` safely.** Electron's real `screen` throws on any property read until the app is ready, so `import { screen } from 'electron'` and passing it straight to a `launchMain` call made before `app.whenReady()` turns the display domain off (the failure is reported through `onError`; the launch and the other domains are unaffected). To get display telemetry, either call `launchMain` after `app.whenReady()`, or leave `screen` out of the early call. The `powerMonitor` has the same rule on some platforms. - `powerMonitor` -> `events.system`: `power_suspend`, `power_resume`, `power_lock`, `power_unlock`, `power_ac`, `power_battery`. No params. Platform dependent, per Electron's typings: `lock-screen`/`unlock-screen` are macOS and Windows, `on-ac` is macOS and Windows, and `on-battery` is macOS only, so an event that never fires is not evidence about the power state. +- `powerMonitor` -> system traces `thermal_state` and `cpu_speed_limit` (on whenever `powerMonitor` is passed and `captureSystemTraces` is on). They are event-driven and sampled, never held: a state the OS stops reporting leaves a gap. `thermal_state` is macOS only: it is written once at start from `getCurrentThermalState()` and on every `thermal-state-change` (a state outside Electron's four named ones reads as `unknown`, which writes nothing). `cpu_speed_limit` is an integer percent 0-100 (fractions are rounded to the nearest integer; out-of-range or non-numeric values are dropped) from `speed-limit-change` only, on macOS and Windows: Electron has no `getCurrentCPUSpeedLimit()`, so there is no value until the OS reports a limit, and a measured 0 is written. `thermal-state-change` and `speed-limit-change` pass a details object (`{ state }` / `{ limit }`) as the first listener argument; the tracker also accepts a bare value. - `screen` -> `events.system`: `display_added` and `display_removed` (camelCase `{ id, nativeId, width, height, naturalWidth, naturalHeight, scale, maxFps, rotation, type }`, absent keys omitted; `id` is the display's number in the CURRENT `displays` list (a removed display is no longer listed and takes the number after the last listed one, so correlate display events by `nativeId`, which is the stable key), `width`/`height` are device pixels, `rotation` is quarter turns; a display whose id is -1, Electron's "invalid or not yet known" marker, is ignored) and `display_metrics_changed` (the same plus `changed`, Electron's changed metrics). **Pass `screen` (after app ready) to get the `displays` trace: without it an Electron session has no display trace at all, because renderers never write it (the host owns it).** It also feeds the `displays` system trace, the mobile layout: a list `[{ id, native_id, state?, type?, width, height, natural_width?, natural_height?, scale?, max_fps?, rotation? }]` (`id` 0 is the display showing the KEY window (the focused window, else the most recently focused, else the first created; the primary display when there is no window yet), the others follow in ascending order of Electron's id with Electron's own id in `native_id`; the numbering is recomputed, and the list re-written, when focus or a window move changes the key window's display; `state: "on"` marks id 0; windows on other displays are not recorded; the retired `electron_displays` name is gone), written when it changes. The trace is absent only before its first value: if a later refresh cannot read a usable list, the previous list stays held and keeps being re-recorded (the same open decision as other held traces). - `app` (plus `BrowserWindow` for windows that already exist) -> `events.system`: `window_focus`, `window_blur`, `window_show`, `window_hide` (`{ windowId }`), and `renderer_unresponsive` / `renderer_responsive` (`{ webContentsId }`). These are on whenever `app` is passed (they add two `browser-window-created` listeners, for focus and for show/hide, and one `web-contents-created` listener); pass `captureWindowEvents: false` to opt out of the events. That switch does NOT stop key-window tracking: when `screen` is passed the `displays` trace follows the focused window, so two `browser-window-created` listeners (focus, moved) and focus/blur/moved/closed listeners on every window stay armed even with the option off (with window events on and `screen` passed, `app` carries three `browser-window-created` listeners). Windows created after `launchMain` are tracked too. The hang pair is listened for on `webContents` only, because `BrowserWindow` re-emits it and each hang would otherwise be recorded twice; a `` or `BrowserView` is covered when Electron announces it through `web-contents-created`. diff --git a/packages/electron/src/launch-main.ts b/packages/electron/src/launch-main.ts index c3c265e4..da523258 100644 --- a/packages/electron/src/launch-main.ts +++ b/packages/electron/src/launch-main.ts @@ -460,8 +460,8 @@ export function launchMain(appToken: string, options: LaunchMainOptions): Bugsee // relaunch option, so a relaunch keeps THIS wrapper (closing over the tracker) and `hardware.gpu` keeps being served // (stop() does not clear it; a GPU that had not yet resolved is not re-requested). `systemTraceProducers` and // `wrapSystemEventsSource` are RESET options, so a relaunched client has the plain node producers and events source: - // every Electron trace and event (`app_metrics`, `displays`, `child_process_gone`, `power_*`, `display_*`, - // `window_*`, `renderer_*`) stops until `launchMain` is called again (the carrier client must be cleared by a stop for + // every Electron trace and event (`app_metrics`, `displays`, `thermal_state`, `cpu_speed_limit`, `child_process_gone`, + // `power_*`, `display_*`, `window_*`, `renderer_*`) stops until `launchMain` is called again (the carrier client must be cleared by a stop for // that, as for any launch). const callerExtras = nodeOptions.hardwareExtras; const reportExtrasOnce = reportOncePerMessage(nodeOptions.onError); diff --git a/packages/electron/src/system-traces.ts b/packages/electron/src/system-traces.ts index f88070af..21be2f8f 100644 --- a/packages/electron/src/system-traces.ts +++ b/packages/electron/src/system-traces.ts @@ -194,7 +194,8 @@ class ThermalStateProducer extends TraceProducerBase { if (wire !== undefined) this.emit(TraceName.THERMAL_STATE, wire); } protected onStart(): void { - // subscribe FIRST (arming the listener), then the baseline read: no change can fall between the two + // subscribe before the baseline read so the listener is armed first; both run synchronously on the one JS thread, so + // no change event can fire between them (the order only matters if the read ever becomes asynchronous) this.#off = this.tracker.on('thermalState', (e) => this.#publish(e.state)); const current = this.tracker.current('thermalState'); if (current !== undefined) this.#publish(current.state); diff --git a/packages/trackers/src/electron/power-tracker.test.ts b/packages/trackers/src/electron/power-tracker.test.ts index 8b8c0cfa..ec7225e3 100644 --- a/packages/trackers/src/electron/power-tracker.test.ts +++ b/packages/trackers/src/electron/power-tracker.test.ts @@ -253,6 +253,15 @@ describe('ElectronPowerTracker', () => { off(); expect(m.total()).toBe(0); }); + it('rounds to the NEAREST integer (not floor, not ceil) at the .5 and .6 / .4 boundaries', () => { + const m = fakeMonitor(); + const seen = vi.fn(); + track(m).on('speedLimit', seen); + for (const limit of [62.5, 99.6, 99.4, 0.4, 0.5, 37.49]) { + m.fire('speed-limit-change', { limit }); + } + expect(seen.mock.calls.map((c) => c[0].limit)).toEqual([63, 100, 99, 0, 1, 37]); + }); it('a BARE limit (the listener receives the number itself) is read too', () => { const m = fakeMonitor(); const seen = vi.fn(); From 1a27208d12b3fe1684d65702027b5c91c45b0770 Mon Sep 17 00:00:00 2001 From: Alexey Karimov Date: Wed, 28 Oct 2026 15:18:32 +0500 Subject: [PATCH 2/2] docs(electron): note the systemMetricsSampler exception for the power traces MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- packages/electron/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/electron/README.md b/packages/electron/README.md index 5a8af019..4cddfa06 100644 --- a/packages/electron/README.md +++ b/packages/electron/README.md @@ -85,7 +85,7 @@ Pass any of `powerMonitor`, `screen` and `BrowserWindow` (the Electron objects, **Pass `screen` safely.** Electron's real `screen` throws on any property read until the app is ready, so `import { screen } from 'electron'` and passing it straight to a `launchMain` call made before `app.whenReady()` turns the display domain off (the failure is reported through `onError`; the launch and the other domains are unaffected). To get display telemetry, either call `launchMain` after `app.whenReady()`, or leave `screen` out of the early call. The `powerMonitor` has the same rule on some platforms. - `powerMonitor` -> `events.system`: `power_suspend`, `power_resume`, `power_lock`, `power_unlock`, `power_ac`, `power_battery`. No params. Platform dependent, per Electron's typings: `lock-screen`/`unlock-screen` are macOS and Windows, `on-ac` is macOS and Windows, and `on-battery` is macOS only, so an event that never fires is not evidence about the power state. -- `powerMonitor` -> system traces `thermal_state` and `cpu_speed_limit` (on whenever `powerMonitor` is passed and `captureSystemTraces` is on). They are event-driven and sampled, never held: a state the OS stops reporting leaves a gap. `thermal_state` is macOS only: it is written once at start from `getCurrentThermalState()` and on every `thermal-state-change` (a state outside Electron's four named ones reads as `unknown`, which writes nothing). `cpu_speed_limit` is an integer percent 0-100 (fractions are rounded to the nearest integer; out-of-range or non-numeric values are dropped) from `speed-limit-change` only, on macOS and Windows: Electron has no `getCurrentCPUSpeedLimit()`, so there is no value until the OS reports a limit, and a measured 0 is written. `thermal-state-change` and `speed-limit-change` pass a details object (`{ state }` / `{ limit }`) as the first listener argument; the tracker also accepts a bare value. +- `powerMonitor` -> system traces `thermal_state` and `cpu_speed_limit` (on whenever `powerMonitor` is passed and `captureSystemTraces` is on; supplying the legacy `systemMetricsSampler` option replaces the trace producers, so these stay absent then). They are event-driven and sampled, never held: a state the OS stops reporting leaves a gap. `thermal_state` is macOS only: it is written once at start from `getCurrentThermalState()` and on every `thermal-state-change` (a state outside Electron's four named ones reads as `unknown`, which writes nothing). `cpu_speed_limit` is an integer percent 0-100 (fractions are rounded to the nearest integer; out-of-range or non-numeric values are dropped) from `speed-limit-change` only, on macOS and Windows: Electron has no `getCurrentCPUSpeedLimit()`, so there is no value until the OS reports a limit, and a measured 0 is written. `thermal-state-change` and `speed-limit-change` pass a details object (`{ state }` / `{ limit }`) as the first listener argument; the tracker also accepts a bare value. - `screen` -> `events.system`: `display_added` and `display_removed` (camelCase `{ id, nativeId, width, height, naturalWidth, naturalHeight, scale, maxFps, rotation, type }`, absent keys omitted; `id` is the display's number in the CURRENT `displays` list (a removed display is no longer listed and takes the number after the last listed one, so correlate display events by `nativeId`, which is the stable key), `width`/`height` are device pixels, `rotation` is quarter turns; a display whose id is -1, Electron's "invalid or not yet known" marker, is ignored) and `display_metrics_changed` (the same plus `changed`, Electron's changed metrics). **Pass `screen` (after app ready) to get the `displays` trace: without it an Electron session has no display trace at all, because renderers never write it (the host owns it).** It also feeds the `displays` system trace, the mobile layout: a list `[{ id, native_id, state?, type?, width, height, natural_width?, natural_height?, scale?, max_fps?, rotation? }]` (`id` 0 is the display showing the KEY window (the focused window, else the most recently focused, else the first created; the primary display when there is no window yet), the others follow in ascending order of Electron's id with Electron's own id in `native_id`; the numbering is recomputed, and the list re-written, when focus or a window move changes the key window's display; `state: "on"` marks id 0; windows on other displays are not recorded; the retired `electron_displays` name is gone), written when it changes. The trace is absent only before its first value: if a later refresh cannot read a usable list, the previous list stays held and keeps being re-recorded (the same open decision as other held traces). - `app` (plus `BrowserWindow` for windows that already exist) -> `events.system`: `window_focus`, `window_blur`, `window_show`, `window_hide` (`{ windowId }`), and `renderer_unresponsive` / `renderer_responsive` (`{ webContentsId }`). These are on whenever `app` is passed (they add two `browser-window-created` listeners, for focus and for show/hide, and one `web-contents-created` listener); pass `captureWindowEvents: false` to opt out of the events. That switch does NOT stop key-window tracking: when `screen` is passed the `displays` trace follows the focused window, so two `browser-window-created` listeners (focus, moved) and focus/blur/moved/closed listeners on every window stay armed even with the option off (with window events on and `screen` passed, `app` carries three `browser-window-created` listeners). Windows created after `launchMain` are tracked too. The hang pair is listened for on `webContents` only, because `BrowserWindow` re-emits it and each hang would otherwise be recorded twice; a `` or `BrowserView` is covered when Electron announces it through `web-contents-created`.