agent: engine slop pass — DSP, typed errors, regex parsing, tracing, audit fixes

External code review on 2026-05-12 rated the codebase 4/10 across audio DSP, error typing, JS injection, env-var safety, ALSA parsing, and async logging. This commit lands the prognosis-level fixes plus three audit follow-ups.

Audio/DSP:
- StreamingResampler/rubato confirmed in the live capture path
- regression test at 12 kHz (rms < 0.01, ~40 dB) catches naive decimation
- near-Nyquist test at 9 kHz (rms < 0.05, ~26 dB) exercises transition band

Core errors:
- Other(String) removed; ProviderNotRegistered introduced
- Io variant restructured as struct with kind/message/raw_os_error
- FileNotFound display quotes paths
- Configuration variant removed (unused)

Core types:
- ModelId, EngineName backed by Cow<'static, str>; const borrowed ctor
- Megabytes::from_gb takes u64 (was f64)
- AudioSamples::sample_rate is NonZeroU32; zero-rate defensive branch removed

Capture:
- /proc/asound/cards parsing rewritten as anchored regex (OnceLock)
- regression test covers product names with embedded colons
- monitor_pattern_detection test restored alongside the regex test
- DEAD_SILENCE_FLOOR promoted to module-level with rationale
- DEVICE_VALIDATION_MS, SILENCE_RMS_FLOOR documented with field-observation rationale
- RMS validation loop made idiomatic
- eprintln! migrated to tracing with structured fields and targets

Tauri startup:
- unsafe std::env::set_var removed; ensure_x11_on_wayland renamed to warn_if_x11_env_unset_on_wayland (launcher/wrapper owns env-var contract)
- DB init + log prune + preferences load collapsed to one block_on
- build_preferences_script rewrites JS injection from JSON.parse string to direct object literal plus malformed-JSON guard and unit tests
- WebKitGTK microphone auto-grant logs warning at startup
- tracing subscriber initialised at top of run() (warn,magnotia=info,... on stderr; honors RUST_LOG); previously eprintln→tracing migration was silent because no subscriber existed

Filename counter:
- RECORDING_COUNTER uses SeqCst

Tests: cargo test --workspace --lib green (322 passed, 0 failed across 10 crates).

Three independent audits (original cleanup → Wren → fresh Codex subagent) concur on no critical findings.

Deferred to docs/superpowers/plans/2026-05-12-engine-slop-residuals.md: storage-layer typed errors, remaining eprintln→tracing sweep, capture actor-model refactor, property-based DSP testing, frontend/backend error boundary cleanup.
This commit is contained in:
2026-05-12 22:03:58 +01:00
parent b463c32f17
commit db654deecc
14 changed files with 557 additions and 208 deletions

View File

@@ -40,10 +40,10 @@ impl WavWriter {
bits_per_sample: 16,
sample_format: hound::SampleFormat::Int,
};
let file = std::fs::File::create(path).map_err(MagnotiaError::Io)?;
let file = std::fs::File::create(path).map_err(MagnotiaError::from)?;
let buffered = BufWriter::new(file);
let inner = hound::WavWriter::new(buffered, spec).map_err(|e| {
MagnotiaError::Io(std::io::Error::other(format!("WAV create failed: {e}")))
MagnotiaError::from(std::io::Error::other(format!("WAV create failed: {e}")))
})?;
Ok(Self {
inner,
@@ -61,7 +61,7 @@ impl WavWriter {
let clamped = sample.clamp(-1.0, 1.0);
let int_sample = (clamped * i16::MAX as f32) as i16;
self.inner.write_sample(int_sample).map_err(|e| {
MagnotiaError::Io(std::io::Error::other(format!("WAV write failed: {e}")))
MagnotiaError::from(std::io::Error::other(format!("WAV write failed: {e}")))
})?;
}
self.samples_since_flush += samples.len();
@@ -78,7 +78,7 @@ impl WavWriter {
/// boundaries (end-of-utterance, UI events) for tighter recovery.
pub fn flush(&mut self) -> Result<()> {
self.inner.flush().map_err(|e| {
MagnotiaError::Io(std::io::Error::other(format!("WAV flush failed: {e}")))
MagnotiaError::from(std::io::Error::other(format!("WAV flush failed: {e}")))
})?;
self.samples_since_flush = 0;
Ok(())
@@ -90,7 +90,7 @@ impl WavWriter {
/// that care about the unflushed tail should always finalise.
pub fn finalize(self) -> Result<()> {
self.inner.finalize().map_err(|e| {
MagnotiaError::Io(std::io::Error::other(format!("WAV finalize failed: {e}")))
MagnotiaError::from(std::io::Error::other(format!("WAV finalize failed: {e}")))
})?;
Ok(())
}
@@ -105,19 +105,20 @@ pub fn write_wav(path: &Path, audio: &AudioSamples) -> Result<()> {
sample_format: hound::SampleFormat::Int,
};
let mut writer = hound::WavWriter::create(path, spec)
.map_err(|e| MagnotiaError::Io(std::io::Error::other(format!("WAV create failed: {e}"))))?;
let mut writer = hound::WavWriter::create(path, spec).map_err(|e| {
MagnotiaError::from(std::io::Error::other(format!("WAV create failed: {e}")))
})?;
for &sample in audio.samples() {
let clamped = sample.clamp(-1.0, 1.0);
let int_sample = (clamped * i16::MAX as f32) as i16;
writer.write_sample(int_sample).map_err(|e| {
MagnotiaError::Io(std::io::Error::other(format!("WAV write failed: {e}")))
MagnotiaError::from(std::io::Error::other(format!("WAV write failed: {e}")))
})?;
}
writer.finalize().map_err(|e| {
MagnotiaError::Io(std::io::Error::other(format!("WAV finalize failed: {e}")))
MagnotiaError::from(std::io::Error::other(format!("WAV finalize failed: {e}")))
})?;
Ok(())