obsidian-mcp 2.2.0

MCP server for Obsidian vaults — direct filesystem access for AI agents
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
//! Daemon installer/bootstrap manager for shared semantic runtime.

use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::time::Duration;

use serde_json::json;
use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader};

#[cfg(unix)]
use tokio::net::UnixStream;
#[cfg(windows)]
use tokio::net::windows::named_pipe::ClientOptions;

use crate::config::SemanticRuntimeConfig;
use crate::error::{VaultError, VaultResult};

use super::home::{self, InstallLock, SemanticHomePaths};
use super::manifest::{self, ManifestIpc, RuntimeManifest, RuntimeManifestInput};
use super::protocol::{self, DAEMON_API_VERSION, ERR_INCOMPATIBLE_API_VERSION};
use super::server::IpcEndpoint;

const DEFAULT_DOWNLOAD_BASE_URL: &str =
    "https://github.com/lstpsche/obsidian-mcp/releases/download";

#[derive(Debug, Clone)]
pub struct BootstrapConfig {
    pub semantic_home_override: Option<PathBuf>,
    pub daemon_path_override: Option<PathBuf>,
    pub model_name: String,
    pub download_url_override: Option<String>,
    pub bootstrap_client_name: String,
    pub bootstrap_client_version: String,
}

impl BootstrapConfig {
    pub fn from_env() -> Self {
        let runtime = SemanticRuntimeConfig::load_from_env();
        Self {
            semantic_home_override: runtime.semantic_home_override,
            daemon_path_override: runtime.daemon_path_override,
            model_name: runtime.model_name,
            download_url_override: runtime.daemon_download_url,
            bootstrap_client_name: "obsidian-mcp".to_string(),
            bootstrap_client_version: env!("CARGO_PKG_VERSION").to_string(),
        }
    }
}

impl Default for BootstrapConfig {
    fn default() -> Self {
        Self::from_env()
    }
}

#[derive(Debug, Clone)]
pub struct BootstrapResult {
    pub semantic_home: PathBuf,
    pub endpoint: IpcEndpoint,
    pub daemon_binary_path: PathBuf,
    pub manifest: RuntimeManifest,
    pub reused_existing: bool,
}

#[derive(Debug)]
enum HealthProbeOutcome {
    Healthy(protocol::HealthResult),
    Unreachable,
    Incompatible(String),
    Invalid(String),
}

