vibesurfer 0.1.10

A real browser for your local AI agent.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! `vs serve` — host the daemon in this process.
//!
//! Folded into the `vs` binary so the project ships exactly one CLI
//! surface; the M0 `vibesurferd` binary is gone. Auto-spawn re-execs
//! `vs serve` instead.
//!
//! # Threading model
//!
//! On **macOS**, `WKWebView` is hard-pinned to the Cocoa main thread.
//! [`run`] therefore stays on the OS main thread, initializes
//! `NSApplication`, constructs the `WkBackend` here, and spawns a
//! worker thread that runs the tokio runtime + the daemon. Engine calls
//! issued by the daemon (on tokio workers) flow through an mpsc
//! channel back to main, where they're drained between NSRunLoop
//! ticks. See [`vs_engine_webkit::runtime::MainThreadDispatcher`].
//!
//! On **Linux**, the same shape applies with a GLib main context and
//! WebKitGTK 6. On **Windows**, with a Win32 message pump and
//! WebView2.

use std::sync::Arc;

use anyhow::{Context as _, Result};
use vs_daemon::{config::Paths as DaemonPaths, server, Daemon};

/// Args specific to `vs serve`. `paths` is the resolved daemon home.
pub struct ServeArgs {
    pub paths: DaemonPaths,
    /// If true, do not start a daemon — instead, read the PID file,
    /// send SIGTERM, and wait for the socket to disappear.
    pub stop: bool,
}

/// `vs serve --stop`. Reads the daemon PID file, sends SIGTERM (Unix)
/// or TerminateProcess (Windows), and waits up to 5s for the socket
/// file to disappear. Stale PID files for processes that already exit
/// are cleaned up.
pub fn run_stop(paths: &DaemonPaths) -> Result<()> {
    let pid_file = paths.pid_file();
    let pid_str = match std::fs::read_to_string(&pid_file) {
        Ok(s) => s,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            eprintln!("no daemon running (no PID file at {})", pid_file.display());
            return Ok(());
        }
        Err(e) => return Err(e).context("read pid file"),
    };
    let pid: i32 = pid_str
        .trim()
        .parse()
        .with_context(|| format!("malformed PID file: {pid_str:?}"))?;
    #[cfg(unix)]
    {
        let r = unsafe { libc::kill(pid, libc::SIGTERM) };
        if r != 0 {
            let e = std::io::Error::last_os_error();
            if e.raw_os_error() == Some(libc::ESRCH) {
                let _ = std::fs::remove_file(&pid_file);
                eprintln!("no daemon running (cleaned stale PID file for pid {pid})");
                return Ok(());
            }
            return Err(anyhow::anyhow!("kill({pid}, SIGTERM) failed: {e}"));
        }
    }
    #[cfg(windows)]
    {
        use windows::Win32::Foundation::CloseHandle;
        use windows::Win32::System::Threading::{OpenProcess, TerminateProcess, PROCESS_TERMINATE};
        unsafe {
            let h = OpenProcess(PROCESS_TERMINATE, false, u32::try_from(pid).unwrap_or(0))
                .map_err(|e| anyhow::anyhow!("OpenProcess({pid}): {e}"))?;
            let r = TerminateProcess(h, 0);
            let _ = CloseHandle(h);
            r.map_err(|e| anyhow::anyhow!("TerminateProcess({pid}): {e}"))?;
        }
    }
    let socket = paths.socket();
    for _ in 0..50 {
        if !socket.exists() {
            eprintln!("daemon stopped (pid {pid})");
            return Ok(());
        }
        std::thread::sleep(std::time::Duration::from_millis(100));
    }
    Err(anyhow::anyhow!(
        "daemon (pid {pid}) did not exit within 5s; socket still present at {}",
        socket.display()
    ))
}

/// Future that resolves on SIGTERM (Unix) or never (other platforms).
/// Lets the server loop treat SIGTERM equivalently to ctrl-c.
#[cfg(unix)]
pub async fn wait_terminate() {
    if let Ok(mut s) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
        s.recv().await;
    } else {
        std::future::pending::<()>().await;
    }
}

