Skip to content

Handle cancellation during frontmost wait without clipboard fallback - #365

Merged
alexkroman merged 1 commit into
mainfrom
claude/upbeat-knuth-xixoej
Nov 25, 2026
Merged

alexkroman merged 1 commit into
mainfrom
claude/upbeat-knuth-xixoej

Conversation

@alexkroman

Copy link
Copy Markdown
Collaborator

What & why

Fixes #334: when a cancel lands during the frontmost wait (the waitForTargetActivation step), poll returns false on the cancelled sleep, which reads as "target app lost". The code was treating this as a genuine app loss and taking the copy fallback, but it's actually a cancellation and should abort without touching the clipboard.

The fix adds a Task.checkCancellation() call in the error path after activateTargetApp fails, so a cancel that arrived during the frontmost wait is properly recognized and re-thrown before the clipboard fallback logic runs.

Changes:

  • Refactored cancelDuringActivationSkipsPaste test into a parameterized helper expectCancelMidActivationLeavesClipboard that accepts closures for the activation and frontmost-wait steps
  • Added new test cancelDuringFrontmostWaitLeavesClipboard that verifies cancellation during the poll loop aborts without the copy fallback
  • Added waitForTargetActivation parameter to KeyInjector initialization in tests to control the frontmost-wait behavior
  • Added Task.checkCancellation() in KeyInjector.swift after the activateTargetApp error to distinguish cancellation from genuine app loss
  • Added time limits to both tests to prevent hangs

How it was tested

  • scripts/check.sh passes (or CI will, if I'm not on a Mac)
  • I read AGENTS.md and this doesn't reintroduce anything deliberately removed
  • Docs updated if behavior changed

The new test cancelDuringFrontmostWaitLeavesClipboard directly exercises the fixed code path by simulating a cancel landing mid-poll. Both tests verify that cancellation aborts before the paste and before any clipboard modification.

A cancel landing during the frontmost wait makes poll's sleep throw, so it
returns false and activateTargetApp throws .targetAppLost. performInsert
then wrote the transcript to the user's clipboard before rethrowing —
contradicting the cancel contract (no paste, clipboard untouched).

Check for cancellation in the activation catch before the copy fallback,
and cover the production poll path: the existing cancel test cancelled in
activateTarget and returned true, so it never hit poll's cancel -> false.

Fixes #334

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1UaqgyU8LGTsN7693y

Copy link
Copy Markdown
Collaborator Author

CI status: gate is red only because ios-build was cancelled by its 30-minute timeout-minutes. It was not a test or lint failure.

  • macOS check passed, including the new KeyInjectorFallbackTests case.
  • In ios-build, all 365 iOS tests passed and BlurtiOSCore coverage was 96.1%. swiftlint analyze then took about 10.5 minutes on that runner, and the job was cancelled during periphery.
  • This PR changes only #if os(macOS) code in KeyInjector and its macOS test, so the iOS targets don't compile any of it.

I've re-run the failed jobs once. If ios-build times out again, the cause is the job's time budget rather than this PR's code.


Generated by Claude Code

@alexkroman
alexkroman added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit 9ab98e8 Nov 25, 2026
19 of 21 checks passed
@alexkroman
alexkroman deleted the claude/upbeat-knuth-xixoej branch November 25, 2026 01:33
alexkroman pushed a commit that referenced this pull request Nov 25, 2026
#365 and this PR each grow the file; together they reached 401. One
comment reflowed (within the 120-column limit), no code change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Cancel during frontmost wait is treated as targetAppLost and overwrites the clipboard

2 participants