Skip to content

Run an AppleScript handler with typed arguments and a pre-send check - #390

Merged
mattt merged 2 commits into
mattt:mainfrom
oliverames:pr/apple-events-helper
Nov 22, 2026
Merged

mattt merged 2 commits into
mattt:mainfrom
oliverames:pr/apple-events-helper

Conversation

@oliverames

@oliverames oliverames commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Split out from #383 per #364.

This adds NSAppleScript.Subroutine and optional calling: and beforeSending: parameters to NSAppleScript.execute, so a fixed script can receive typed Apple event descriptors instead of interpolated source text, and run a last-second check on the serial queue right before executing.

Review fixes

  • beforeSending is @Sendable, so the nonisolated(unsafe) binding is gone.
  • The task's cancellation is checked again after the hook returns, so a task cancelled while the hook runs does not start the script. A test cancels the task while the hook is blocked and checks that the script never runs.

Testing

  • swift format lint --strict is clean on both files.
  • imcp-serverTests/AppleScriptTests: 9 tests, 0 failures. They cover hostile string values, typed parameter descriptors (string, boolean, strings, enumCode), beforeSending ordering and short-circuiting, cancellation before the script, cancellation while queued, and cancellation during the hook.
  • CI: the macOS 27 job passes. The macOS 26 job is red for an unrelated reason, explained below.

The macOS 26 failure is not a test

The macOS 26 job fails in different steps on different runs (Test once, Build on the latest run), and the log names no test because the build fails first. The error is a stale module cache:

error: Clang dependency scanner failure: fatal error: file '/Applications/Xcode_26.0.1.app/.../module.modulemap' has been modified since the module file '.../HelperDerivedData/ModuleCache.noindex/.../_Builtin_float-...pcm' was built
fatal error: could not build module 'CoreFoundation'

The runner image now has Xcode 26.0.1, while the cached .pcm was built against 26.0. The cache key already includes the Xcode build number, but the cached module files live under DerivedData/.../HelperDerivedData/ModuleCache.noindex, and the workflow's cleanup only removes DerivedData/ModuleCache.noindex. I have a separate small PR that removes every ModuleCache.noindex under DerivedData before saving.

@mattt

mattt commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Hi @oliverames. Thanks for splitting this out. Two things before I merge: could you make beforeSending @Sendable and drop the nonisolated(unsafe) binding, and check isCancelled once more after the hook returns, so a task that's cancelled while the hook runs doesn't start the script? CI's test step is also failing on macOS 26 while it passes on 27, and the quiet log doesn't name the test. Could you take a look at which one it is?

oliverames pushed a commit to oliverames/iMCP that referenced this pull request Nov 20, 2026
Mattt's mattt#390 review:

- beforeSending is @sendable, so the nonisolated(unsafe) binding is gone.
- The task's cancellation is checked again after the hook returns, so a
  task cancelled while the hook runs does not start the script.
- A test cancels the task while the hook is blocked and checks that the
  script never runs.
@oliverames
oliverames force-pushed the pr/apple-events-helper branch from c317c8f to ce80938 Compare November 20, 2026 22:53
oliverames pushed a commit to oliverames/iMCP that referenced this pull request Nov 20, 2026
@oliverames

oliverames commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

@mattt On the macOS 26 test step: it isn't a test. The job fails in different steps on different runs (Test on the first run, Build on the latest), and the log says the build failed before any test ran:

error: Clang dependency scanner failure: fatal error: file '/Applications/Xcode_26.0.1.app/.../module.modulemap' has been modified since the module file '.../HelperDerivedData/ModuleCache.noindex/.../_Builtin_float-...pcm' was built
fatal error: could not build module 'CoreFoundation'

The runner image has Xcode 26.0.1 now while the cached .pcm was built by 26.0. The cache key already includes the Xcode build number, so a new image gets a fresh key, but the cleanup step only removed DerivedData/ModuleCache.noindex. The iMCP Helper target keeps a second module cache at DerivedData/.../HelperDerivedData/ModuleCache.noindex, and that's what stayed in the saved archive.

I opened #393 to remove every ModuleCache.noindex under DerivedData before the cache is saved. So there's no failing test to name; the job is red for an infrastructure reason. The macOS 27 job passes, and a run with the test step un-quieted passed both jobs while the image was still consistent.

oliverames pushed a commit to oliverames/iMCP that referenced this pull request Nov 20, 2026
The fork's integration build still had the pre-review code for the
Reminders, Notes and AppleScript helper work. Bring in the same fixes
Mattt is reviewing upstream:

- Reminders: clamp stored relative alarms instead of trapping; carry the
  reminder's calendar and time zone on all-day and timed dues and keep
  seconds; read views in the stored time zone; include the house number
  in the street match and reject a bare locality; gate complete by
  reminders_update and rename by reminders_lists_rename; report an
  uncertain result when a complete's readback finds no reminder; accept
  completion_date on update; use the database tag records for notes_tags.
- Notes: count scannedNoteCount against the scan budget and cap the page
  request by what is left; keep unreadableNoteIDs whether or not they
  matched; read tag filters from the database tag records; stop
  overwriting keywords from note text; don't pad short table rows; drop
  TagTally.
- AppleScript: make beforeSending @sendable, drop nonisolated(unsafe),
  and check cancellation after the hook returns.

781 tests, 0 failures; format clean; the app builds.
oliverames pushed a commit to oliverames/iMCP that referenced this pull request Nov 20, 2026
@oliverames

Copy link
Copy Markdown
Contributor Author

@mattt Both are in ce80938. beforeSending is @Sendable now and the nonisolated(unsafe) binding is gone. After the hook returns, the queue checks isCancelled again and throws CancellationError without starting the script. testCancellingDuringTheHookStopsTheScript holds the hook open, cancels the task, and checks that the script never runs.

The macOS 26 job is still red for the module-cache reason above, and #393 fixes that.

Oliver Ames added 2 commits November 21, 2026 09:05
NSAppleScript.execute can now call a handler of a fixed script with arguments that reach it as Apple event descriptors, never as source text. A caller's beforeSending check runs on the script queue right before the script.
Mattt's mattt#390 review:

- beforeSending is @sendable, so the nonisolated(unsafe) binding is gone.
- The task's cancellation is checked again after the hook returns, so a
  task cancelled while the hook runs does not start the script.
- A test cancels the task while the hook is blocked and checks that the
  script never runs.
@oliverames
oliverames force-pushed the pr/apple-events-helper branch from ce80938 to 14070a2 Compare November 21, 2026 14:06
@oliverames

Copy link
Copy Markdown
Contributor Author

@mattt I rebased this on main now that #393 is in, and the macOS 26 job passes. I also dropped the empty commit I'd pushed to re-run CI, so it's down to the helper and the fix for your two points, which is now 14070a2.

oliverames pushed a commit to oliverames/iMCP that referenced this pull request Nov 21, 2026
@mattt
mattt merged commit 4a125d6 into mattt:main Nov 22, 2026
2 checks passed
@oliverames
oliverames deleted the pr/apple-events-helper branch November 25, 2026 15:12
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