hex/src/meeting/sweep.rs
Dax 49de3e1675
meetings: import audio files (labs) (#1462)
* meetings: import audio files behind a Labs switch

Adds Labs › Import audio (macOS, off by default): Meetings offers Import audio… beside Start recording, decodes a WAV, M4A, MP3, AIFF, or CAF file to 16 kHz mono float through a new AVFoundation Swift bridge in bounded blocks, writes it as an ordinary meeting with an empty microphone track, and runs the same final transcript, Markdown export, and notes path a recording does. Imported turns are labelled Speaker in the pane and its Markdown export; a manifest flag keeps older files reading as recorded meetings. Refuses while a recording or another import runs, and a final.pending meeting finishes on the next launch if Hex quits mid-import.

* meetings: harden audio import decoding and cleanup

Fixes review findings on #1462: the staging WAV is named for AVAudioFile, declares kAudioFileWAVEType, closes before Rust reads it, and the converter loop separates input exhaustion from termination, writes every non-empty buffer, downmixes stereo, and reports a zero-frame read as EOF. Every failure before final.pending removes the meeting folder; quitting during the final pass leaves it Interrupted with final.pending for the next launch; shutdown logs import errors and still joins pending finals. The import button and watcher share the managed Labs gate, and the watcher refuses an off-lab import before spawning a worker. Adds macOS decode tests for 44.1 kHz stereo float and 16-bit PCM.

* meetings: cancel abandoned audio imports and sweep their folders

A quit now cancels the AVFoundation decode between blocks, so the existing cleanup removes the meeting folder within the drain. A crash or a quit past the deadline can still leave a staging file and lock; a launch sweep removes those folders only when they have no manifest, contain system.importing.wav, and their lock is free. Also drops read error code 0 as EOF, autopools each converter iteration, and makes the macOS stereo tests silent on the left so a lost downmix fails.

* meetings: keep compressed imports whole and close the import crash window

A short AVAudioFile read no longer ends input; only a zero-frame read, kAudioFileEndOfFileError, or an error at/after the recorded end does. The import now fsyncs its staging WAV, creates and syncs the empty microphone track, and computes its samples before the rename; the manifest write immediately follows the rename. The launch sweep also removes a folder with both finished tracks and no manifest, covering the rename-to-manifest window.

* meetings: mark imports explicitly for the launch sweep

An import writes import.pending under its lock before decoding and removes it only after final.pending is synced; the sweep now requires that marker, no manifest, and a free lock, so it never guesses from the files a folder contains. A tail read error after a short read counts as EOF for MP3s whose estimated length is too high, while a mid-file error after a full read still fails.
2026-11-06 15:25:22 -05:00

136 lines
4.9 KiB
Rust

//! Removes meeting folders left by an audio import that ended before its
//! manifest was written: a crash, or a quit during a long decode. A folder
//! with a manifest is never touched, and a held lock means an import is still
//! using it.
use std::fs;
use std::path::Path;
use super::IMPORT_PENDING_FILE;
const MANIFEST: &str = "manifest.json";
/// Removes abandoned import folders under the meetings root. Runs once per
/// launch, before the pending-finals pass; never fails launch.
pub(crate) fn remove_orphaned_imports() -> usize {
let Ok(root) = super::root() else {
return 0;
};
remove_orphaned_in(&root)
}
fn remove_orphaned_in(root: &Path) -> usize {
let Ok(entries) = fs::read_dir(root) else {
return 0;
};
let mut removed = 0;
for entry in entries.flatten() {
let directory = entry.path();
if !directory.is_dir() {
continue;
}
// A manifest means a real meeting; never look further.
if directory.join(MANIFEST).exists() {
continue;
}
// Only the marker written before decode and removed after
// final.pending identifies an interrupted import. Nothing else is
// guessed at from the files a folder happens to contain.
if !directory.join(IMPORT_PENDING_FILE).exists() {
continue;
}
let active = match super::ActiveMeeting::try_acquire(&directory) {
Ok(Some(active)) => active,
Ok(None) => continue,
Err(error) => {
tracing::warn!(
%error,
path = %directory.display(),
"could not inspect an abandoned import folder"
);
continue;
}
};
// The lock was only a check that no import still writes here; drop it
// so Windows can remove the folder with the lock file inside.
drop(active);
match fs::remove_dir_all(&directory) {
Ok(()) => {
removed += 1;
tracing::info!(
path = %directory.display(),
"removed an abandoned audio import folder"
);
}
Err(error) => {
tracing::warn!(
%error,
path = %directory.display(),
"could not remove an abandoned audio import folder"
);
}
}
}
removed
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
fn temp_root(name: &str) -> PathBuf {
let root =
std::env::temp_dir().join(format!("hex-meeting-sweep-{name}-{}", std::process::id()));
let _ = fs::remove_dir_all(&root);
fs::create_dir_all(&root).unwrap();
root
}
fn folder(root: &Path, name: &str) -> PathBuf {
let directory = root.join(name);
fs::create_dir_all(&directory).unwrap();
directory
}
#[test]
fn only_abandoned_import_folders_are_removed() {
let root = temp_root("orphans");
// A marker with a partial staging file, before the import's rename.
let orphan = folder(&root, "orphan");
fs::write(orphan.join(IMPORT_PENDING_FILE), b"").unwrap();
fs::write(orphan.join("system.importing.wav"), b"partial").unwrap();
// A marker with both finished tracks, between rename and manifest.
let renamed = folder(&root, "renamed");
fs::write(renamed.join(IMPORT_PENDING_FILE), b"").unwrap();
fs::write(renamed.join("system.wav"), b"audio").unwrap();
fs::write(renamed.join("microphone.wav"), b"empty").unwrap();
// Both tracks with no marker are never assumed to be an import.
let recording = folder(&root, "recording");
fs::write(recording.join("system.wav"), b"audio").unwrap();
fs::write(recording.join("microphone.wav"), b"empty").unwrap();
// A marker with a manifest is a meeting, pending or finished.
let finished = folder(&root, "finished");
fs::write(finished.join(IMPORT_PENDING_FILE), b"").unwrap();
fs::write(finished.join(MANIFEST), b"{}").unwrap();
// A held lock means an import still uses the folder.
let busy = folder(&root, "busy");
fs::write(busy.join(IMPORT_PENDING_FILE), b"").unwrap();
fs::write(busy.join("system.importing.wav"), b"partial").unwrap();
let held = super::super::ActiveMeeting::acquire(&busy).unwrap();
assert_eq!(remove_orphaned_in(&root), 2);
assert!(!orphan.exists(), "the pre-rename import was removed");
assert!(!renamed.exists(), "the renamed import was removed");
assert!(recording.exists(), "tracks without a marker stay");
assert!(finished.exists(), "a folder with a manifest stays");
assert!(busy.exists(), "a held lock keeps its folder");
drop(held);
fs::remove_dir_all(root).unwrap();
}
}