#[cfg(not(unix))]
pub async fn wait_terminate() {
    std::future::pending::<()>().await;
}

// =============================================================================
// macOS: NSApp on main, tokio on worker, real WKWebView backend.
// =============================================================================

#[cfg(target_os = "macos")]
#[allow(clippy::too_many_lines)]
pub fn run(args: &ServeArgs) -> Result<()> {
    use objc2::MainThreadMarker;
    use objc2_app_kit::NSApplication;
    use objc2_foundation::{NSDate, NSDefaultRunLoopMode, NSRunLoop};
    use vs_engine_webkit::{backend::webkit::WkBackend, Engine, EngineRuntime};

    if args.stop {
        return run_stop(&args.paths);
    }

    init_tracing();
    install_panic_hook();
    install_seh_handler();
    args.paths.ensure_root().context("ensure ~/.vibesurfer")?;

    let mtm = MainThreadMarker::new()
        .ok_or_else(|| anyhow::anyhow!("vs serve must be invoked from the OS main thread"))?;
    // Initialize NSApp; required for WKWebView even though we don't run
    // the AppKit event loop directly.
    let _app = NSApplication::sharedApplication(mtm);

    let store = vs_store::Store::open(args.paths.db()).context("open state.db")?;
    let captures_dir = args.paths.captures();
    let skills_dir = args.paths.root.join("skills");

    // Engine lives on this thread (the Cocoa main thread). Construct
    // the WkBackend here and hand it to `EngineRuntime::dispatcher`,
    // which gives us back a runtime handle (for the daemon) and a
    // dispatcher we drive in this thread's run loop.
    let backend = WkBackend::new(mtm).with_capture_dir(captures_dir.clone());
    let engine_box: Box<dyn Engine> = Box::new(backend);
    let (engine_runtime, mut dispatcher) = EngineRuntime::dispatcher(engine_box);
    let engine_runtime = Arc::new(engine_runtime);

    let mut daemon = Daemon::new(store, engine_runtime.clone())
        .with_captures_dir(captures_dir)
        .with_skills_dir(skills_dir);

    if let Ok(k) = vs_store::MasterKey::resolve(args.paths.key_file()) {
        daemon = daemon.with_master_key(k);
    } else {
        tracing::warn!(
            "no master key (keyring entry missing and {} not present); vs_auth save|load will fail",
            args.paths.key_file().display()
        );
    }

    let socket = args.paths.socket();
    let pid_path = args.paths.pid_file();

    // Spawn the tokio runtime on a worker. It owns the daemon and the
    // socket server; ctrl-c on the worker triggers a graceful shutdown
    // by closing `shutdown_rx` and dropping the runtime, which closes
    // our engine channel and pops us out of the run-loop below.
    let server_thread = std::thread::Builder::new()
        .name("vs-daemon-tokio".into())
        .spawn(move || -> Result<()> {
            let rt = tokio::runtime::Builder::new_multi_thread()
                .worker_threads(4)
                .enable_all()
                .build()
                .context("build tokio runtime")?;
            if let Err(e) = std::fs::write(&pid_path, std::process::id().to_string()) {
                tracing::warn!(?pid_path, error = %e, "write pid file");
            }
            rt.block_on(async move {
                let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
                let mut server =
                    tokio::spawn(async move { server::serve(daemon, socket, shutdown_rx).await });
                tokio::select! {
                    _ = tokio::signal::ctrl_c() => {
                        tracing::info!("ctrl-c received, shutting down");
                        let _ = shutdown_tx.send(());
                        if let Ok(Err(e)) = server.await {
                            tracing::error!(error = %e, "server task ended with error");
                        }
                    }
                    () = wait_terminate() => {
                        tracing::info!("SIGTERM received, shutting down");
                        let _ = shutdown_tx.send(());
                        if let Ok(Err(e)) = server.await {
                            tracing::error!(error = %e, "server task ended with error");
                        }
                    }
                    res = &mut server => {
                        // server::serve returned without an external
                        // signal — typically a bind failure. Surface it
                        // before the run loop exits.
                        match res {
                            Ok(Err(e)) => tracing::error!(
                                error = %e,
                                "server task failed before shutdown signal"
                            ),
                            Err(e) => tracing::error!(
                                error = %e,
                                "server task panicked"
                            ),
                            Ok(Ok(())) => {}
                        }
                    }
                }
            });
            let _ = std::fs::remove_file(&pid_path);
            // Dropping `rt` and the moved `engine_runtime` (held by the
            // daemon) closes the engine channel, which signals the main
            // loop to exit.
            drop(rt);
            Ok(())
        })
        .context("spawn vs-daemon-tokio thread")?;

    // Main run-loop: drain engine jobs, then pump NSRunLoop briefly.
    // Exit when the channel closes (the worker dropped the daemon).
    let runloop = NSRunLoop::currentRunLoop();
    'main: loop {
        // Drain all queued jobs.
        loop {
            match dispatcher.tick() {
                Ok(true) => {}
                Ok(false) => break,
                Err(()) => break 'main,
            }
        }
        // Pump the runloop briefly so WKWebView delegates / JS
        // completion handlers fire on this thread.
        let slice = NSDate::dateWithTimeIntervalSinceNow(0.05);
        unsafe { runloop.runMode_beforeDate(NSDefaultRunLoopMode, &slice) };
    }

    let _ = server_thread.join();
    drop(engine_runtime); // explicit, even though it's already dead
    Ok(())
}

