hex/src/meeting/recording.rs
Dax 72ac81ad61
fix(meetings): finish recordings across quits and update restarts (#1900)
* fix(meetings): recover recordings lost to a quit or update restart

Rune ends a quitting app with `_exit` unless it is told the app has work
after its loop, so every in-app quit (Quit Hex, `hex quit`, Restart to
update) skipped Hex's drain since the Nov 14 Rune bump. A meeting
recording was killed mid-write: zeroed WAV headers, no `final.pending`,
and nothing to recover at the next launch.

- `rune::orderly_exit()` in the macOS, Windows, and Linux app loops, so
  the existing teardown runs on every quit; the AppKit terminate path
  still drains from its will-terminate notification.
- macOS joins the meeting controller before installing a staged update
  and before the other workers, and a recording gets 30 s to finalise
  rather than 5.
- `meeting::recording()` is a process-wide flag the updater reads.
  Restart to update refuses with a reason, re-checks before swapping,
  and keeps the release staged.
- Launch recovery selects every unlocked unfinished meeting, not just
  `final.pending`; repairs zeroed or wrong WAV RIFF/data sizes from the
  file length, fills sample counts and duration from the audio, and runs
  the existing final pass so the transcript, Markdown export, and notes
  are written. Failed and completed meetings are untouched.

Checks: `a_zeroed_wav_header_is_repaired_from_its_file_length`,
`a_stranded_recording_is_selected_for_recovery`,
`recovery_fills_the_manifest_from_the_repaired_audio`,
`the_flag_holds_until_the_recorder_drops_it`,
`restart_to_update_waits_while_a_meeting_records`.

* fix(meetings): settle recovery attempts and keep retention honest

Reviews of #1900:
- a recovery that dies during the final pass leaves recovery.attempt;
  the next launch marks the meeting Interrupted ("recovery did not
  finish") and never retries that audio. final.pending folders that
  already have a transcript are finished too.
- a repaired, unfinalised track overwrites list_all's relabel with the
  audio's real duration and ended_at.
- retention only waits for stranded meetings while recovery can run
  (Meetings on, model installed); the MCP query uses the same rule.
- repair only rewrites a zero or overlong data chunk, so a trailing
  chunk or pad byte is never read as audio.
- the macOS status item dims Restart to update while a meeting records.
- finish_restart keeps Ready only when an update is staged.
- macOS installs a staged update before joining the controller when
  nothing records; Windows arms a 20 s teardown deadline; Linux writes
  its reopen marker only after a restart is accepted.
- the recording flag is a counter, tested without assuming idleness.

* fix(meetings): bound recovery retries and harden quits

- recovery.attempt now counts attempts: a pass that dies is retried once,
  then the meeting is marked Interrupted ("recovery did not finish") and
  that audio is never handed to Hex again. The pass holds the meeting
  busy flag, so the quit deadline and Restart to update wait for it.
- a finished meeting with a stale attempt marker keeps its final.pending,
  which the ordinary final pass then exports; settling no longer deletes
  it.
- pending recoveries stop between meetings when a recording starts and
  are offered again once Hex is idle.
- an empty finalised track is not "unfinalised"; the audio's duration
  wins whenever repair changed the manifest's track counts.
- can_recover reads meetings.enabled and the model from settings.json in
  readers (`hex mcp`, `hex meetings`), matching the app.
- updater: State::Applied is never overwritten by a later check, and a
  restart check that races a quit never swaps a second time.
- macOS installs the staged bundle before joining the meeting
  controller, marks quitting before any drain, and extends the 5 s exit
  deadline to 30 s from the quit's start while meeting audio is in
  flight. `meeting::record` refuses once quitting.
- Windows marks quitting after the loop, bounds teardown at 20 s (30 s
  while meeting audio is in flight) and exits 1 when the window failed;
  Linux marks quitting before joining meetings and removes a reopen
  marker when a restart is refused.

* fix(updater): keep the staged update installable when a late download is dropped

- check() now returns the state the locks actually show when it discards a
  release during a quit: Applied only if one was installed, else Ready while
  an older update is staged, else Current. install_on_quit therefore still
  installs the staged update a plain quit is about to apply.
- after waiting for meeting audio, the macOS exit deadline gives the rest of
  the drain up to 5 s more, inside the 30 s bound from the quit's start.
  Windows polls meeting audio to 30 s instead of choosing a bound once.
- the Restart to update refusal reads "Hex is finishing a meeting; restart to
  update when it's done", true for a recording and a launch recovery.
- a recording refused because Hex is quitting removes the empty folder it
  created (and only when it holds nothing but the lock file).
- docs: the macOS drain installs before joining the controller, and the
  deadline/discarded-release wording matches the code.
2026-11-16 12:30:57 -05:00

50 lines
1.4 KiB
Rust

//! Whether this process is busy with meeting audio, for the quits and updates
//! that must wait for it to be finalised.
//!
//! A recording and a launch recovery each hold a [`Recording`] for their run,
//! so a quit or a restart to update can see them from any thread without
//! asking the controller.
use std::sync::atomic::{AtomicUsize, Ordering};
static RECORDING: AtomicUsize = AtomicUsize::new(0);
/// Whether a meeting is recording right now.
pub fn recording() -> bool {
RECORDING.load(Ordering::Acquire) > 0
}
/// Marks this process as busy with meeting audio until dropped. Overlapping
/// holders in this process (a recording and a recovery pass) each hold their
/// own; another Hex process has its own counter.
pub(super) struct Recording(());
impl Recording {
pub(super) fn start() -> Self {
RECORDING.fetch_add(1, Ordering::AcqRel);
Self(())
}
}
impl Drop for Recording {
fn drop(&mut self) {
RECORDING.fetch_sub(1, Ordering::AcqRel);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_flag_holds_until_every_recorder_drops_it() {
// No assertion about idleness: other tests may record at the same
// time. Each guard's own hold is what this checks.
let first = Recording::start();
assert!(recording());
let second = Recording::start();
drop(first);
assert!(recording(), "another recorder still holds it");
drop(second);
}
}