The user asked for a faster, real app instead of Electron. The app is now a Rust crate (serenity + songbird); the GUI in M5 will be a Tauri 2 shell on the system WebView2. - Same behaviour as the Electron core: one voice channel, per-speaker PCM to the voice worker, speaking_end and speaker_leave, sentence playback with stop and cancel, transcription notice on join, slash command join/leave/stop (create-or-update), registered chat channels with live events, history and sends that never ping, worker process with a fresh token and restart backoff. - songbird 0.6.1 garbles about a third of received frames under DAVE (its decoder keeps the bytes DAVE decryption leaves at the end of the payload; a 20 ms stepped test tone showed repeats, skips and reordering, while sending was exact). songbird now only decrypts; the core reorders packets in a small window (Opus FEC for the last lost frame) and decodes them with libopus itself. - Live test on real Discord with two bots: five sentences heard (CER 0/0/0/5/12.5%), 0.71-0.90 s from end of speech to final, speech read back at 3.4%, sound gone 0.08 s after stop. The release binary uses about 16 MB of memory; a Windows build (MinGW) logs in under Wine. - Spec moved to app/src/discord_core.spec.md; architecture, decisions, roadmap and pc-features updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
4.4 KiB
Rust
121 lines
4.4 KiB
Rust
//! The worker process with a stand-in `voice_worker` package run by a real Python, so the spawn line
|
|
//! is the one the app uses. JARVIS_TEST_PYTHON overrides the Python (default python3 / python).
|
|
|
|
use std::path::PathBuf;
|
|
use std::time::Duration;
|
|
|
|
use jarvis_app::worker_process::{ProcessEvent, WorkerProcess, WorkerProcessOptions};
|
|
use serde_json::Value;
|
|
|
|
const FAKE_MAIN: &str = r#"
|
|
import json, os, sys, time
|
|
out = os.environ["FAKE_OUT"]
|
|
runs = len(os.listdir(out))
|
|
with open(os.path.join(out, f"run{runs}.json"), "w") as f:
|
|
json.dump({"argv": sys.argv[1:], "token": os.environ.get("JARVIS_VOICE_TOKEN")}, f)
|
|
if os.environ.get("FAKE_CRASH_FIRST") == "1" and runs == 0:
|
|
sys.exit(3)
|
|
print("loading models...", flush=True)
|
|
print("READY 127.0.0.1:" + sys.argv[sys.argv.index("--port") + 1], flush=True)
|
|
time.sleep(60)
|
|
"#;
|
|
|
|
fn python() -> String {
|
|
std::env::var("JARVIS_TEST_PYTHON").unwrap_or_else(|_| if cfg!(windows) { "python".into() } else { "python3".into() })
|
|
}
|
|
|
|
struct Fake {
|
|
root: PathBuf,
|
|
out: PathBuf,
|
|
}
|
|
|
|
impl Fake {
|
|
fn new(tag: &str) -> Self {
|
|
let root = std::env::temp_dir().join(format!("jarvis-fake-worker-{tag}-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&root);
|
|
std::fs::create_dir_all(root.join("voice_worker")).unwrap();
|
|
std::fs::write(root.join("voice_worker/__init__.py"), "").unwrap();
|
|
std::fs::write(root.join("voice_worker/__main__.py"), FAKE_MAIN).unwrap();
|
|
let out = root.join("out");
|
|
std::fs::create_dir_all(&out).unwrap();
|
|
Self { root, out }
|
|
}
|
|
fn run(&self, n: usize) -> Value {
|
|
serde_json::from_str(&std::fs::read_to_string(self.out.join(format!("run{n}.json"))).unwrap()).unwrap()
|
|
}
|
|
fn opts(&self, crash_first: bool) -> WorkerProcessOptions {
|
|
let mut o = WorkerProcessOptions::new(python(), &self.root, vec!["--melo-python".into(), "/x/python".into()]);
|
|
o.env.insert("FAKE_OUT".into(), self.out.display().to_string());
|
|
if crash_first {
|
|
o.env.insert("FAKE_CRASH_FIRST".into(), "1".into());
|
|
}
|
|
o.restart_delay = Duration::from_millis(50);
|
|
o
|
|
}
|
|
}
|
|
|
|
impl Drop for Fake {
|
|
fn drop(&mut self) {
|
|
let _ = std::fs::remove_dir_all(&self.root);
|
|
}
|
|
}
|
|
|
|
async fn wait_for(rx: &mut tokio::sync::broadcast::Receiver<ProcessEvent>, f: impl Fn(&ProcessEvent) -> bool) -> ProcessEvent {
|
|
tokio::time::timeout(Duration::from_secs(10), async {
|
|
loop {
|
|
let e = rx.recv().await.unwrap();
|
|
if f(&e) {
|
|
return e;
|
|
}
|
|
}
|
|
})
|
|
.await
|
|
.expect("timed out")
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn runs_the_worker_on_loopback_with_the_token_only_in_the_environment() {
|
|
let fake = Fake::new("env");
|
|
let p = WorkerProcess::new(fake.opts(false)).unwrap();
|
|
let mut rx = p.subscribe();
|
|
p.start();
|
|
wait_for(&mut rx, |e| *e == ProcessEvent::Ready).await;
|
|
let run = fake.run(0);
|
|
let port = p.port().to_string();
|
|
let argv: Vec<String> = serde_json::from_value(run["argv"].clone()).unwrap();
|
|
assert_eq!(argv, ["--host", "127.0.0.1", "--port", &port, "--melo-python", "/x/python"]);
|
|
assert_eq!(run["token"], p.token.as_str());
|
|
assert!(p.token.len() >= 40);
|
|
assert!(!argv.join(" ").contains(&p.token));
|
|
assert!(p.url().starts_with("ws://127.0.0.1:"));
|
|
p.stop().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_dead_worker_restarts_on_the_same_port_with_the_same_token() {
|
|
let fake = Fake::new("restart");
|
|
let p = WorkerProcess::new(fake.opts(true)).unwrap();
|
|
let mut rx = p.subscribe();
|
|
p.start();
|
|
assert_eq!(wait_for(&mut rx, |e| matches!(e, ProcessEvent::Exited { .. })).await, ProcessEvent::Exited { code: Some(3) });
|
|
wait_for(&mut rx, |e| *e == ProcessEvent::Ready).await;
|
|
assert_eq!(p.restarts(), 1);
|
|
assert_eq!(fake.run(1)["argv"], fake.run(0)["argv"]);
|
|
assert_eq!(fake.run(1)["token"], fake.run(0)["token"]);
|
|
p.stop().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn stop_ends_the_worker_for_good() {
|
|
let fake = Fake::new("stop");
|
|
let p = WorkerProcess::new(fake.opts(false)).unwrap();
|
|
let mut rx = p.subscribe();
|
|
p.start();
|
|
wait_for(&mut rx, |e| *e == ProcessEvent::Ready).await;
|
|
p.stop().await;
|
|
assert!(matches!(rx.recv().await.unwrap(), ProcessEvent::Exited { .. }));
|
|
tokio::time::sleep(Duration::from_millis(200)).await;
|
|
assert!(rx.try_recv().is_err(), "no restart after stop");
|
|
assert_eq!(p.restarts(), 0);
|
|
}
|