// =============================================================================
// Linux: GTK on main, tokio on worker, real WebKitGTK 6 backend.
// =============================================================================

#[cfg(target_os = "linux")]
#[allow(clippy::too_many_lines)]
pub fn run(args: &ServeArgs) -> Result<()> {
    use vs_engine_webkit::{backend::wpe::WpeBackend, Engine, EngineRuntime};

    init_tracing();
    install_panic_hook();
    install_seh_handler();
    args.paths.ensure_root().context("ensure ~/.vibesurfer")?;
    if args.stop {
        return run_stop(&args.paths);
    }

    // GTK init must happen on the OS main thread, before any WebView.
    gtk4::init().context("gtk4 init")?;

    let store = vs_store::Store::open(args.paths.db()).context("open state.db")?;
    let captures_dir = args.paths.captures();
    let skills_dir = args.paths.root.join("skills");

    let backend = WpeBackend::new().with_capture_dir(captures_dir.clone());
    let engine_box: Box<dyn Engine> = Box::new(backend);
    let (engine_runtime, mut dispatcher) = EngineRuntime::dispatcher(engine_box);
    let engine_runtime = Arc::new(engine_runtime);

    let mut daemon = Daemon::new(store, engine_runtime.clone())
        .with_captures_dir(captures_dir)
        .with_skills_dir(skills_dir);

    if let Ok(k) = vs_store::MasterKey::resolve(args.paths.key_file()) {
        daemon = daemon.with_master_key(k);
    } else {
        tracing::warn!(
            "no master key (keyring entry missing and {} not present); vs_auth save|load will fail",
            args.paths.key_file().display()
        );
    }

    let socket = args.paths.socket();
    let pid_path = args.paths.pid_file();

    let server_thread = std::thread::Builder::new()
        .name("vs-daemon-tokio".into())
        .spawn(move || -> Result<()> {
            let rt = tokio::runtime::Builder::new_multi_thread()
                .worker_threads(4)
                .enable_all()
                .build()
                .context("build tokio runtime")?;
            if let Err(e) = std::fs::write(&pid_path, std::process::id().to_string()) {
                tracing::warn!(?pid_path, error = %e, "write pid file");
            }
            rt.block_on(async move {
                let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
                let mut server =
                    tokio::spawn(async move { server::serve(daemon, socket, shutdown_rx).await });
                tokio::select! {
                    _ = tokio::signal::ctrl_c() => {
                        tracing::info!("ctrl-c received, shutting down");
                        let _ = shutdown_tx.send(());
                        if let Ok(Err(e)) = server.await {
                            tracing::error!(error = %e, "server task ended with error");
                        }
                    }
                    () = wait_terminate() => {
                        tracing::info!("SIGTERM received, shutting down");
                        let _ = shutdown_tx.send(());
                        if let Ok(Err(e)) = server.await {
                            tracing::error!(error = %e, "server task ended with error");
                        }
                    }
                    res = &mut server => {
                        match res {
                            Ok(Err(e)) => tracing::error!(
                                error = %e,
                                "server task failed before shutdown signal"
                            ),
                            Err(e) => tracing::error!(
                                error = %e,
                                "server task panicked"
                            ),
                            Ok(Ok(())) => {}
                        }
                    }
                }
            });
            let _ = std::fs::remove_file(&pid_path);
            drop(rt);
            Ok(())
        })
        .context("spawn vs-daemon-tokio thread")?;

    // Pump the GLib main context on the main thread, draining engine
    // jobs between iterations. Exit when the channel closes.
    let main_ctx = glib::MainContext::default();
    'main: loop {
        loop {
            match dispatcher.tick() {
                Ok(true) => {}
                Ok(false) => break,
                Err(()) => break 'main,
            }
        }
        // Iterate non-blocking — if the GLib loop has nothing to do,
        // sleep briefly so we don't burn CPU.
        if !main_ctx.iteration(false) {
            std::thread::sleep(std::time::Duration::from_millis(10));
        }
    }

    let _ = server_thread.join();
    drop(engine_runtime);
    Ok(())
}