/// Ensure a shared daemon exists and is healthy.
pub async fn ensure_daemon(config: &BootstrapConfig) -> VaultResult<BootstrapResult> {
    let semantic_home =
        home::resolve_semantic_home_with_override(config.semantic_home_override.as_deref(), None)?;
    let paths = home::semantic_home_paths(&semantic_home);
    home::ensure_home_layout(&paths)?;

    let _install_lock = InstallLock::acquire_async(&paths).await?;
    let mut existing_manifest = manifest::load(&paths.manifest_path)?;
    let default_endpoint = home::default_ipc_endpoint(&paths);

    if let Some(current_manifest) = existing_manifest.as_mut() {
        let endpoint =
            endpoint_from_manifest(current_manifest).unwrap_or_else(|| default_endpoint.clone());
        match probe_health(&endpoint).await? {
            HealthProbeOutcome::Healthy(_) => {
                current_manifest.touch_health();
                manifest::save_atomic(&paths.manifest_path, current_manifest)?;
                let daemon_binary_path = PathBuf::from(&current_manifest.binary_path);
                return Ok(BootstrapResult {
                    semantic_home,
                    endpoint,
                    daemon_binary_path,
                    manifest: current_manifest.clone(),
                    reused_existing: true,
                });
            }
            HealthProbeOutcome::Incompatible(message) => {
                return Err(VaultError::DaemonBootstrap(format!(
                    "existing daemon is incompatible with API v{DAEMON_API_VERSION}: {message}"
                )));
            }
            HealthProbeOutcome::Invalid(message) => {
                tracing::warn!(error = %message, "manifest endpoint responded but was invalid; daemon will be restarted");
            }
            HealthProbeOutcome::Unreachable => {
                tracing::info!("manifest endpoint unreachable; daemon will be started");
            }
        }
    }

    let endpoint = existing_manifest
        .as_ref()
        .and_then(endpoint_from_manifest)
        .unwrap_or(default_endpoint);

    let mut daemon_binary_path =
        resolve_daemon_binary_path(config, &paths, existing_manifest.as_ref())?;
    let mut binary_sha256 = existing_manifest
        .as_ref()
        .and_then(|manifest| manifest.binary_sha256.clone());

    if !daemon_binary_path.exists() {
        if config.daemon_path_override.is_some() {
            return Err(VaultError::DaemonBootstrap(format!(
                "daemon override path does not exist: {}",
                daemon_binary_path.display()
            )));
        }
        if let Some(path_binary) = find_daemon_on_path() {
            tracing::info!(path = %path_binary.display(), "using daemon binary found on $PATH");
            daemon_binary_path = path_binary;
        } else {
            let download_url = resolve_download_url(config)?;
            tracing::info!(url = %download_url, "downloading semantic daemon binary");
            let checksum = download_and_install(&download_url, &daemon_binary_path).await?;
            binary_sha256 = Some(checksum);
        }
    }

    let mut child =
        start_daemon_process(&daemon_binary_path, &paths, &endpoint, &config.model_name)?;
    let pid = child.id().unwrap_or_default();

    tokio::time::sleep(Duration::from_millis(50)).await;
    match child.try_wait() {
        Ok(Some(status)) => {
            return Err(VaultError::DaemonBootstrap(format!(
                "daemon process exited immediately with {status} \
                 (binary: '{}', check {})",
                daemon_binary_path.display(),
                paths.daemon_stderr_log_path.display()
            )));
        }
        Ok(None) => {}
        Err(err) => {
            tracing::warn!(error = %err, "failed to check daemon process status after spawn");
        }
    }
    drop(child);

    let health = wait_for_health(&endpoint, Duration::from_secs(10)).await?;
    let ipc = ManifestIpc {
        transport: endpoint_transport(&endpoint).to_string(),
        endpoint: endpoint.endpoint_string(),
    };

    let runtime_manifest = RuntimeManifest::from_input(RuntimeManifestInput {
        daemon_api_version: health.daemon_api_version,
        daemon_version: health.daemon_version,
        binary_path: daemon_binary_path.display().to_string(),
        binary_sha256,
        ipc,
        pid,
        semantic_home: semantic_home.display().to_string(),
        fastembed_cache_dir: paths.fastembed_cache_dir.display().to_string(),
        model_name: config.model_name.clone(),
        bootstrap_client_name: config.bootstrap_client_name.clone(),
        bootstrap_client_version: config.bootstrap_client_version.clone(),
    });
    manifest::save_atomic(&paths.manifest_path, &runtime_manifest)?;

    Ok(BootstrapResult {
        semantic_home,
        endpoint,
        daemon_binary_path,
        manifest: runtime_manifest,
        reused_existing: false,
    })
}

fn resolve_daemon_binary_path(
    config: &BootstrapConfig,
    paths: &SemanticHomePaths,
    existing_manifest: Option<&RuntimeManifest>,
) -> VaultResult<PathBuf> {
    if let Some(path) = config.daemon_path_override.as_ref() {
        return Ok(path.clone());
    }

    if let Some(manifest) = existing_manifest {
        let manifest_path = PathBuf::from(&manifest.binary_path);
        if !manifest_path.as_os_str().is_empty() {
            return Ok(manifest_path);
        }
    }

    Ok(paths.daemon_binary_path.clone())
}

fn find_daemon_on_path() -> Option<PathBuf> {
    let binary_name = home::daemon_binary_name();
    let path_var = std::env::var_os("PATH")?;
    std::env::split_paths(&path_var)
        .map(|dir| dir.join(binary_name))
        .find(|candidate| candidate.is_file())
}

