Skip to content

fix(desktop): read Automation status when System Events is stopped - #12625

Merged
Git-on-my-level merged 5 commits into
mainfrom
fix/automation-permission-system-events-stopped
Sep 3, 2026
Merged

Git-on-my-level merged 5 commits into
mainfrom
fix/automation-permission-system-events-stopped

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Sep 2, 2026 •

Copy link
Copy Markdown
Member

The bug

Automation is granted to Omi Beta in System Settings — Omi Beta ▸ System Events, toggle on — and the in-app Permissions page reports Automation · Not Granted, with the three-step "go turn it on" instructions under it. Reopening the page and reactivating the app both refresh, and both re-confirm Not Granted.

Root cause

AEDeterminePermissionToAutomateTarget answers -600 (procNotFound) whenever its target is not running, and System Events is a background-only app that exits as soon as it goes idle. On any Mac that is not currently being scripted — the normal state — the passive probe never gets to see the grant.

applyAutomationPermissionStatus deliberately treats -600 as "no new information" and preserves the previous value. That is right on its own terms: a stopped target is not evidence of a revocation. But it also cannot upgrade from a value that was never determined, and hasAutomationPermission starts every launch at its false default. So the row settles on Not Granted for the whole session, and each refresh re-confirms it.

Measured on the reporting machine, same TCC grant, one variable:

System Events running      → AEDeterminePermissionToAutomateTarget = 0     (granted)
System Events stopped      → AEDeterminePermissionToAutomateTarget = -600  (unknown)

The fix

The request path already knew this: SBAutomationConsent.requestSystemEventsConsent starts the target with LaunchServices before it asks. Only the passive status read was missing that step.

SBAutomationConsent — which the file already names as the single owner of this mechanism — gains currentSystemEventsPermission(): probe, and if the answer is -600, start System Events and probe once more. AppState.queryAutomationPermissionStatus() now delegates to it, so the read and the request cannot disagree. It stays silent: LaunchServices sends no Apple Event, and askUserIfNeeded: false cannot raise a dialog. It is still a status read, not a request.

-600 preservation stays, as the fallback for a target that genuinely will not start.

Same-machine verification of the fixed logic, System Events stopped:

old probe   = -600
fixed probe = 0

Also in this PR: the chat permission tool re-derived status == noErr itself, so on -600 it wrote a permission the user holds away as missing and clobbered the flag the Permissions page was showing. It now takes the shared projection, for the reason the accessibility branch immediately above it already documents.

Tests

SBOnboardingPermissionFlowTests, four new behavioral cases against the production API:

  • a stopped target is started and the resolved status returned, with both probes asserted non-prompting
  • a determinate answer (0 / -1743 / -1744) starts nothing
  • a target that will not start still reports -600, so the caller can preserve what it knows
  • the reported bug end to end: hasAutomationPermission false, first probe -600, second 0 → the row resolves to granted

Local: swift build -c debug clean; the permission suites pass (75 tests, 0 failures); pinned swift-format and SwiftLint clean; check_desktop_test_quality.py at baseline; line-count ratchet clean at the original base.

Follow-up (review): the launch-capable probe was still called synchronously from two @MainActor sites in the chat permission tool, freezing the chat window for the LaunchServices wait (up to ~5s on a cold target). Both now route through Task.detached(priority: .userInitiated) — the rule AppState.refreshAutomationPermission already follows — with a source-contract regression test (AppStatePermissionProbeTests.testChatPermissionStatusProbeNeverRunsOnTheMainActor, suite 14/14 green; SBOnboardingPermissionFlowTests 17/17 green).

Line-Count-Exception: desktop/macos/Desktop/Sources/Providers/ChatToolExecutor.swift | 3750 -> 3772 | Two @mainactor call sites each need a small Task.detached hop so the launch-capable probe cannot freeze the chat window; upstream main grew the file under this PR, so the pre-existing +6 plus the +16 isolation fix are declared together.

Product invariants affected

  • INV-CHAT-1 — ChatToolExecutor.swift is touched only to route one already-existing permission-flag write through the shared projection. No transcript, session, or continuity state is read, written, or stored.

Failure-Class: none

🤖 Generated with Claude Code

Review in cubic

@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.

1 issue found across 5 files

Confidence score: 3/5

  • SBOnboardingPermissionRuntime.swift synchronously waits on LaunchServices when System Events is stopped, which can freeze the main actor for up to five seconds and make the onboarding/chat permission check visibly unresponsive — run the probe, including the launch wait, off the main actor.
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/Onboarding/SecondBrain/SBOnboardingPermissionRuntime.swift">

<violation number="1" location="desktop/macos/Desktop/Sources/Onboarding/SecondBrain/SBOnboardingPermissionRuntime.swift:133">
P2: When System Events is stopped, the chat permission status check can freeze the main actor for up to five seconds because this synchronous probe now waits for LaunchServices. Run the probe, including the launch wait, off the main actor before updating the shared projection.</violation>
</file>

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

