Skip to content

Run Music's AppleScript off the main thread - #307

Merged
mattt merged 5 commits into
mainfrom
mattt/music-applescript-queue
Nov 4, 2026
Merged

mattt merged 5 commits into
mainfrom
mattt/music-applescript-queue

Conversation

@mattt

@mattt mattt commented Oct 26, 2026 •

Copy link
Copy Markdown
Owner

This PR runs Music's AppleScript on a serial background queue instead of the main thread, so a slow or unresponsive Music no longer blocks iMCP's main actor. A new NSAppleScript.execute(_:) compiles and runs each script in one block on the queue. It skips a script whose task is cancelled before the script starts, and it keeps AppleScript's error number, so Music still reports a missing Automation permission clearly.

Seeking now reads the track's duration and sets the position in one script, so no other iMCP script can run between the two steps.

mattt added 3 commits November 4, 2026 03:29
NSAppleScript.execute(_:) compiles and runs each script
in one block on a serial queue,
so the main thread stays free while the target app answers.
A task that is cancelled before its script starts runs nothing;
the queue checks again after compiling, before any Apple event is sent.
Errors keep their AppleScript number and message.
Music's now-playing, control, and seek scripts
now run on the shared AppleScript queue
instead of blocking the main actor while Music answers.
A seek reads the current track's duration and sets the position
in one script, so no other iMCP script can run between the two.
They are still separate Apple events,
and Music can change tracks between them on its own.
The position is clamped to zero and the maximum in Swift,
and to the track's duration in the script when Music knows it.
@mattt
mattt force-pushed the mattt/music-applescript-queue branch from 5e01236 to 46e02b8 Compare November 4, 2026 11:37
@mattt
mattt changed the base branch from mattt/add-messages-send to main November 4, 2026 11:37
@mattt
mattt marked this pull request as ready for review November 4, 2026 11:39
@mattt
mattt requested a balanced review from Copilot November 4, 2026 11:39
The test relied on a half-second script
starting before the second script was queued.
On a slow runner the second script could run first.
Now the test holds the serial queue with a semaphore
until it has cancelled the queued script.

Copilot AI 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.

🟡 Changes recommended

The tests do not verify that script execution stays off the main actor, and sleep-based cancellation timing can cause failures under CI load.

2 open issues
What changed in this PR

Moves Music’s AppleScript execution to a serial background queue so slow scripts no longer block iMCP’s main actor.

Changes:

  • Adds an asynchronous script runner with cancellation checks and preserved AppleScript error details.
  • Combines seek duration lookup and position updates into one script.
  • Adds runner and seek-script tests and registers the new test file.
File Description
Tests/​imcp-serverTests/​MusicTests.swift Tests combined seek-script generation and compilation.
Tests/​imcp-serverTests/​AppleScriptTests.swift Adds execution, cancellation, and error-preservation tests.
iMCP.xcodeproj/​project.pbxproj Registers AppleScript tests in the test target.
App/​Services/​Music.swift Uses asynchronous script execution and a combined seek script.
App/​Extensions/​Foundation+Extensions.swift Adds the serial background AppleScript runner and structured errors.

Review effort: Balanced

The test only checked the script's result.
Now it holds the serial queue,
starts a script from the main actor,
and checks that the script has not finished
until the queue is released.

Copilot AI 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.

🔵 Needs a closer look

The execution-context change still needs macOS validation with real Music Apple events, Automation prompts and denials, and an unresponsive Music process, since the new tests cover only local scripts.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@mattt
mattt merged commit e50c981 into main Nov 4, 2026
3 checks passed
@mattt
mattt deleted the mattt/music-applescript-queue branch November 4, 2026 12:07
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.

2 participants