fn resolve_download_url(config: &BootstrapConfig) -> VaultResult<String> {
    if let Some(url) = config.download_url_override.as_ref()
        && !url.trim().is_empty()
    {
        return Ok(url.trim().to_string());
    }

    let version = env!("CARGO_PKG_VERSION");
    let tag = format!("v{version}");
    let target = target_triple()?;
    let asset = if cfg!(windows) {
        format!("obsidian-semanticd-{version}-{target}.zip")
    } else {
        format!("obsidian-semanticd-{version}-{target}.tar.gz")
    };
    Ok(format!("{DEFAULT_DOWNLOAD_BASE_URL}/{tag}/{asset}"))
}

fn target_triple() -> VaultResult<&'static str> {
    if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
        Ok("x86_64-unknown-linux-gnu")
    } else if cfg!(all(target_os = "linux", target_arch = "aarch64")) {
        Ok("aarch64-unknown-linux-gnu")
    } else if cfg!(all(target_os = "macos", target_arch = "x86_64")) {
        Ok("x86_64-apple-darwin")
    } else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
        Ok("aarch64-apple-darwin")
    } else if cfg!(all(target_os = "windows", target_arch = "x86_64")) {
        Ok("x86_64-pc-windows-msvc")
    } else {
        Err(VaultError::DaemonBootstrap(
            "unsupported target for daemon auto-download".to_string(),
        ))
    }
}

async fn download_and_install(url: &str, destination: &Path) -> VaultResult<String> {
    let response = reqwest::get(url).await.map_err(|err| {
        VaultError::DaemonBootstrap(format!("failed to download daemon binary '{url}': {err}"))
    })?;
    if !response.status().is_success() {
        return Err(VaultError::DaemonBootstrap(format!(
            "failed to download daemon binary '{url}': HTTP {}",
            response.status()
        )));
    }

    let bytes = response.bytes().await.map_err(|err| {
        VaultError::DaemonBootstrap(format!("failed to read daemon download bytes: {err}"))
    })?;

    if let Some(parent) = destination.parent() {
        std::fs::create_dir_all(parent)?;
    }

    let checksum = home::sha256_hex(&bytes);
    if url.ends_with(".zip") {
        extract_zip_binary(bytes.as_ref(), destination)?;
    } else if url.ends_with(".tar.gz") {
        extract_tar_gz_binary(bytes.as_ref(), destination)?;
    } else {
        std::fs::write(destination, bytes.as_ref())?;
    }

    #[cfg(unix)]
    make_executable(destination)?;

    Ok(checksum)
}

fn extract_tar_gz_binary(archive_bytes: &[u8], destination: &Path) -> VaultResult<()> {
    let cursor = std::io::Cursor::new(archive_bytes);
    let decoder = flate2::read::GzDecoder::new(cursor);
    let mut archive = tar::Archive::new(decoder);
    let wanted_name = home::daemon_binary_name();
    let mut found = false;

    for entry in archive.entries().map_err(|err| {
        VaultError::DaemonBootstrap(format!("failed to read tar.gz entries: {err}"))
    })? {
        let mut entry = entry.map_err(|err| {
            VaultError::DaemonBootstrap(format!("failed to read tar.gz entry: {err}"))
        })?;
        let path = entry.path().map_err(|err| {
            VaultError::DaemonBootstrap(format!("failed to inspect tar.gz path: {err}"))
        })?;
        let Some(file_name) = path.file_name().and_then(|name| name.to_str()) else {
            continue;
        };
        if file_name == wanted_name {
            if !entry.header().entry_type().is_file() {
                return Err(VaultError::DaemonBootstrap(format!(
                    "tar entry '{}' is not a regular file (type: {:?}); refusing to extract",
                    file_name,
                    entry.header().entry_type()
                )));
            }
            entry.unpack(destination).map_err(|err| {
                VaultError::DaemonBootstrap(format!(
                    "failed to unpack daemon binary to '{}': {err}",
                    destination.display()
                ))
            })?;
            found = true;
            break;
        }
    }

    if !found {
        return Err(VaultError::DaemonBootstrap(format!(
            "downloaded tar.gz archive does not contain '{}'",
            wanted_name
        )));
    }
    Ok(())
}

