use crate::annotate::{Annotated, Annotation};
use brep_app::app::BrepApp;
use brep_app::automation::command::{Envelope as AppEnvelope, Reply as AppReply};
use brep_app::automation::AppOptions;
use brep_mcp_core::host::{HostConfig, HostHandle, HostInfo, HostRequest, Reply};
use egui_kittest::Harness;
use serde_json::Value;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
pub struct HeadlessConfig {
pub width: f32,
pub height: f32,
pub ppp: f32,
pub seed: bool,
pub store_dir: PathBuf,
pub max_steps: u32,
pub settle_limit: Duration,
}
impl Default for HeadlessConfig {
fn default() -> Self {
Self {
width: 1400.0,
height: 960.0,
ppp: 1.0,
seed: true,
store_dir: std::env::temp_dir().join("brep-mcp-store"),
max_steps: 600,
settle_limit: Duration::from_secs(60),
}
}
}
impl From<HostConfig> for HeadlessConfig {
fn from(c: HostConfig) -> Self {
Self { width: c.width, height: c.height, ppp: c.ppp, seed: c.seed, store_dir: c.store_dir, ..Self::default() }
}
}
fn convert(mut r: AppReply) -> Reply {
let blob = r.blob.take();
let (id, frame) = (r.id, r.frame);
match serde_json::to_value(&r).map_err(|e| e.to_string()).and_then(|v| Reply::from_app(v, blob)) {
Ok(reply) => reply,
Err(e) => Reply::err(id, frame, e),
}
}
static LAST_PANIC: Mutex<Option<String>> = Mutex::new(None);
fn install_panic_hook() {
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| {
let previous = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
let msg = info
.payload()
.downcast_ref::<String>()
.cloned()
.or_else(|| info.payload().downcast_ref::<&str>().map(|s| s.to_string()))
.unwrap_or_else(|| "panic".into());
let loc = info.location().map(|l| format!(" at {}:{}", l.file(), l.line())).unwrap_or_default();
*LAST_PANIC.lock().unwrap() = Some(format!("{msg}{loc}"));
previous(info);
}));
});
}
fn take_panic() -> Option<String> {
LAST_PANIC.lock().unwrap().take()
}
pub fn spawn(cfg: impl Into<HeadlessConfig>) -> Result<HostHandle, String> {
let cfg: HeadlessConfig = cfg.into();
install_panic_hook();
brep_app::automation::registry::set_enabled(true);
brep_app::automation::registry::clear();
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<HostRequest>();
let (ready_tx, ready_rx) = std::sync::mpsc::channel::<Result<HostInfo, String>>();
let join = std::thread::Builder::new()
.name("brep-mcp-headless".into())
.spawn(move || {
let built = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| build(&cfg)));
let (mut harness, info) = match built {
Ok(Ok(v)) => v,
Ok(Err(e)) => {
let _ = ready_tx.send(Err(e));
return;
}
Err(_) => {
let _ = ready_tx.send(Err(format!("the app panicked while starting: {}", take_panic().unwrap_or_default())));
return;
}
};
let queue = harness.state().app().automation().clone();
let ppp = cfg.ppp;
if let Err(why) = settle(&mut harness, &queue, ppp, cfg.settle_limit) {
let _ = ready_tx.send(Err(format!("app was not settled before command 1 \u{2014} {why}")));
return;
}
let _ = ready_tx.send(Ok(info));
while let Some(req) = rx.blocking_recv() {
match req {
HostRequest::Stop => break,
HostRequest::Command { envelope, reply } => {
let id = envelope.id;
let rx_reply = queue.submit(AppEnvelope { id, cmd: envelope.cmd, args: envelope.args });
let mut steps = 0u32;
let result = loop {
if let Ok(r) = rx_reply.try_recv() {
break convert(r);
}
if queue.is_poisoned() {
break Reply::err(id, queue.frame(), "session poisoned");
}
if steps >= cfg.max_steps {
break Reply::err(id, queue.frame(), format!("no reply after {} frames", cfg.max_steps));
}
if let Err(why) = step(&mut harness, &queue, ppp) {
queue.poison(why.clone());
break Reply::err(id, queue.frame(), why);
}
steps += 1;
if steps > 2 {
std::thread::sleep(Duration::from_millis(2));
}
};
let _ = reply.send(result);
}
HostRequest::Annotate { spec, reply } => {
let result = (|| {
let note = Annotation::parse(&spec)?;
let missing = crate::annotate::unknown_keys(¬e.highlight);
if !missing.is_empty() {
return Err(format!("annotate: {}", missing.join("; ")));
}
let highlighted = note.highlight.len();
harness.state_mut().note = note;
step(&mut harness, &queue, ppp)?;
Ok(serde_json::json!({
"frame": queue.frame(),
"highlighted": highlighted,
}))
})();
let _ = reply.send(result);
}
HostRequest::Screenshot { region, reply } => {
let result = (|| {
step(&mut harness, &queue, ppp)?;
let image = harness.render().map_err(|e| format!("headless render: {e}"))?;
let view = harness.state().app().view_rect();
let region: brep_app::automation::cmd_capture::Region =
serde_json::from_value(region).map_err(|e| format!("region: {e}"))?;
let color = egui::ColorImage {
size: [image.width() as usize, image.height() as usize],
source_size: egui::vec2(image.width() as f32, image.height() as f32),
pixels: image
.pixels()
.map(|p| egui::Color32::from_rgba_premultiplied(p.0[0], p.0[1], p.0[2], p.0[3]))
.collect(),
};
brep_app::automation::cmd_capture::encode_capture(&color, ppp, view, ®ion)
})();
let _ = reply.send(result);
}
}
}
})
.map_err(|e| format!("spawn host thread: {e}"))?;
match ready_rx.recv() {
Ok(Ok(info)) => Ok(HostHandle::new(tx, info, join)),
Ok(Err(e)) => Err(e),
Err(_) => Err("the host thread ended before the app was ready".into()),
}
}
type App = Harness<'static, Annotated>;
fn build(cfg: &HeadlessConfig) -> Result<(App, HostInfo), String> {
std::fs::create_dir_all(&cfg.store_dir).map_err(|e| format!("store dir {}: {e}", cfg.store_dir.display()))?;
let store_dir = cfg.store_dir.clone();
let seed = cfg.seed;
let adapter = Arc::new(Mutex::new(String::new()));
let adapter_out = adapter.clone();
let harness = Harness::builder()
.with_size(egui::vec2(cfg.width, cfg.height))
.with_pixels_per_point(cfg.ppp)
.with_step_dt(1.0 / 60.0)
.wgpu()
.build_eframe(move |cc| {
brep_app::fonts::install(&cc.egui_ctx);
let store = brep_app::store::native_store_at(store_dir.clone());
let app = BrepApp::new_with(cc, AppOptions { store: Some(store), seed })
.expect("BrepApp::new_with under kittest");
*adapter_out.lock().unwrap() = app.diagnostics().adapter_line();
Annotated::new(app)
});
let info = HostInfo {
backend: "headless",
adapter: adapter.lock().unwrap().clone(),
platform: std::env::consts::OS,
width: cfg.width,
height: cfg.height,
ppp: cfg.ppp,
};
Ok((harness, info))
}
fn step(harness: &mut App, queue: &Arc<brep_app::automation::queue::AutomationQueue>, ppp: f32) -> Result<(), String> {
let frame = harness.ctx.cumulative_frame_nr();
let view = harness.state().app().view_rect();
let ctx = harness.ctx.clone();
queue.drain_input(harness.input_mut(), frame, ppp, view, &ctx);
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| harness.step()));
match r {
Ok(()) => Ok(()),
Err(_) => Err(format!("the app panicked: {}", take_panic().unwrap_or_else(|| "(no message)".into()))),
}
}
#[derive(Debug, Clone)]
pub enum SettleError {
DeadlineExpired { limit: Duration, frames: u32, pending: Vec<&'static str> },
StepFailed(String),
}
impl std::fmt::Display for SettleError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SettleError::DeadlineExpired { limit, frames, pending } => {
let plural = if *frames == 1 { "" } else { "s" };
write!(f, "the {} ms hang guard expired after {frames} frame{plural}; still pending: ", limit.as_millis())?;
if pending.is_empty() {
write!(f, "nothing, but the app had not been idle for two consecutive frames")
} else {
write!(f, "{}", pending.join(", "))
}
}
SettleError::StepFailed(why) => write!(f, "a frame failed: {why}"),
}
}
}
fn pending_work(harness: &App) -> Vec<&'static str> {
let e = harness.state().app().docs_engine();
[
("run", e.run_pending()),
("queries", e.queries_pending()),
("mesh_imports", e.mesh_imports_pending()),
("step_probes", e.step_probes_pending()),
("topology", e.topology_pending()),
("projection", e.sheet_lines_pending()),
]
.into_iter()
.filter_map(|(name, on)| on.then_some(name))
.collect()
}
fn settle(
harness: &mut App,
queue: &Arc<brep_app::automation::queue::AutomationQueue>,
ppp: f32,
limit: Duration,
) -> Result<(), SettleError> {
let start = Instant::now();
let mut idle_frames = 0u32;
let mut frames = 0u32;
loop {
step(harness, queue, ppp).map_err(SettleError::StepFailed)?;
frames += 1;
let pending = pending_work(harness);
if pending.is_empty() {
idle_frames += 1;
if idle_frames >= 2 {
return Ok(());
}
} else {
idle_frames = 0;
}
if start.elapsed() >= limit {
return Err(SettleError::DeadlineExpired { limit, frames, pending });
}
if !pending.is_empty() {
std::thread::sleep(Duration::from_millis(8));
}
}
}
#[allow(dead_code)]
fn _value(_: Value) {}