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

@@ -170,6 +170,25 @@ impl StreamingResampler {
mod tests {
use super::*;
fn resampled_sine_rms(from_rate: u32, input_frequency: f32) -> f64 {
let sample_count = from_rate as usize;
let samples: Vec<f32> = (0..sample_count)
.map(|i| {
let t = i as f32 / from_rate as f32;
(std::f32::consts::TAU * input_frequency * t).sin()
})
.collect();
let mut resampler = StreamingResampler::new(from_rate).unwrap();
let mut produced = Vec::new();
for chunk in samples.chunks(997) {
produced.extend(resampler.push_samples(chunk).unwrap());
}
produced.extend(resampler.flush().unwrap());
(produced.iter().map(|&s| (s as f64).powi(2)).sum::<f64>() / produced.len() as f64).sqrt()
}
#[test]
fn passthrough_at_16khz() {
let mut r = StreamingResampler::new(16_000).unwrap();
@@ -183,6 +202,24 @@ mod tests {
assert!(StreamingResampler::new(0).is_err());
}
#[test]
fn high_frequency_content_is_filtered_before_downsampling() {
let rms = resampled_sine_rms(48_000, 12_000.0);
assert!(
rms < 0.01,
"12kHz content must be low-pass filtered before 16kHz output with at least ~40dB attenuation; rms={rms}"
);
}
#[test]
fn near_nyquist_content_is_attenuated_before_downsampling() {
let rms = resampled_sine_rms(48_000, 9_000.0);
assert!(
rms < 0.05,
"9kHz content just above 16kHz Nyquist should be materially attenuated; rms={rms}"
);
}
#[test]
fn streaming_48k_to_16k_preserves_duration() {
let from_rate = 48_000u32;