fn extract_zip_binary(archive_bytes: &[u8], destination: &Path) -> VaultResult<()> {
    let cursor = std::io::Cursor::new(archive_bytes);
    let mut archive = zip::ZipArchive::new(cursor)
        .map_err(|err| VaultError::DaemonBootstrap(format!("failed to open zip archive: {err}")))?;

    let wanted_name = home::daemon_binary_name();
    let mut found = false;
    for idx in 0..archive.len() {
        let mut file = archive.by_index(idx).map_err(|err| {
            VaultError::DaemonBootstrap(format!("failed to read zip entry: {err}"))
        })?;
        let Some(file_name) = std::path::Path::new(file.name())
            .file_name()
            .and_then(|name| name.to_str())
        else {
            continue;
        };
        if file_name == wanted_name {
            if file.is_dir() {
                return Err(VaultError::DaemonBootstrap(format!(
                    "zip entry '{}' is a directory; refusing to extract",
                    file.name()
                )));
            }
            if file.name().contains("..") {
                return Err(VaultError::DaemonBootstrap(format!(
                    "zip entry '{}' contains path traversal sequence; refusing to extract",
                    file.name()
                )));
            }
            let mut out = std::fs::File::create(destination)?;
            std::io::copy(&mut file, &mut out).map_err(|err| {
                VaultError::DaemonBootstrap(format!(
                    "failed to extract daemon binary to '{}': {err}",
                    destination.display()
                ))
            })?;
            found = true;
            break;
        }
    }

    if !found {
        return Err(VaultError::DaemonBootstrap(format!(
            "downloaded zip archive does not contain '{}'",
            wanted_name
        )));
    }
    Ok(())
}

#[cfg(unix)]
fn make_executable(path: &Path) -> VaultResult<()> {
    use std::os::unix::fs::PermissionsExt;

    let mut permissions = std::fs::metadata(path)?.permissions();
    permissions.set_mode(0o755);
    std::fs::set_permissions(path, permissions)?;
    Ok(())
}

fn start_daemon_process(
    binary_path: &Path,
    paths: &SemanticHomePaths,
    endpoint: &IpcEndpoint,
    model_name: &str,
) -> VaultResult<tokio::process::Child> {
    let stderr_log = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&paths.daemon_stderr_log_path)
        .map_err(|err| {
            VaultError::DaemonBootstrap(format!(
                "failed to open daemon stderr log file '{}': {err}",
                paths.daemon_stderr_log_path.display()
            ))
        })?;

    let mut command = tokio::process::Command::new(binary_path);
    command
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::from(stderr_log))
        .env("OBSIDIAN_SEMANTIC_HOME", &paths.root)
        .env("OBSIDIAN_SEMANTIC_ENDPOINT", endpoint.endpoint_string())
        .env("OBSIDIAN_SEMANTIC_MODEL", model_name)
        .env("FASTEMBED_CACHE_DIR", &paths.fastembed_cache_dir);

    if let Ok(log_level) = std::env::var("OBSIDIAN_LOG_LEVEL") {
        command.env("OBSIDIAN_LOG_LEVEL", log_level);
    }

    command.spawn().map_err(|err| {
        VaultError::DaemonBootstrap(format!(
            "failed to spawn semantic daemon '{}': {err}",
            binary_path.display()
        ))
    })
}

async fn wait_for_health(
    endpoint: &IpcEndpoint,
    timeout: Duration,
) -> VaultResult<protocol::HealthResult> {
    let started = tokio::time::Instant::now();
    loop {
        match probe_health(endpoint).await? {
            HealthProbeOutcome::Healthy(health) => return Ok(health),
            HealthProbeOutcome::Incompatible(message) => {
                return Err(VaultError::DaemonBootstrap(format!(
                    "daemon API incompatibility detected: {message}"
                )));
            }
            HealthProbeOutcome::Invalid(message) => {
                return Err(VaultError::DaemonBootstrap(format!(
                    "daemon health probe returned invalid response: {message}"
                )));
            }
            HealthProbeOutcome::Unreachable => {}
        }

        if started.elapsed() >= timeout {
            return Err(VaultError::DaemonBootstrap(format!(
                "timed out waiting for daemon health on endpoint '{}'",
                endpoint.endpoint_string()
            )));
        }
        tokio::time::sleep(Duration::from_millis(200)).await;
    }
}

