Skip to content

Report a cancelled app-activation wait as a cancel, not a failure - #423

Merged
alexkroman merged 3 commits into
mainfrom
claude/poll-cancellation
Nov 25, 2026
Merged

alexkroman merged 3 commits into
mainfrom
claude/poll-cancellation

Conversation

@alexkroman

Copy link
Copy Markdown
Collaborator

What & why

KeyInjector.poll returned false both when its time budget ran out and when it was cancelled, so each caller had to work out which one happened. performInsert handled it with a Task.checkCancellation() in its activation catch (9ab98e8). SystemVoiceCommandPerformer.open did not, so a command cancelled while its app was coming forward was shown and logged as couldNotPerform("bring <app> to the front").

  • poll now throws CancellationError when it is cancelled before its check succeeds, including a cancel during the last check after the deadline. So false now only means the budget ran out.
  • KeyInjector: waitUntilFrontmost and the waitForTargetActivation seam are now async throws, and activateTargetApp drops its typed throws(BlurtError). performInsert rethrows a CancellationError ahead of the copy-fallback catch, replacing the Task.checkCancellation() workaround.
  • SystemVoiceCommandPerformer: the end of open (the poll, then the "frontmost is enough" check) moves into a testable @MainActor static func waitForFront(_:clock:look:frontmost:). It lets the cancel through, and the router already handles that: SelectionSpeechRouter.execute treats a cancellation error on a cancelled task as an abandoned command.
  • The only poll callers are KeyInjector.waitUntilFrontmost and SystemVoiceCommandPerformer.open, plus tests.

Behaviour change: a voice command cancelled while waiting for an app it opened to come forward now ends as abandoned instead of failing with "Couldn't bring to the front". There is no failure state on the pill and no errors.jsonl entry; the commands.jsonl outcome is abandoned instead of failed, and the step carries no error.

The paste path behaves the same for every cancel that lands during the frontmost wait. One edge differs: a cancel landing during the synchronous isTerminated / activate() check, if activation then fails, now takes the .targetAppLost copy fallback, where before the catch's checkCancellation() aborted it. That window has no suspension point, so the outcome there already depended on timing.

How it was tested

  • Written in a Linux sandbox with no Swift toolchain: scripts/check.sh --portable passes, as do check-swift-size.sh and check-test-clock.sh. CI on macOS verifies the build, every test, swiftlint, swift-format and periphery.

  • New tests, all on TestClock with no wall-clock waits:

    • KeyInjectorSystemActionsTests: poll throws CancellationError when cancelled mid-wait; with a zero budget and an already-cancelled task it still throws at the last check.
    • New SystemVoiceCommandPerformerFrontTests, through SelectionSpeechRouter.execute:
      • cancelled during the wait: no outcome, no failure, no step records;
      • never comes forward: VoiceCommandError.couldNotPerform;
      • frontmost when the wait runs out: returns false without throwing.
  • Adjusted: the existing poll and waitUntilFrontmost tests add try. KeyInjectorFallbackTests' "cancel during frontmost wait" now goes through the throwing seam and still asserts no ⌘V and an untouched clipboard.

  • Size margins are thin: KeyInjector.swift is at 390/400 lines and SystemVoiceCommandPerformer's type body at about 235/250.

  • 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


Generated by Claude Code

`KeyInjector.poll` answered `false` both when its budget ran out and when
its sleep was cancelled, so every caller had to tell the two apart itself.
`performInsert` did, with a `Task.checkCancellation()` in its activation
catch; `SystemVoiceCommandPerformer.open` did not, so a command abandoned
while the app it opened came forward was reported and logged as failing to
"bring <app> to the front".

`poll` now throws `CancellationError` when cancelled before its check
held, including during the last read past the deadline, so `false` only
ever means the budget ran out. `waitUntilFrontmost` and the injector's
`waitForTargetActivation` seam throw with it; `performInsert` lets a
`CancellationError` through without the copy fallback and drops the
re-check. The open-app wait moves into `waitForFront`, which lets the
cancel propagate, and the router's existing handling ends the command as
abandoned rather than failed.

Tests: `poll` throws when cancelled mid-wait and at the last read; a
command cancelled while its app comes forward is abandoned with no failure
and no step error, while one whose app never comes forward still fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC
The test compares against localizedDescription, which needs Foundation
imported in the file itself under MemberImportVisibility.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC
@alexkroman
alexkroman enabled auto-merge November 25, 2026 21:11
@alexkroman
alexkroman added this pull request to the merge queue Nov 25, 2026
alexkroman pushed a commit that referenced this pull request Nov 25, 2026
… CI run

Recent multi-commit PRs (#410, #413, #414, #416, #420, #422, #423, #425, #426)
mostly spent their extra ~37-minute runs on things nothing caught before CI:

- scripts/check-swift-lint-lite.sh: a toolchain-free text match for the opt-in
  SwiftLint/swift-format rules it can see (modifier_order, no_empty_block,
  NoBlockComments, swift-format's `catch  where`). Run by check.sh (so
  --portable and --lint-only) and the swift-budgets edit hook; would have
  flagged the pre-fix files of #414, #422 and #425.
- .claude/hooks/push-gate.sh: a PreToolUse hook that runs
  `check.sh --portable` (~20 s) before any `git push` and blocks a red one —
  jscpd clones (#420, #422) and post-merge size overruns (#363, #378) were
  pushes that skipped the documented portable run.
- AGENTS.md / CLAUDE.md: a "Rules only CI catches" list for the rest
  (variable_shadowing, one_declaration_per_file, type_contents_order,
  multiline_function_chains, MemberImportVisibility, one argument per line),
  and the new hook and script in the hooks table and permissions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1UDUZXyuF6g7GUmmNu
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 25, 2026
@alexkroman
alexkroman added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit fe83789 Nov 25, 2026
22 checks passed
@alexkroman
alexkroman deleted the claude/poll-cancellation branch November 25, 2026 22:27
alexkroman pushed a commit that referenced this pull request Nov 25, 2026
One conflict, in .claude/hooks/swift-budgets.sh: #428 added the
lint-lite rules inside the per-script scope cases this PR removed. The
size and clock checks still narrow their own arguments; check-swift-lint-
lite.sh scans what it's given, so the hook keeps one scope case for it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC
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