Re-trigger cubic


let status = read(false)
guard status == -600 else { return status }
start()

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 System Events is stopped, the chat permission status check can freeze the main actor for up to five seconds because this synchronous probe now waits for LaunchServices. Run the probe, including the launch wait, off the main actor before updating the shared projection.

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

<comment>When System Events is stopped, the chat permission status check can freeze the main actor for up to five seconds because this synchronous probe now waits for LaunchServices. Run the probe, including the launch wait, off the main actor before updating the shared projection.</comment>

<file context>
@@ -101,6 +101,39 @@ enum SBAutomationConsent {
+
+    let status = read(false)
+    guard status == -600 else { return status }
+    start()
+    return read(false)
+  }
</file context>

Git-on-my-level and others added 2 commits September 3, 2026 07:21
The Permissions page reported Automation as "Not Granted" on a Mac where
System Settings showed the System Events toggle on, and no amount of
reopening the page or reactivating the app moved it.

`AEDeterminePermissionToAutomateTarget` answers -600 (procNotFound)
whenever its target is not running, and System Events is a background-only
app that exits as soon as it goes idle — which is its normal state. The
passive probe treated that as "no new information" and preserved whatever
the app already believed, and from a cold launch that belief is the `false`
default of `hasAutomationPermission`. So the row settled on Not Granted for
the whole session and every refresh re-confirmed it.

Verified on the reporting machine: with System Events stopped, the shipped
probe returns -600 and the same probe with the launch step returns 0.

The request path already knew this — `SBAutomationConsent` starts the target
before asking. Give it a non-prompting status read that does the same and
point the passive probe at it, so both paths share one mechanism.
LaunchServices sends no Apple Event and `askUserIfNeeded: false` cannot
raise a dialog, so the read stays silent.

-600 preservation stays as the fallback for a target that will not start.

Also stop the chat permission tool from re-deriving `status == noErr`
itself: on -600 it wrote a held permission away as missing, and clobbered
the flag the Permissions page was showing. It now takes the same projection,
for the reason the accessibility branch beside it already documents.

Co-Authored-By: Claude Opus 5 <[email protected]>
The passive automation probe learned to start System Events, which makes
it blocking: LaunchServices can take up to ~5s on a cold target. Both
remaining call sites in ChatToolExecutor (@mainactor) still invoked it
synchronously — the check-status tool froze the chat window for the
duration of that wait, and the request tool's completion handler ran it
inside Task { @mainactor … }.

Route the probe through Task.detached(priority: .userInitiated) at both
sites, the same rule AppState.refreshAutomationPermission already
follows, and add a source-contract regression test so the probe cannot
silently move back onto the main actor.

Verification: xcrun swift test --package-path desktop/macos/Desktop
--filter AppStatePermissionProbeTests (14 passed, incl. new
testChatPermissionStatusProbeNeverRunsOnTheMainActor) and
--filter SBOnboardingPermissionFlowTests (17 passed).

Failure-Class: none

Co-Authored-By: Claude Opus 5 <[email protected]>
@Git-on-my-level
Git-on-my-level force-pushed the fix/automation-permission-system-events-stopped branch from 18499f8 to 378a052 Compare September 3, 2026 00:59
Git-on-my-level and others added 3 commits September 3, 2026 00:04
The new main-actor regression test's escape comment sat three lines
above the `String(contentsOf:)` call it was meant to cover.
check_desktop_test_quality.py only looks at the offending line and the
one immediately before it, so the escape never registered and the
ratchet counted the site as new debt (55/148 against a 54/147
baseline), failing CI on Hygiene and both Desktop Swift jobs.

Failure-Class: none

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…wording

HubEscalationTests.testPublicWebPromptIsSpeakableAndExcludesPrivateToolContext
still asserted the pre-bce354d767 prefix. That commit (2026-09-02, "preserve
realtime voice context boundaries") added "thoroughly" to
RealtimeHubTools.publicWebSearchPrompt's opening sentence without updating
this assertion. It went unnoticed because CI path-filters skip the Swift jobs
unless desktop/macos changes, and this is the first PR to touch that path
since the commit landed — it fails only on the PR's merge-ref combination
(this branch's test + main's current prompt text), not on either tip alone.

Verified locally against the actual refs/pull/12625/merge combination
(6/6 HubEscalationTests pass) and via full pr-preflight (22/22).

Failure-Class: none

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@Git-on-my-level
Git-on-my-level merged commit dbfb309 into main Sep 3, 2026
25 checks passed
@Git-on-my-level
Git-on-my-level deleted the fix/automation-permission-system-events-stopped branch September 3, 2026 10:03
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