#[cfg(unix)]
async fn probe_health(endpoint: &IpcEndpoint) -> VaultResult<HealthProbeOutcome> {
    let IpcEndpoint::UnixSocket(path) = endpoint;
    let stream = match UnixStream::connect(path).await {
        Ok(stream) => stream,
        Err(err)
            if matches!(
                err.kind(),
                std::io::ErrorKind::NotFound
                    | std::io::ErrorKind::ConnectionRefused
                    | std::io::ErrorKind::ConnectionReset
            ) =>
        {
            return Ok(HealthProbeOutcome::Unreachable);
        }
        Err(err) => {
            return Err(VaultError::DaemonBootstrap(format!(
                "failed to connect to daemon endpoint '{}': {err}",
                path.display()
            )));
        }
    };

    request_health(stream).await
}

#[cfg(windows)]
async fn probe_health(endpoint: &IpcEndpoint) -> VaultResult<HealthProbeOutcome> {
    let IpcEndpoint::NamedPipe(name) = endpoint;
    let stream = match ClientOptions::new().open(name) {
        Ok(stream) => stream,
        Err(err)
            if err.kind() == std::io::ErrorKind::NotFound
                || err.kind() == std::io::ErrorKind::ConnectionRefused
                || err.raw_os_error() == Some(231) =>
        {
            return Ok(HealthProbeOutcome::Unreachable);
        }
        Err(err) => {
            return Err(VaultError::DaemonBootstrap(format!(
                "failed to connect to daemon named pipe '{}': {err}",
                name
            )));
        }
    };

    request_health(stream).await
}

#[cfg(not(any(unix, windows)))]
async fn probe_health(_endpoint: &IpcEndpoint) -> VaultResult<HealthProbeOutcome> {
    Ok(HealthProbeOutcome::Unreachable)
}

async fn request_health<S>(mut stream: S) -> VaultResult<HealthProbeOutcome>
where
    S: AsyncRead + AsyncWrite + Unpin,
{
    let request = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "health",
        "params": {
            "client_name": "obsidian-mcp",
            "client_version": env!("CARGO_PKG_VERSION"),
            "min_api_version": DAEMON_API_VERSION,
            "max_api_version": DAEMON_API_VERSION
        }
    });
    let request_str = serde_json::to_string(&request).map_err(|err| {
        VaultError::DaemonBootstrap(format!("failed to serialize daemon health request: {err}"))
    })?;

    stream
        .write_all(request_str.as_bytes())
        .await
        .map_err(|err| {
            VaultError::DaemonBootstrap(format!("failed to write daemon health request: {err}"))
        })?;
    stream.write_all(b"\n").await.map_err(|err| {
        VaultError::DaemonBootstrap(format!("failed to finish daemon health request: {err}"))
    })?;
    stream.flush().await.map_err(|err| {
        VaultError::DaemonBootstrap(format!("failed to flush daemon health request: {err}"))
    })?;

    let mut reader = BufReader::new(stream);
    let mut line = String::new();
    let read = reader.read_line(&mut line).await.map_err(|err| {
        VaultError::DaemonBootstrap(format!("failed to read daemon health response: {err}"))
    })?;
    if read == 0 {
        return Ok(HealthProbeOutcome::Unreachable);
    }

    let response: serde_json::Value = serde_json::from_str(&line).map_err(|err| {
        VaultError::DaemonBootstrap(format!(
            "failed to parse daemon health response JSON: {err}"
        ))
    })?;

    if let Some(error) = response.get("error") {
        let code = error
            .get("code")
            .and_then(serde_json::Value::as_i64)
            .unwrap_or_default();
        let message = error
            .get("message")
            .and_then(serde_json::Value::as_str)
            .unwrap_or("daemon health error")
            .to_string();
        if code == ERR_INCOMPATIBLE_API_VERSION {
            return Ok(HealthProbeOutcome::Incompatible(message));
        }
        return Ok(HealthProbeOutcome::Invalid(message));
    }

    let Some(result) = response.get("result") else {
        return Ok(HealthProbeOutcome::Invalid(
            "missing result in daemon health response".to_string(),
        ));
    };
    let health =
        serde_json::from_value::<protocol::HealthResult>(result.clone()).map_err(|err| {
            VaultError::DaemonBootstrap(format!("failed to decode daemon health result: {err}"))
        })?;
    Ok(HealthProbeOutcome::Healthy(health))
}

