Skip to content

fix(desktop): media playback exempts the capture idle gate - #11556

Merged
kodjima33 merged 2 commits into
mainfrom
kodjima33/idle-media-playback
Aug 14, 2026
Merged

kodjima33 merged 2 commits into
mainfrom
kodjima33/idle-media-playback

Conversation

@kodjima33

@kodjima33 kodjima33 commented Aug 14, 2026 •

Copy link
Copy Markdown
Collaborator

What

Movie-watching no longer counts as being away: the capture loop's idle gate skipped every tick after 60 s without HID input, so an hour of passive video (Nik's report: Maximum notifications, one nudge in a movie hour, 14 skipping capture ticks (idle) cycles logged) produced almost no screen analyses and therefore almost no proactive notifications.

  • MediaPlaybackIdlePolicy.effectiveIdleSeconds pins the gate's idle value to 0 while another process holds a display-sleep-prevention assertion (PreventUserIdleDisplaySleep / legacy NoDisplaySleepAssertion — what browsers and video players hold during playback). No media → HID idle passes through untouched.
  • MediaPlaybackDetector: 10 s-cached IOPMCopyAssertionsByProcess walk, ignoring our own pid (our capture stack's own assertions must not count as "the user is watching").
  • One log line per idle-but-watching episode (CaptureGate: HID-idle but media playback active — capture continues) for observability.

Product invariants

None matched (path-based check: none).

Failure-Class: none

Verification

  • MediaPlaybackIdleExemptionTests 3/3 (policy pass-through / pin-to-zero, detector TTL cache re-probe).
  • IOKit probe live-tested both ways on the target machine: standalone run of the identical assertion-walk code printed PREVENTED=false at rest and PREVENTED=true under a held PreventUserIdleDisplaySleep assertion.
  • End-to-end on the running app (omi-notch built from this branch, real user idleness, real assertion): 02:44:46 CaptureGate: HID-idle but media playback active — capture continues → 02:45:09 RewindIndexer: Last 60s — 4 frames…, zero idle skips during the hold.
  • Negative path live: with the assertion lapsed and the user idle, 02:27:09 CaptureGate: skipping capture ticks (idle) — no media keeps the old power-saving behavior.

Line-Count-Exception: desktop/macos/Desktop/Sources/ProactiveAssistants/ProactiveAssistantsPlugin.swift | 1761 -> 1779 | The idle-gate exemption must live at the gate check itself (effective-idle computation + once-per-episode log flag); the policy and detector logic already live in the new MediaPlaybackIdleExemption.swift, and splitting the plugin for 18 gate-site lines would fragment the capture-gate sequence.

…ght is not being away

Watching video produces no HID input, so after 60 quiet seconds the capture
loop skipped every tick and the proactive assistants went blind exactly when
Nik asked 'why no nudges while I watch a movie for an hour at Maximum?'
(14 idle-skip cycles logged in that hour).

MediaPlaybackIdlePolicy pins the gate's idle value to 0 while another process
holds a display-sleep-prevention assertion (PreventUserIdleDisplaySleep /
NoDisplaySleepAssertion — what browsers and players hold during playback),
via MediaPlaybackDetector (10s-cached IOPMCopyAssertionsByProcess walk that
ignores our own pid). No media -> HID idle passes through untouched.

Verified live on omi-notch (real user idleness + real assertion):
02:44:46 'CaptureGate: HID-idle but media playback active — capture continues'
then 02:45:09 'RewindIndexer: Last 60s — 4 frames', zero idle skips during the
hold; negative path at 02:27:09 (assertion lapsed) still logged the idle skip.
IOKit probe live-tested both ways; MediaPlaybackIdleExemptionTests 3/3.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 issues found across 4 files

Confidence score: 3/5

  • In desktop/macos/Desktop/Sources/ProactiveAssistants/ProactiveAssistantsPlugin.swift, cache misses currently run the IOKit assertion-table probe on captureFrame’s @MainActor, so slow powerd/Mach IPC can block the main run loop and cause visible UI/frame-capture stalls — move cache-miss probing off the main actor and only publish the result back on main.
  • In desktop/macos/Desktop/Sources/ProactiveAssistants/Core/MediaPlaybackIdleExemption.swift, using wall-clock time for the 10s TTL can leave a stale assertion decision in place after backward clock changes, causing idle capture to stay enabled after playback ends (or stay suppressed during playback) — switch TTL checks to a monotonic clock source.
  • In desktop/macos/Desktop/Sources/ProactiveAssistants/ProactiveAssistantsPlugin.swift, didLogMediaIdleExemption is only reset on HID activity, so if playback stops while HID idle is already high, the next playback session won’t log exemption state changes — reset the flag whenever effective playback state transitions.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="desktop/macos/Desktop/Sources/ProactiveAssistants/Core/MediaPlaybackIdleExemption.swift">

<violation number="1" location="desktop/macos/Desktop/Sources/ProactiveAssistants/Core/MediaPlaybackIdleExemption.swift:45">
P2: When macOS adjusts the wall clock backward, this branch can retain the assertion result far beyond the 10-second TTL. That can keep idle capture enabled after playback ends or suppress it during playback; use a monotonic clock, or reject negative elapsed values.</violation>
</file>

<file name="desktop/macos/Desktop/Sources/ProactiveAssistants/ProactiveAssistantsPlugin.swift">

<violation number="1" location="desktop/macos/Desktop/Sources/ProactiveAssistants/ProactiveAssistantsPlugin.swift:784">
P2: Every cache miss synchronously runs the IOKit assertion-table walk on `captureFrame`'s `@MainActor`, so a slow powerd/Mach-IPC response can stall the app's main run loop. Move cache-miss probing off the main actor and publish the result before applying the policy.</violation>

<violation number="2" location="desktop/macos/Desktop/Sources/ProactiveAssistants/ProactiveAssistantsPlugin.swift:790">
P3: When playback stops while HID idle remains above 60 seconds, `didLogMediaIdleExemption` never resets because the reset checks only HID activity. The next playback episode is then silent; reset the flag whenever the effective idle value is no longer exempted.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

}