// =============================================================================
// Windows: WebView2 + Win32 message pump on main, tokio on worker.
// =============================================================================

#[cfg(target_os = "windows")]
#[allow(clippy::too_many_lines)]
pub fn run(args: &ServeArgs) -> Result<()> {
    use vs_engine_webkit::{backend::webview2::Webview2Backend, Engine, EngineRuntime};
    use windows::Win32::System::Com::{CoInitializeEx, COINIT_APARTMENTTHREADED};
    use windows::Win32::UI::WindowsAndMessaging::{
        DispatchMessageW, PeekMessageW, TranslateMessage, MSG, PM_REMOVE,
    };

    init_tracing();
    install_panic_hook();
    install_seh_handler();
    args.paths.ensure_root().context("ensure ~/.vibesurfer")?;
    if args.stop {
        return run_stop(&args.paths);
    }

    // SAFETY: required first call on this thread before any
    // WebView2 COM API. RPC_E_CHANGED_MODE on second call is fine.
    unsafe {
        let _ = CoInitializeEx(None, COINIT_APARTMENTTHREADED);
    }

    let store = vs_store::Store::open(args.paths.db()).context("open state.db")?;
    let captures_dir = args.paths.captures();
    let skills_dir = args.paths.root.join("skills");

    let backend = Webview2Backend::new().with_capture_dir(captures_dir.clone());
    let engine_box: Box<dyn Engine> = Box::new(backend);
    let (engine_runtime, mut dispatcher) = EngineRuntime::dispatcher(engine_box);
    let engine_runtime = Arc::new(engine_runtime);

    let mut daemon = Daemon::new(store, engine_runtime.clone())
        .with_captures_dir(captures_dir)
        .with_skills_dir(skills_dir);

    if let Ok(k) = vs_store::MasterKey::resolve(args.paths.key_file()) {
        daemon = daemon.with_master_key(k);
    } else {
        tracing::warn!(
            "no master key (keyring entry missing and {} not present); vs_auth save|load will fail",
            args.paths.key_file().display()
        );
    }

    let socket = args.paths.socket();
    let pid_path = args.paths.pid_file();

    let server_thread = std::thread::Builder::new()
        .name("vs-daemon-tokio".into())
        .spawn(move || -> Result<()> {
            let rt = tokio::runtime::Builder::new_multi_thread()
                .worker_threads(4)
                .enable_all()
                .build()
                .context("build tokio runtime")?;
            if let Err(e) = std::fs::write(&pid_path, std::process::id().to_string()) {
                tracing::warn!(?pid_path, error = %e, "write pid file");
            }
            rt.block_on(async move {
                let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
                let mut server =
                    tokio::spawn(async move { server::serve(daemon, socket, shutdown_rx).await });
                tokio::select! {
                    _ = tokio::signal::ctrl_c() => {
                        tracing::info!("ctrl-c received, shutting down");
                        let _ = shutdown_tx.send(());
                        if let Ok(Err(e)) = server.await {
                            tracing::error!(error = %e, "server task ended with error");
                        }
                    }
                    () = wait_terminate() => {
                        tracing::info!("SIGTERM received, shutting down");
                        let _ = shutdown_tx.send(());
                        if let Ok(Err(e)) = server.await {
                            tracing::error!(error = %e, "server task ended with error");
                        }
                    }
                    res = &mut server => {
                        match res {
                            Ok(Err(e)) => tracing::error!(
                                error = %e,
                                "server task failed before shutdown signal"
                            ),
                            Err(e) => tracing::error!(
                                error = %e,
                                "server task panicked"
                            ),
                            Ok(Ok(())) => {}
                        }
                    }
                }
            });
            let _ = std::fs::remove_file(&pid_path);
            drop(rt);
            Ok(())
        })
        .context("spawn vs-daemon-tokio thread")?;

    // Pump Win32 messages on the main thread, draining engine jobs
    // between iterations. Exit when the channel closes.
    let mut shutdown = false;
    while !shutdown {
        loop {
            match dispatcher.tick() {
                Ok(true) => {}
                Ok(false) => break,
                Err(()) => {
                    shutdown = true;
                    break;
                }
            }
        }
        // Non-blocking PeekMessage. If a message exists, dispatch
        // (WebView2 callback completions arrive this way).
        let mut msg = MSG::default();
        unsafe {
            while PeekMessageW(&raw mut msg, None, 0, 0, PM_REMOVE).as_bool() {
                let _ = TranslateMessage(&raw const msg);
                DispatchMessageW(&raw const msg);
            }
        }
        std::thread::sleep(std::time::Duration::from_millis(10));
    }

    let _ = server_thread.join();
    drop(engine_runtime);
    Ok(())
}