fn endpoint_transport(endpoint: &IpcEndpoint) -> &'static str {
    match endpoint {
        #[cfg(unix)]
        IpcEndpoint::UnixSocket(_) => "unix_socket",
        #[cfg(windows)]
        IpcEndpoint::NamedPipe(_) => "named_pipe",
    }
}

fn endpoint_from_manifest(manifest: &RuntimeManifest) -> Option<IpcEndpoint> {
    if manifest.ipc.transport == "unix_socket" {
        #[cfg(unix)]
        {
            return Some(IpcEndpoint::UnixSocket(PathBuf::from(
                &manifest.ipc.endpoint,
            )));
        }
    }

    if manifest.ipc.transport == "named_pipe" {
        #[cfg(windows)]
        {
            return Some(IpcEndpoint::NamedPipe(manifest.ipc.endpoint.clone()));
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::daemon::server;

    #[test]
    fn resolve_download_url_uses_override() {
        let config = BootstrapConfig {
            download_url_override: Some("https://example.com/custom.tar.gz".to_string()),
            ..Default::default()
        };
        let url = resolve_download_url(&config).expect("download URL should resolve");
        assert_eq!(url, "https://example.com/custom.tar.gz");
    }

    #[test]
    fn resolve_download_url_uses_versioned_semanticd_asset_name() {
        let url = resolve_download_url(&BootstrapConfig::default()).expect("resolve default URL");
        let version = env!("CARGO_PKG_VERSION");
        assert!(
            url.contains(&format!("/releases/download/v{version}/")),
            "url should target release tag path, got: {url}"
        );
        assert!(
            url.contains(&format!("obsidian-semanticd-{version}-")),
            "url should include versioned semantic daemon asset, got: {url}"
        );
        if cfg!(windows) {
            assert!(url.ends_with(".zip"), "windows URL should end with .zip");
        } else {
            assert!(url.ends_with(".tar.gz"), "unix URL should end with .tar.gz");
        }
    }

    #[test]
    fn endpoint_from_manifest_rejects_unknown_transport() {
        let manifest = RuntimeManifest::from_input(RuntimeManifestInput {
            daemon_api_version: 1,
            daemon_version: "1.0.1".to_string(),
            binary_path: "/tmp/semanticd".to_string(),
            binary_sha256: None,
            ipc: ManifestIpc {
                transport: "tcp".to_string(),
                endpoint: "127.0.0.1:1234".to_string(),
            },
            pid: 10,
            semantic_home: "/tmp/home".to_string(),
            fastembed_cache_dir: "/tmp/home/model/fastembed-cache".to_string(),
            model_name: "BAAI/bge-small-en-v1.5".to_string(),
            bootstrap_client_name: "obsidian-mcp".to_string(),
            bootstrap_client_version: "1.0.1".to_string(),
        });
        assert!(endpoint_from_manifest(&manifest).is_none());
    }

    #[test]
    fn find_daemon_on_path_discovers_binary_in_temp_dir() {
        let dir = tempfile::tempdir().expect("tempdir");
        let binary_name = home::daemon_binary_name();
        let fake_binary = dir.path().join(binary_name);
        std::fs::write(&fake_binary, b"fake").expect("write fake binary");
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = std::fs::metadata(&fake_binary).unwrap().permissions();
            perms.set_mode(0o755);
            std::fs::set_permissions(&fake_binary, perms).unwrap();
        }

        let original_path = std::env::var_os("PATH").unwrap_or_default();
        let mut new_path = std::env::split_paths(&original_path).collect::<Vec<_>>();
        new_path.insert(0, dir.path().to_path_buf());
        let joined = std::env::join_paths(&new_path).expect("join paths");
        // SAFETY: test-only; mutating env is acceptable in single-threaded test context
        unsafe { std::env::set_var("PATH", &joined) };

        let result = find_daemon_on_path();
        unsafe { std::env::set_var("PATH", &original_path) };

        assert_eq!(result, Some(fake_binary));
    }

    #[test]
    fn find_daemon_on_path_returns_none_when_absent() {
        let dir = tempfile::tempdir().expect("tempdir");
        let original_path = std::env::var_os("PATH").unwrap_or_default();
        // SAFETY: test-only; mutating env is acceptable in single-threaded test context
        unsafe { std::env::set_var("PATH", dir.path()) };

        let result = find_daemon_on_path();
        unsafe { std::env::set_var("PATH", &original_path) };

        assert!(result.is_none());
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn ensure_daemon_reuses_healthy_manifest_endpoint() {
        let dir = tempfile::tempdir().expect("tempdir should be created");
        let paths = SemanticHomePaths::new(dir.path().join("semantic-home"));
        home::ensure_home_layout(&paths).expect("home layout should be created");
        let endpoint = IpcEndpoint::UnixSocket(paths.ipc_dir.join("semanticd.sock"));
        let daemon_config = server::DaemonServerConfig {
            endpoint: endpoint.clone(),
            model_name: "BAAI/bge-small-en-v1.5".to_string(),
            semantic_home: paths.root.clone(),
        };

        let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel::<()>();
        let server_task = tokio::spawn(async move {
            server::run_with_shutdown(daemon_config, async move {
                let _ = shutdown_rx.await;
            })
            .await
            .expect("daemon server should run");
        });

        let mut ready = false;
        let IpcEndpoint::UnixSocket(socket_path) = &endpoint;
        for _ in 0..50 {
            if UnixStream::connect(socket_path).await.is_ok() {
                ready = true;
                break;
            }
            tokio::time::sleep(Duration::from_millis(25)).await;
        }
        assert!(ready, "daemon endpoint did not become ready");

        let manifest = RuntimeManifest::from_input(RuntimeManifestInput {
            daemon_api_version: DAEMON_API_VERSION,
            daemon_version: env!("CARGO_PKG_VERSION").to_string(),
            binary_path: "/tmp/nonexistent-semanticd".to_string(),
            binary_sha256: None,
            ipc: ManifestIpc {
                transport: "unix_socket".to_string(),
                endpoint: endpoint.endpoint_string(),
            },
            pid: 4242,
            semantic_home: paths.root.display().to_string(),
            fastembed_cache_dir: paths.fastembed_cache_dir.display().to_string(),
            model_name: "BAAI/bge-small-en-v1.5".to_string(),
            bootstrap_client_name: "obsidian-mcp".to_string(),
            bootstrap_client_version: "1.0.1".to_string(),
        });
        manifest::save_atomic(&paths.manifest_path, &manifest).expect("manifest should persist");

        let config = BootstrapConfig {
            semantic_home_override: Some(paths.root.clone()),
            daemon_path_override: None,
            model_name: "BAAI/bge-small-en-v1.5".to_string(),
            download_url_override: None,
            bootstrap_client_name: "obsidian-mcp".to_string(),
            bootstrap_client_version: "1.0.1".to_string(),
        };
        let result = ensure_daemon(&config)
            .await
            .expect("bootstrap should reuse daemon");
        assert!(result.reused_existing, "expected reuse-existing path");

        shutdown_tx.send(()).expect("shutdown signal should send");
        server_task.await.expect("server task should join");
    }
}