Skip to content

Fail dictations whose request the system dropped, not wedge them - #425

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

alexkroman merged 3 commits into
mainfrom
claude/shared-cancellation-check

Conversation

@alexkroman

@alexkroman alexkroman commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

What & why

Four places asked "is this error a cancellation?" four different ways. The engine now answers it once, as two internal properties on Error (Sources/BlurtEngine/Error+Cancellation.swift):

They're internal to BlurtEngine: since #398 the engine imports no provider SDK, and the AssemblyAI SDK states the one rule it needs where it needs it.

What the history showed for the dictation pipeline: the shape-only rule came from #326 (fixing #324). But cancel() is the only thing that tears down the in-flight request, and it calls pipelineTask?.cancel() before setPhase(.cancelled) in the same actor turn. So the only case #326 changed is a system drop. There the pipeline returned nil and left the phase on .transcribing; performPress only starts from a terminal phase, so the dictation key stayed dead until relaunch. The existing test only checked phase != .failed(...), so it passed while the key was dead.

Where Rule Why
SelectionSpeechRouter (execute, perform, paste) isAbandonment Unchanged from #342; the private isCancellation helper is removed.
DictationSession.awaitUpload Task.isCancelled only A cancellation-shaped error on an uncancelled task can only be a system drop (see above). Task.isCancelled alone, rather than isAbandonment, so any error arriving after a real cancel still leaves .cancelled in place.
SpeechClient (SDK, TTS) URLError.cancelled and Task.isCancelled, inline A stop cancels the reading task (speech(for:)'s onTermination), so stops still read as stops.
AsyncTranscriptionClient.isTransient (SDK) unchanged Only decides whether to retry; it already left cancellations out.

Behaviour change:

  • Dictation: when the system drops the request mid-transcription (URLError.cancelled, no cancel), the dictation now ends .failed(.sttFailed(URLError.cancelled)), with a red pill and an errors.jsonl entry. Before, the pill stayed on "Transcribing…" and every later press was refused until relaunch. A user's cancel still ends .cancelled, silently.
  • Read aloud / ask: a read the system cut short now fails (the router logs it and plays the failure cue) instead of ending silently as if stopped. A user's stop is still silent.
  • Command developer-mode log: a step stopped by a dropped request now records its error. The command itself was already logged as failed since Electron selection read permanently breaks after one voice command, and SelectionSpeechRouter.paste() wedges .working on un-cancelled URLError.cancelled #342.
  • None for the router's rewrite/compose/command outcomes, and none for what AsyncTranscriptionClient retries.

How it was tested

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

  • CancelRaceTests.systemDroppedRequestFails replaces the old "ignored" test. A system-dropped request ends .failed(.sttFailed(URLError(.cancelled))), nothing is pasted, and the next press reaches .recording.

  • SpeechTransportTests.systemDroppedIsFailure: URLError.cancelled with no stop comes out of the synthesizer as URLError, not CancellationError, both before and during the body.

  • CancellationTests: unit tests for both properties, inside and outside a cancelled task.

  • SelectionSpeechRouter.swift is at 396/400 lines.

  • 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 with Claude Code

https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC

Four catch sites asked "is this error a cancellation?" four ways. The
AssemblyAI module now answers it once, on Error:

- isCancellationShaped: CancellationError or URLError.cancelled, whoever
  tore the request down.
- isAbandonment: cancellation-shaped and the current task was cancelled.
  The same error on a task nobody cancelled is a request the system
  dropped (network switch, sleep), which is a failure.

Each site now states which rule it means:

- SelectionSpeechRouter's command and rewrite/compose paths use
  isAbandonment, as dcc9d48 (#342) made them; the private isCancellation
  helper is gone. A command step stopped by a dropped request now records
  its error in the step log too, matching the command's own failure record.
- DictationSession.awaitUpload treats only Task.isCancelled as a cancel.
  Its only mid-flight teardown, cancel(), cancels pipelineTask before
  setPhase(.cancelled) abandons the upload, in the same actor turn, so a
  cancellation-shaped error with the task not cancelled can only be a
  system drop. #326 had swallowed that, leaving the phase on
  .transcribing, which performPress refuses: the dictation key stayed dead
  until relaunch. It now ends as .failed(.sttFailed(URLError.cancelled)).
  The test that asserted only "not failed" (and so passed on the wedge)
  now asserts the failure and that the next press records.
- AssemblyAISpeechSynthesizer maps URLError.cancelled to
  CancellationError only on abandonment. A stop cancels the synthesis
  task, so stops still read as stops; a read the system cut short now
  fails (the router beeps and logs it) instead of ending silently.
- AsyncTranscriptionClient.isTransient excludes cancellation by shape,
  as its URL-code set already did: no change in what it retries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC
swift-format writes a catch with no pattern before `where` with two
spaces (`catch  where`), as main already does; the lint job's
[Spacing] findings on the two clauses this PR rewrote.

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 a conflict with the base branch Nov 25, 2026
@alexkroman
alexkroman enabled auto-merge November 25, 2026 21:44
#398 made BlurtEngine provider-neutral: it imports neither the AssemblyAI
SDK nor BlurtAssemblyAI. This PR had put the shared cancellation rule in
the SDK and imported it into the engine's router, so:

- isCancellationShaped and isAbandonment move into BlurtEngine, as an
  internal extension (Error+Cancellation.swift). The router and the
  dictation pipeline, the PR's behaviour change, use them unchanged.
  Internal, so nothing that imports both modules (the tests,
  BlurtAssemblyAI) sees two definitions.
- The SDK states the one rule it needs in place: #398 moved the TTS
  request into SpeechClient, whose catch now reads a URLError.cancelled
  as a stop only when its task was cancelled, as this PR's synthesizer
  change did. AsyncTranscriptionClient and the SDK README are main's
  again: this PR never changed what it retries.
- ARCHITECTURE.md: main's text, with this PR's note that a dropped read
  fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1Vp9Andd7LBEDhGKeC
@alexkroman
alexkroman added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit 63f4d26 Nov 25, 2026
22 checks passed
@alexkroman
alexkroman deleted the claude/shared-cancellation-check branch November 25, 2026 22:43
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