func isDisplaySleepPrevented(now: Date = Date()) -> Bool {
if now.timeIntervalSince(cachedAt) < cacheTTL {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: When macOS adjusts the wall clock backward, this branch can retain the assertion result far beyond the 10-second TTL. That can keep idle capture enabled after playback ends or suppress it during playback; use a monotonic clock, or reject negative elapsed values.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At desktop/macos/Desktop/Sources/ProactiveAssistants/Core/MediaPlaybackIdleExemption.swift, line 45:

<comment>When macOS adjusts the wall clock backward, this branch can retain the assertion result far beyond the 10-second TTL. That can keep idle capture enabled after playback ends or suppress it during playback; use a monotonic clock, or reject negative elapsed values.</comment>

<file context>
@@ -0,0 +1,76 @@
+  }
+
+  func isDisplaySleepPrevented(now: Date = Date()) -> Bool {
+    if now.timeIntervalSince(cachedAt) < cacheTTL {
+      return cachedValue
+    }
</file context>

let hidIdleSeconds = systemIdleSeconds()
let idleSeconds = MediaPlaybackIdlePolicy.effectiveIdleSeconds(
hidIdleSeconds: hidIdleSeconds,
isDisplaySleepPrevented: mediaPlaybackDetector.isDisplaySleepPrevented())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Every cache miss synchronously runs the IOKit assertion-table walk on captureFrame's @MainActor, so a slow powerd/Mach-IPC response can stall the app's main run loop. Move cache-miss probing off the main actor and publish the result before applying the policy.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At desktop/macos/Desktop/Sources/ProactiveAssistants/ProactiveAssistantsPlugin.swift, line 784:

<comment>Every cache miss synchronously runs the IOKit assertion-table walk on `captureFrame`'s `@MainActor`, so a slow powerd/Mach-IPC response can stall the app's main run loop. Move cache-miss probing off the main actor and publish the result before applying the policy.</comment>

<file context>
@@ -770,8 +774,22 @@ public class ProactiveAssistantsPlugin: NSObject {
+    let hidIdleSeconds = systemIdleSeconds()
+    let idleSeconds = MediaPlaybackIdlePolicy.effectiveIdleSeconds(
+      hidIdleSeconds: hidIdleSeconds,
+      isDisplaySleepPrevented: mediaPlaybackDetector.isDisplaySleepPrevented())
+    if hidIdleSeconds >= captureTrigger.idleThreshold, idleSeconds < captureTrigger.idleThreshold,
+      !didLogMediaIdleExemption
</file context>

{
didLogMediaIdleExemption = true
log("CaptureGate: HID-idle but media playback active — capture continues")
} else if hidIdleSeconds < captureTrigger.idleThreshold {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: When playback stops while HID idle remains above 60 seconds, didLogMediaIdleExemption never resets because the reset checks only HID activity. The next playback episode is then silent; reset the flag whenever the effective idle value is no longer exempted.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At desktop/macos/Desktop/Sources/ProactiveAssistants/ProactiveAssistantsPlugin.swift, line 790:

<comment>When playback stops while HID idle remains above 60 seconds, `didLogMediaIdleExemption` never resets because the reset checks only HID activity. The next playback episode is then silent; reset the flag whenever the effective idle value is no longer exempted.</comment>

<file context>
@@ -770,8 +774,22 @@ public class ProactiveAssistantsPlugin: NSObject {
+    {
+      didLogMediaIdleExemption = true
+      log("CaptureGate: HID-idle but media playback active — capture continues")
+    } else if hidIdleSeconds < captureTrigger.idleThreshold {
+      didLogMediaIdleExemption = false
+    }
</file context>
Suggested change
} else if hidIdleSeconds < captureTrigger.idleThreshold {
} else if hidIdleSeconds < captureTrigger.idleThreshold || idleSeconds >= captureTrigger.idleThreshold {

@kodjima33

Copy link
Copy Markdown
Collaborator Author

Merging past two known-flaky checks, same pair and precedent as #11528 tonight: 'TERM case never launched detached app' and 'workflow test-install fixture' fail identically on unrelated diffs (previously on a 2-line color-token change). This PR touches only the capture idle gate (Swift policy + tests + e2e cover + changelog); Backend Hermetic Merge Gate and all other checks are green. Fix verified live 4× (transitions at 02:44/02:56/03:03/03:25, frames flowing, negative path preserved).

@kodjima33
kodjima33 merged commit dae686e into main Aug 14, 2026
30 of 34 checks passed
@kodjima33
kodjima33 deleted the kodjima33/idle-media-playback branch August 14, 2026 08:57
kodjima33 added a commit that referenced this pull request Aug 14, 2026
…ackDetector

Moves the effective-idle computation and once-per-episode log out of
ProactiveAssistantsPlugin (1779 -> 1767 lines) into the detector, where the
policy and cache already live. Restores the plugin below its pre-#11556
line count so the Release Eligibility lane — which re-runs the line-count
ratchet on main commits WITHOUT the merged PR's body exception (issue

Behavior identical, re-verified live on the rebuilt omi-notch bundle:
05:01:29 'CaptureGate: HID-idle but media playback active — capture
continues' with the user genuinely HID-idle 45 min and a real
PreventUserIdleDisplaySleep assertion held.
MediaPlaybackIdleExemptionTests 4/4 (adds the wrapper-matches-policy test).
kodjima33 added a commit that referenced this pull request Aug 14, 2026
…etector (unblocks beta train) (#11565)

## What

Unblocks the desktop beta train. Every `desktop_auto_release` run since
06:38Z fails because Release Eligibility re-runs the line-count ratchet
on main commits **without** the merged PR's body exception (filed as
#11564) — so #11553's and #11556's legitimate, PR-lane-approved
exceptions poison eligibility on their merge commits and no candidate
can cut.

This folds #11556's 18 gate-site lines out of
`ProactiveAssistantsPlugin.swift` (1779 → 1767) into
`MediaPlaybackDetector`, where the exemption policy and cache already
live. A shrinking commit passes the ratchet statelessly, so eligibility
on this commit (and descendants) goes green and the hourly train cuts
again.

Behavior is identical: `effectiveIdleSeconds(hidIdleSeconds:threshold:)`
on the detector wraps the same pure policy and keeps the
once-per-episode log.

## Product invariants

None matched (path-based check: none).

## Verification

- Re-verified live on the rebuilt omi-notch bundle from this branch:
`05:01:29 CaptureGate: HID-idle but media playback active — capture
continues` with the user genuinely HID-idle 45 min and a real
`PreventUserIdleDisplaySleep` assertion held (screenshot in worktree).
- `MediaPlaybackIdleExemptionTests` 4/4 — adds
`testDetectorEffectiveIdleMatchesPolicy` proving the gate-facing wrapper
mirrors the pure policy.
- Debug build clean; pinned swift-format applied.


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/BasedHardware/omi/pull/11565?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant