Skip to content

fix(desktop): sync login state with macOS BTM, offload I/O, and add retry recovery - #3061

Open
knqiufan wants to merge 1 commit into
masterfrom
fix/desktop-issue-3019
Open

knqiufan wants to merge 1 commit into
masterfrom
fix/desktop-issue-3019

Conversation

@knqiufan

@knqiufan knqiufan commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator

Which issue or RFC does this PR close?

Closes #3019.

Rationale for this change

Follow-up improvements to Open Desktop at login (#2978):

  1. On macOS 13+, disabling a LaunchAgent in System Settings → General → Login Items & Extensions updates Background Task Management (BTM) without deleting ~/Library/LaunchAgents/PowerContext Desktop.plist, causing Desktop Settings to show the switch as enabled when macOS will not launch it.
  2. desktop_login ran synchronous filesystem and Windows Registry calls on the Tauri UI event-loop thread.
  3. If the initial desktop_login read failed when SettingsPage mounted, the switch stayed disabled with no inline retry action.

What changes are included in this PR?

  • macOS BTM status synchronization (desktop/src-tauri/src/desktop_preferences/launch_agent.rs, desktop/src-tauri/Cargo.toml):
    • Kept the LaunchAgent plist mechanism rather than migrating to SMAppService.mainApp because CI/preview macOS bundles are unsigned (and SMAppService.mainApp fails without proper code signing on unsigned Intel bundles and unbundled dev runs) and LaunchAgent preserves compatibility with existing registrations.
    • Added SMAppService::statusForLegacyURL: queries via ServiceManagement.framework (objc2 + objc2-foundation NSURL, already in Cargo.lock) to read the effective macOS BTM status (NotRegistered, Enabled, RequiresApproval, NotFound).
    • Polls BTM briefly (up to 40 × 50 ms) only after writing a new/updated plist so initial registration reflects BTM indexing (Enabled).
    • Avoids rewriting a byte-identical plist when already present on disk, and surfaces RequiresApproval on enable (Some(true)) as SafeError::Forbidden so Desktop does not misreport a user-revoked System Settings approval as a generic verification failure.
  • Offload blocking OS calls (desktop/src-tauri/src/desktop_preferences.rs):
    • Converted desktop_login to async fn and moved macOS filesystem/BTM operations and Windows autostart Registry calls onto tauri::async_runtime::spawn_blocking.
  • Inline retry & approval guidance (desktop/ui/src/app/SettingsPage.tsx, desktop/ui/tests/settings.test.tsx):
    • Added a Retry read (重试读取) action inside PageFeedback when the initial desktopLogin() check fails (nativeLogin === null).
    • Extracted a shared runLoginRequest helper to guard against stale/unmounted async responses across initial load, retry, and toggle updates.
    • Added specific System Settings approval guidance when desktop_login returns "forbidden".

Are there any user-facing changes?

  • On macOS, Open Desktop at login now reflects when the login item has been disabled in System Settings → General → Login Items & Extensions, and guides the user back to System Settings if re-enabling is blocked by BTM approval.
  • When reading the OS login setting fails on opening Settings, users can click Retry read (重试读取) inline instead of navigating away.

How was this change tested?

  • rustup run 1.95.0 cargo fmt --manifest-path desktop/src-tauri/Cargo.toml --check
  • rustup run 1.95.0 cargo clippy --locked --manifest-path desktop/src-tauri/Cargo.toml --all-targets -- -D warnings
  • rustup run 1.95.0 cargo test --locked --manifest-path desktop/src-tauri/Cargo.toml (49 unit tests including launch_agent BTM state/polling/FFI tests, plus integration test suites)
  • cd desktop && pnpm lint && pnpm test && pnpm ipc:check && pnpm build (16 Vitest files, 102 tests passed including retry and approval guidance tests)
  • Verified SMAppService.statusForLegacyPlist(at:) behavior empirically on macOS 26.6 against ~/Library/LaunchAgents/ items (enabled, disabled/RequiresApproval, and newly written plist indexing latency); Windows spawn_blocking branch was checked for syntax/types locally and awaits Windows CI verification.

AI usage statement

Antigravity (Gemini 4 Hydrogen) assisted with analysis, implementation, and Open Code Review verification.

@knqiufan
knqiufan requested a review from PsiACE as a code owner December 6, 2026 08:22
@knqiufan knqiufan added the module:desktop Primary module: desktop app and platform integration label Dec 6, 2026

@hidb4ai hidb4ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The legacy LaunchAgent approach is reasonable: querying Apple's statusForLegacyURL API distinguishes a present plist from effective OS authorization, and the async command moves blocking OS work off the UI thread. The retry/error UI and request-generation guard are coherent.

7 Settings tests, TypeScript, 48 Linux Rust unit tests (2 ignored), rustfmt and the generated IPC contract check passed. The filesystem/state-machine tests use controlled BTM status inputs. I did not independently run macOS ServiceManagement/System Settings/login acceptance or Windows Registry acceptance; the author's macOS evidence is separately identified, and the platform CI matrix remains incomplete. No new table or public interface. Readiness: 74/100; no actionable code defect found in this review.

Current head: df91a7c. Clean merge with master efba45e. CI: {'IN_PROGRESS': 1, 'SUCCESS': 1}. Skipped jobs are not successful validation.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:desktop Primary module: desktop app and platform integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(desktop): refine Open Desktop at login state sync, responsiveness, and error recovery

2 participants