Repository navigation
feat(diagnostics): record OS suspend/resume so sleep gaps aren't read as freezes - #10530
Conversation
… as freezes Renderer timers stop across OS sleep, so a multi-hour `renderer_memory` heartbeat gap looks identical to a wedged renderer. That ambiguity sent the uber-crash investigation down a deadlock path that the telemetry later disproved -- and a healthy machine's own trace shows a 427-minute mid-session gap with reason=interval, so the gap alone proves nothing either way. powerMonitor 'resume' was already wired for renderer wake recovery but left no breadcrumb. Stamp suspend and report the measured span on resume so the next freeze report can be told apart from a laptop lid.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThe 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main/system-resume-broadcast.test.ts (1)
24-26: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win
offmock ignores callback identity, so unsubscribe assertions can't catch a reference mismatch.
offdeletes by event key only (_callbackunused), sotoHaveBeenCalledWith(event, expect.any(Function))can't verify the exact listener passed tooffmatches the one registered viaon. A regression where cleanup detaches a different closure than the one registered (which would leak a realpowerMonitorlistener) wouldn't fail this suite.♻️ Optional strengthening
const source = { on: vi.fn((event: PowerLifecycleEvent, callback: ResumeListener) => { listeners.set(event, callback) }), - off: vi.fn((event: PowerLifecycleEvent, _callback: ResumeListener) => { - listeners.delete(event) + off: vi.fn((event: PowerLifecycleEvent, callback: ResumeListener) => { + if (listeners.get(event) === callback) { + listeners.delete(event) + } }) }Also applies to: 76-79
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 5639c1ce-5261-4733-bafb-e47028979d44
📒 Files selected for processing (2)
src/main/system-resume-broadcast.test.tssrc/main/system-resume-broadcast.ts
Adversarial review flagged that the first cut would flood the 30-entry breadcrumb ring and evict the crash evidence it exists to explain. Measured on 7 days of pmset history: 70 user-visible sleep cycles, worst 60-min burst of 7. Median span is 2 SECONDS -- only ~24% run past 60s, so most of that traffic could never explain a gap anyway. Cycles are counted Sleep -> next FULL Wake, since powerMonitor's resume maps to NSWorkspaceDidWake, which does not fire for dark wake. Drop the suspend breadcrumb (suspend now only stamps a timestamp) and emit a single `system_slept` on resume, gated at 60s. That cuts 70 cycles to 17 over the same week (worst burst 3) while still catching every sleep long enough to swallow a 60s renderer heartbeat.
68ce852 to
ac8497d
Compare
The off mock deleted by event name alone, so teardown detaching a different closure than the one registered still passed -- a leak of the real powerMonitor listener would have gone unnoticed. For 'suspend' that leak has no other observable effect through the public API. Co-authored-by: Orca <[email protected]>
powerMonitor 'resume' maps to NSWorkspaceDidWake, which does not fire for dark wake, so macOS can deliver suspend -> suspend -> resume. Overwriting the stamp reported only the trailing segment, and when that segment fell under the 60s gate a 90-minute sleep recorded nothing at all -- leaving the gap looking like the unexplained freeze this is meant to rule out. Co-authored-by: Orca <[email protected]>
The comment claimed maintenance sleeps would flood the ring. Re-measuring pmset over 6 days (78 sleeps, 29 full wakes, 51 dark wakes) shows they resolve as DarkWake, which never fires 'resume', so they never recorded a breadcrumb at all. Real rate is 29 breadcrumbs / 6 days, worst 60-minute burst 4 against a 30-entry ring. The gate's actual job is narrower: skip sleeps shorter than the 60s heartbeat, which cannot open a gap to explain. Co-authored-by: Orca <[email protected]>
… as freezes (stablyai#10530) * feat(diagnostics): record OS suspend/resume so sleep gaps aren't read as freezes Renderer timers stop across OS sleep, so a multi-hour `renderer_memory` heartbeat gap looks identical to a wedged renderer. That ambiguity sent the uber-crash investigation down a deadlock path that the telemetry later disproved -- and a healthy machine's own trace shows a 427-minute mid-session gap with reason=interval, so the gap alone proves nothing either way. powerMonitor 'resume' was already wired for renderer wake recovery but left no breadcrumb. Stamp suspend and report the measured span on resume so the next freeze report can be told apart from a laptop lid. * fix(diagnostics): only record sleeps long enough to hide a heartbeat Adversarial review flagged that the first cut would flood the 30-entry breadcrumb ring and evict the crash evidence it exists to explain. Measured on 7 days of pmset history: 70 user-visible sleep cycles, worst 60-min burst of 7. Median span is 2 SECONDS -- only ~24% run past 60s, so most of that traffic could never explain a gap anyway. Cycles are counted Sleep -> next FULL Wake, since powerMonitor's resume maps to NSWorkspaceDidWake, which does not fire for dark wake. Drop the suspend breadcrumb (suspend now only stamps a timestamp) and emit a single `system_slept` on resume, gated at 60s. That cuts 70 cycles to 17 over the same week (worst burst 3) while still catching every sleep long enough to swallow a 60s renderer heartbeat. * test(diagnostics): assert resume listeners detach by identity The off mock deleted by event name alone, so teardown detaching a different closure than the one registered still passed -- a leak of the real powerMonitor listener would have gone unnoticed. For 'suspend' that leak has no other observable effect through the public API. Co-authored-by: Orca <[email protected]> * fix(diagnostics): span from the first suspend across dark wake powerMonitor 'resume' maps to NSWorkspaceDidWake, which does not fire for dark wake, so macOS can deliver suspend -> suspend -> resume. Overwriting the stamp reported only the trailing segment, and when that segment fell under the 60s gate a 90-minute sleep recorded nothing at all -- leaving the gap looking like the unexplained freeze this is meant to rule out. Co-authored-by: Orca <[email protected]> * docs(diagnostics): correct the threshold rationale to match measurement The comment claimed maintenance sleeps would flood the ring. Re-measuring pmset over 6 days (78 sleeps, 29 full wakes, 51 dark wakes) shows they resolve as DarkWake, which never fires 'resume', so they never recorded a breadcrumb at all. Real rate is 29 breadcrumbs / 6 days, worst 60-minute burst 4 against a 30-entry ring. The gate's actual job is narrower: skip sleeps shorter than the 60s heartbeat, which cannot open a gap to explain. Co-authored-by: Orca <[email protected]> --------- Co-authored-by: Orca <[email protected]>
Why
The uber-crash investigation hinged on a 109-minute
renderer_memoryheartbeat gap that read as a wedged renderer. It isn't diagnostic:reason=interval, plus 35 more over 90s. Nearly every one containspmsetsleep events.Renderer timers stop across OS sleep, so "app frozen" and "laptop closed" produce a byte-identical hole in the telemetry.
powerMonitor.on('resume')was already wired here for renderer wake recovery but left no breadcrumb, so nothing in a crash report could separate the two.What
Suspend stamps a timestamp. Resume emits a single
system_sleptbreadcrumb carrying the measuredsuspendedForMs, past a 60s threshold.Two details are load-bearing, both driven by the same fact:
powerMonitor'sresumemaps toNSWorkspaceDidWake, which does not fire for dark wake.1. Suspend keeps the earliest stamp (
suspendedAt ??= now()). macOS routinely deliverssuspend → (dark wake, no resume) → suspend → resume. Overwriting the stamp would report only the trailing segment — and when that segment falls under the gate, a 90-minute sleep records nothing at all, leaving exactly the unexplained gap this PR exists to rule out. The mutation test for this asserts the breadcrumb is present, not merely smaller.2. The 60s gate. A sleep shorter than the renderer's 60s
RENDERER_MEMORY_SAMPLE_INTERVAL_MSonly delays a heartbeat; it cannot open a multi-minute gap, so recording it spends a slot in the 30-entry ring without explaining anything.Measured by replaying 6 days of this machine's real
pmsethistory (78Sleep, 29 fullWake, 51DarkWake) through the shipped state machine:Note this corrects an earlier revision of this description, which claimed ~70 cycles and a worst burst of 7 and justified the gate as flood control. That was wrong: it counted
Wake Requestsscheduling lines as wakes. Maintenance sleeps overwhelmingly resolve asDarkWake, which never firesresume— so they never recorded a breadcrumb in the first place. There was no flooding risk to solve; the gate's real job is the narrower one above.Testing
7 unit tests, all mutation-verified — each mutation below fails at least one test:
suspendedAt = now()instead of??=(dark-wake truncation)suspendedAtoffcalls deep-equaloncalls)The
offmock is identity-aware, so detaching a different closure than the one registered fails the suite — for'suspend'that leak is otherwise unobservable through the public API.Full suite: 35813 passed. The 2 failures in
src/relay/agent-exec-handler.test.tsreproduce on cleanmainwith this change stashed — pre-existing and unrelated. Typecheck clean on all three projects; lint clean.Scope
Main-process only, no platform branches —
powerMonitorsuspend/resume is supported on macOS, Linux, and Windows. Registered once atsrc/main/index.ts:1875with a paired teardown at:2488;suspendedAtis closure-scoped, so re-registration cannot cross-contaminate.Note on the earlier PRs
This does not revert #10473 / #10485 / #10487. Those fix a real macOS 26 frame-mutation hazard that v1.4.152 genuinely shipped — and per the correction above, that hazard is now the best-supported explanation for this user's freeze, not merely a latent extra. This PR closes the instrumentation gap that made the attribution unfalsifiable in the first place.