fn init_tracing() {
    if tracing::dispatcher::has_been_set() {
        return;
    }
    let _ = tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("vs_daemon=info,info")),
        )
        .with_writer(std::io::stderr)
        .try_init();
}

/// Install a panic hook that logs the panic via tracing::error
/// before the default hook prints to stderr. The default hook
/// already writes to stderr, but on Windows a panic on a non-main
/// thread can sometimes terminate the process before stderr is
/// flushed; routing through tracing first guarantees the panic
/// reaches the daemon log file the test harness captures.
fn install_panic_hook() {
    let prev = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |info| {
        let location = info
            .location()
            .map_or_else(|| "<unknown>".to_string(), ToString::to_string);
        let msg = info
            .payload()
            .downcast_ref::<&str>()
            .copied()
            .or_else(|| info.payload().downcast_ref::<String>().map(String::as_str))
            .unwrap_or("<no message>");
        tracing::error!(at = %location, "PANIC: {msg}");
        prev(info);
    }));
}

/// Install a Win32 vectored exception handler that logs structured
/// exceptions (access violation, stack overflow, etc.) before the
/// process dies. Rust's panic machinery does not catch SEH on
/// Windows by default — when a COM call inside `Webview2Backend`
/// dereferences bad memory, the daemon vanishes with no message,
/// no panic hook firing, no trace.
///
/// This hook writes a single line to stderr containing the
/// exception code + faulting address, then returns
/// `EXCEPTION_CONTINUE_SEARCH` so the OS's default handler runs
/// (process death). Stderr is captured to the daemon log by the
/// test harness, so the line lands in CI output.
///
/// Direct stderr write — not tracing — because the allocator may
/// be in a bad state during an SEH; tracing's formatting could
/// deadlock. `writeln!` to a locked stderr handle is the cheapest
/// thing that can work here.
#[cfg(target_os = "windows")]
fn install_seh_handler() {
    use std::io::Write;
    use windows::Win32::System::Diagnostics::Debug::{
        AddVectoredExceptionHandler, EXCEPTION_POINTERS,
    };

    unsafe extern "system" fn handler(info: *mut EXCEPTION_POINTERS) -> i32 {
        if info.is_null() {
            return 0; // EXCEPTION_CONTINUE_SEARCH
        }
        let info = unsafe { &*info };
        if info.ExceptionRecord.is_null() {
            return 0;
        }
        let rec = unsafe { &*info.ExceptionRecord };
        // Filter to fatal-class exceptions only. Status codes in the
        // 0xC0... range are NTSTATUS errors; lower codes are e.g.
        // C++ EH (0xE06D7363), DLL not found, debug breakpoints —
        // not interesting and not always fatal.
        // NTSTATUS is i32 internally — bit-reinterpret to u32 for
        // hex display + range check. `as u32` would clippy-deny on
        // `cast_sign_loss`; the to_ne_bytes round-trip is bit-exact.
        let code = u32::from_ne_bytes(rec.ExceptionCode.0.to_ne_bytes());
        if code & 0xF000_0000 != 0xC000_0000 {
            return 0;
        }
        let mut err = std::io::stderr().lock();
        let _ = writeln!(
            err,
            "VIBESURFER_SEH code=0x{:08x} ip={:p} flags=0x{:x} params={}",
            code, rec.ExceptionAddress, rec.ExceptionFlags, rec.NumberParameters,
        );
        // For STATUS_ACCESS_VIOLATION, ExceptionInformation carries
        // [access_kind, faulting_va] — log both. access_kind: 0=read,
        // 1=write, 8=DEP/execute. faulting_va is the address that was
        // being read / written / executed at the time of the fault.
        if code == 0xC000_0005 && rec.NumberParameters >= 2 {
            let kind = rec.ExceptionInformation[0];
            let va = rec.ExceptionInformation[1];
            let kind_str = match kind {
                0 => "read",
                1 => "write",
                8 => "execute",
                _ => "?",
            };
            let _ = writeln!(
                err,
                "VIBESURFER_SEH access={kind_str} (kind={kind}) faulting_va=0x{va:x}",
            );
        }
        // Also dump RIP / RSP / a few callee-saved registers from the
        // ContextRecord. RIP confirms the IP from ExceptionAddress;
        // RSP + return-address-on-stack often points at the caller
        // even when ExceptionAddress is 0x0.
        if !info.ContextRecord.is_null() {
            let ctx = unsafe { &*info.ContextRecord };
            #[cfg(target_arch = "x86_64")]
            {
                let _ = writeln!(
                    err,
                    "VIBESURFER_SEH rip=0x{:x} rsp=0x{:x} rbp=0x{:x} rcx=0x{:x} rdx=0x{:x}",
                    ctx.Rip, ctx.Rsp, ctx.Rbp, ctx.Rcx, ctx.Rdx,
                );
            }
            #[cfg(target_arch = "aarch64")]
            {
                let _ = writeln!(
                    err,
                    "VIBESURFER_SEH pc=0x{:x} sp=0x{:x} fp=0x{:x}",
                    ctx.Pc, ctx.Sp, ctx.Fp,
                );
            }
        }
        let _ = err.flush();
        0
    }

    unsafe {
        AddVectoredExceptionHandler(1 /* CALL_FIRST */, Some(handler));
    }
}

/// No-op on non-Windows. The Unix kernels we target deliver crashes
/// as signals (SIGSEGV etc.); Rust's panic + signal handling already
/// covers those.
#[cfg(not(target_os = "windows"))]
fn install_seh_handler() {}