Skip to content

Ensure recording and history are saved on app exit - #357

Merged
alexkroman merged 3 commits into
mainfrom
claude/great-hypatia-gd1y1m
Nov 25, 2026
Merged

alexkroman merged 3 commits into
mainfrom
claude/great-hypatia-gd1y1m

Conversation

@alexkroman

Copy link
Copy Markdown
Collaborator

What & why

Adds graceful shutdown handling for in-flight recordings and pending history saves when the app terminates (⌘Q, logout, or Sparkle relaunch). Since deinit doesn't run and async tasks don't resume after applicationWillTerminate, this change ensures:

  1. Recording finalization: Active recordings are stopped and their live transcript is saved to history if non-empty
  2. History persistence: Pending debounced saves are flushed immediately and synchronously, merged with any entries from disk that arrived before this session's load completed

The implementation:

  • Adds prepareForExit() to TranscriptsModel to stop the recorder, save live transcript, and flush history
  • Adds flushHistory() to handle immediate synchronous writes with proper merging
  • Introduces saveQueue (a serial DispatchQueue) to serialize all history writes so older snapshots never land last
  • Makes store, pendingSave, and isLoaded accessible to prepareForExit() by changing their access level
  • Wires prepareForExit() into AppDelegate.applicationWillTerminate(_:)
  • Refactors the debounced save to use saveQueue.async instead of a detached task, ensuring it participates in the serialization

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

Quitting (⌘Q, logout, a Sparkle relaunch) runs no deinit and resumes no
task, so an active Record session left an unfinalized, unplayable .m4a,
its live transcript was never saved, and any change in the 300 ms save
debounce was lost.

- AppDelegate.applicationWillTerminate calls TranscriptsModel.prepareForExit,
  which stops the recorder (closing the file), saves the live transcript as
  a recording entry, and writes the history synchronously, merging the
  saved file first if it hasn't been loaded yet.
- History writes go through one serial queue, so an earlier detached write
  can no longer finish its rename after a later one and resurrect deleted
  entries or drop new ones. The exit flush queues behind any write in
  flight, so it lands last.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1RyMwLTuh8iqUWfZ7i
@alexkroman
alexkroman enabled auto-merge November 24, 2026 22:49
With no output device the drain waits out its whole deadline (0.05 s of
audio plus drainSlack, 2 s), so a flat 3 s bound left under a second of
scheduler room and a loaded runner overshot it (3.39 s on PR #357's check
job). Bound it as the sibling test does: deadline plus slack plus 2 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1RyMwLTuh8iqUWfZ7i
tapMic() lifted with micUp(), which times the finger from the wall clock.
Finger down writes the press, and under the address sanitizer on a busy
runner that alone outlasted the 350 ms hold threshold: the tap became a
hold, its lift queued the stop, and the answer sent it, so the dictation
went straight to stopping. That failed "a settled phase settles the gate
only when it answers the press" on PR #357's ios-sanitizers (address)
job, and a hands-free press or a status-line tap could do the same on a
slow device. tapMic() now lifts at the touch's own stamp.

Adds a test that jumps the model's clock past the hold threshold while
the press is written: before the fix the tap sent a release.

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

prepareForExit (#357) left a recording with no live transcript as a file
with no Library entry; with #363 every recording keeps an entry, so this
one is saved untranscribed (needsTranscription) for the Library to offer
again, as a failed pass's is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
alexkroman pushed a commit that referenced this pull request Nov 25, 2026
…ging #357

Merging #357 brought the file to 401 lines and swiftlint's file_length
failed check. Tightens transcribe's doc comment rather than the merged code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DS5B1V7Z9V8SZ8X1CBBS7q
Merged via the queue into main with commit 3a89eca Nov 25, 2026
13 checks passed
@alexkroman
alexkroman deleted the claude/great-hypatia-gd1y1m branch November 25, 2026 01:13
alexkroman pushed a commit that referenced this pull request Nov 25, 2026
…edes #357's widened wall-clock one

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.

2 participants