dbmd-core 0.13.1

Reference library for db.md, the open standard for databases in plain files. Parsing, store walk, wiki-link graph, validation, query, and write-through indexes. Zero AI dependencies.
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
// SPDX-License-Identifier: Apache-2.0

//! Provider resolution for the embedded harness — flag > env > store-local
//! `.dbmd/config` > local autodetect, mirroring the hub client's
//! `hub_config` discipline exactly, including its central security rule:
//!
//! **The credential is environment-only** (`DBMD_LLM_KEY`, or a preset's
//! conventional key variable), never a file inside the store — a secret in
//! the store tree is one sync away from leaking. And when the *endpoint*
//! came from store-local config (untrusted: it syncs with the store), an
//! ambient key is refused unless `DBMD_LLM_KEY_ORIGIN` explicitly binds it
//! to that exact origin — otherwise a cloned store could point the harness
//! at an attacker endpoint and harvest the user's key.
//!
//! There is no default vendor: presets are inert rows (base URL + protocol +
//! conventional key variable), and with nothing configured the resolver
//! probes the well-known LOCAL servers only (Ollama, LM Studio, llama.cpp) —
//! zero-config never means "silently pick a cloud".

use std::path::Path;
use std::time::Duration;

use super::{Effort, EffortDialect, HarnessError, Protocol, Provider};

/// Env var: explicit provider preset name (or a delegation backend).
pub const PROVIDER_ENV: &str = "DBMD_LLM_PROVIDER";
/// Env var: endpoint base URL.
pub const BASE_URL_ENV: &str = "DBMD_LLM_BASE_URL";
/// Env var: wire protocol (`openai` | `anthropic`).
pub const PROTOCOL_ENV: &str = "DBMD_LLM_PROTOCOL";
/// Env var: model id.
pub const MODEL_ENV: &str = "DBMD_LLM_MODEL";

/// Environment override for the reasoning effort (`off`…`max`).
pub const EFFORT_ENV: &str = "DBMD_LLM_EFFORT";
/// Env var: the credential. The ONLY file-independent key source; wins over
/// preset key variables.
pub const KEY_ENV: &str = "DBMD_LLM_KEY";
/// Env var: origin binding required before an ambient key may be sent to an
/// endpoint selected by store-local config (e.g. `https://llm.example.com`).
pub const KEY_ORIGIN_ENV: &str = "DBMD_LLM_KEY_ORIGIN";
/// Env var: allow plain http to a NON-loopback endpoint (LAN Ollama etc.).
pub const ALLOW_INSECURE_ENV: &str = "DBMD_LLM_ALLOW_INSECURE_HTTP";

/// `.dbmd/config` keys (non-secret knobs only — a key line in this file is
/// never read).
const CONFIG_KEYS: [&str; 5] = [
    "llm_provider",
    "llm_base_url",
    "llm_protocol",
    "llm_model",
    "llm_effort",
];

/// A store may also pin a value per provider (`llm_model_ollama`,
/// `llm_model_codex`), which survives an explicit `--provider` override.
fn is_config_key(key: &str) -> bool {
    CONFIG_KEYS.contains(&key)
        || CONFIG_KEYS
            .iter()
            .any(|base| key.starts_with(&format!("{base}_")))
}

/// Flag-level overrides from the CLI (all optional).
#[derive(Debug, Default, Clone)]
pub struct Overrides {
    /// `--provider` — preset name or delegation backend.
    pub provider: Option<String>,
    /// `--base-url`.
    pub base_url: Option<String>,
    /// `--protocol`.
    pub protocol: Option<String>,
    /// `--model`.
    pub model: Option<String>,
    /// `--effort`.
    pub effort: Option<String>,
}

/// One inert preset row: a name, the protocol it speaks, its base URL, and
/// the conventional key variable users of that provider already export.
#[derive(Debug)]
struct Preset {
    name: &'static str,
    protocol: Protocol,
    base_url: &'static str,
    key_env: Option<&'static str>,
}

/// The preset table. Data, not code: every row rides one of the two wire
/// adapters (or a delegation backend). No row is a default.
const PRESETS: [Preset; 13] = [
    Preset {
        name: "anthropic",
        protocol: Protocol::Anthropic,
        base_url: "https://api.anthropic.com",
        key_env: Some("ANTHROPIC_API_KEY"),
    },
    Preset {
        name: "openai",
        protocol: Protocol::OpenAi,
        base_url: "https://api.openai.com/v1",
        key_env: Some("OPENAI_API_KEY"),
    },
    Preset {
        name: "openrouter",
        protocol: Protocol::OpenAi,
        base_url: "https://openrouter.ai/api/v1",
        key_env: Some("OPENROUTER_API_KEY"),
    },
    Preset {
        name: "groq",
        protocol: Protocol::OpenAi,
        base_url: "https://api.groq.com/openai/v1",
        key_env: Some("GROQ_API_KEY"),
    },
    Preset {
        name: "together",
        protocol: Protocol::OpenAi,
        base_url: "https://api.together.xyz/v1",
        key_env: Some("TOGETHER_API_KEY"),
    },
    Preset {
        name: "deepseek",
        protocol: Protocol::OpenAi,
        base_url: "https://api.deepseek.com/v1",
        key_env: Some("DEEPSEEK_API_KEY"),
    },
    Preset {
        name: "mistral",
        protocol: Protocol::OpenAi,
        base_url: "https://api.mistral.ai/v1",
        key_env: Some("MISTRAL_API_KEY"),
    },
    Preset {
        name: "ollama",
        protocol: Protocol::OpenAi,
        base_url: "http://127.0.0.1:11434/v1",
        key_env: None,
    },
    Preset {
        name: "lmstudio",
        protocol: Protocol::OpenAi,
        base_url: "http://127.0.0.1:1234/v1",
        key_env: None,
    },
    Preset {
        name: "llamacpp",
        protocol: Protocol::OpenAi,
        base_url: "http://127.0.0.1:8080/v1",
        key_env: None,
    },
    Preset {
        name: "codex",
        protocol: Protocol::Codex,
        base_url: super::codex::DEFAULT_BASE_URL,
        key_env: None,
    },
    Preset {
        name: "claude-code",
        protocol: Protocol::ClaudeCli,
        base_url: "",
        key_env: None,
    },
    Preset {
        name: "codex-cli",
        protocol: Protocol::CodexCli,
        base_url: "",
        key_env: None,
    },
];

/// The default model for a ChatGPT-subscription session when the user names
/// none. ChatGPT accounts accept only the models their plan exposes through
/// Codex, and that set moves: a rejected name comes back as a plain HTTP 400
/// naming the model, and `--model` (or `llm_model_codex` in `.dbmd/config`)
/// picks another without a new release.
pub const CODEX_DEFAULT_MODEL: &str = "gpt-5.6-sol";

/// Where a resolved value came from — the origin-binding rule keys on this.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Source {
    Flag,
    Env,
    StoreConfig,
    Preset,
    Autodetect,
}

fn env_nonempty(name: &str) -> Option<String> {
    std::env::var(name).ok().filter(|v| !v.trim().is_empty())
}

/// Parse the harness keys out of `.dbmd/config` (`key = value` lines, `#`
/// comments). Unknown keys are ignored; a `llm_key`-looking line is
/// deliberately NOT a key source.
fn store_config(store_root: &Path) -> Vec<(String, String)> {
    let path = store_root.join(".dbmd").join("config");
    let Ok(text) = std::fs::read_to_string(path) else {
        return Vec::new();
    };
    let mut out = Vec::new();
    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        if let Some((key, value)) = line.split_once('=') {
            let key = key.trim();
            let value = value.trim();
            if is_config_key(key) && !value.is_empty() {
                out.push((key.to_string(), value.to_string()));
            }
        }
    }
    out
}

fn config_value(config: &[(String, String)], key: &str) -> Option<String> {
    config
        .iter()
        .find(|(k, _)| k == key)
        .map(|(_, v)| v.clone())
}

fn parse_protocol(raw: &str) -> Result<Protocol, HarnessError> {
    match raw {
        "openai" | "openai-completions" => Ok(Protocol::OpenAi),
        "anthropic" | "anthropic-messages" => Ok(Protocol::Anthropic),
        "codex" | "codex-responses" => Ok(Protocol::Codex),
        other => Err(HarnessError::Config(format!(
            "unknown protocol `{other}` — use `openai`, `anthropic`, or `codex`"
        ))),
    }
}

fn find_preset(name: &str) -> Result<&'static Preset, HarnessError> {
    PRESETS.iter().find(|p| p.name == name).ok_or_else(|| {
        let names: Vec<&str> = PRESETS.iter().map(|p| p.name).collect();
        HarnessError::Config(format!(
            "unknown provider `{name}` — known presets: {}",
            names.join(", ")
        ))
    })
}

/// The scheme+authority prefix of a URL, for origin binding and loopback
/// checks. Not a general URL parser — enough for `http(s)://host[:port]`.
fn origin_of(url: &str) -> String {
    let after_scheme = url.find("://").map(|i| i + 3).unwrap_or(0);
    let end = url[after_scheme..]
        .find('/')
        .map(|i| after_scheme + i)
        .unwrap_or(url.len());
    url[..end].trim_end_matches('/').to_string()
}

fn is_loopback_url(url: &str) -> bool {
    let origin = origin_of(url);
    let host_port = origin.split("://").nth(1).unwrap_or(&origin);
    let host = host_port
        .rsplit_once(':')
        .map(|(h, _)| h)
        .unwrap_or(host_port);
    matches!(
        host,
        "127.0.0.1" | "localhost" | "[::1]" | "::1" | "0.0.0.0"
    )
}

/// Resolve the provider for a run. `store_root` supplies `.dbmd/config`.
pub fn resolve(store_root: &Path, overrides: &Overrides) -> Result<Provider, HarnessError> {
    let config = store_config(store_root);

    // ── provider preset ──────────────────────────────────────────────────
    // A provider named by flag or env OVERRIDES the store's configured setup.
    // The store's other `llm_*` values were written for whatever provider that
    // store expects (a local model name, say), so they must not silently ride
    // to a different one — a ChatGPT account rejecting `qwen3.8-27b-32k` is the
    // friendly version of that mistake; a wrong endpoint would be the ugly one.
    // A provider-scoped key (`llm_model_codex`) is honored either way.
    let overriding_provider = overrides
        .provider
        .clone()
        .or_else(|| env_nonempty(PROVIDER_ENV));
    let provider_is_overridden = overriding_provider.is_some();
    let preset_name = overriding_provider.or_else(|| config_value(&config, "llm_provider"));
    let preset: Option<&Preset> = match &preset_name {
        Some(name) => Some(find_preset(name)?),
        None => None,
    };
    // Store-local values that still apply after an explicit override: only the
    // ones scoped to the provider actually in use.
    let scoped = |config: &[(String, String)], key: &str| -> Option<String> {
        let name = preset_name.as_deref()?;
        config_value(config, &format!("{key}_{name}"))
    };
    let store_value = |config: &[(String, String)], key: &str| -> Option<String> {
        scoped(config, key).or_else(|| {
            if provider_is_overridden {
                None
            } else {
                config_value(config, key)
            }
        })
    };

    if let Some(preset) = preset {
        if preset.protocol.is_delegate() {
            return Ok(Provider {
                protocol: preset.protocol,
                base_url: String::new(),
                model: String::new(),
                key: None,
                oauth: false,
                source: format!("delegation to the `{}` CLI", preset.name),
                effort_dialect: EffortDialect::Standard,
            });
        }
        // The ChatGPT backend takes a subscription token from `dbmd login
        // codex`, never an API key: the credential comes from the toolkit
        // state dir (refreshed in place), not from the environment.
        if preset.protocol == Protocol::Codex {
            let access = super::auth::access_token("codex")?.ok_or_else(|| {
                HarnessError::Config(
                    "not signed in to ChatGPT — run `dbmd login codex` to use your \
                     subscription (or `--provider codex-cli` to delegate to an \
                     installed, logged-in codex CLI)"
                        .to_string(),
                )
            })?;
            let plan = super::oauth::plan_type(&access)
                .map(|plan| format!(" ({plan})"))
                .unwrap_or_default();
            return Ok(Provider {
                protocol: Protocol::Codex,
                base_url: overrides
                    .base_url
                    .clone()
                    .or_else(|| env_nonempty(BASE_URL_ENV))
                    .unwrap_or_else(|| preset.base_url.to_string()),
                model: overrides
                    .model
                    .clone()
                    .or_else(|| env_nonempty(MODEL_ENV))
                    .or_else(|| store_value(&config, "llm_model"))
                    .unwrap_or_else(|| CODEX_DEFAULT_MODEL.to_string()),
                key: Some(access),
                oauth: true,
                source: format!("ChatGPT subscription{plan}"),
                effort_dialect: EffortDialect::Standard,
            });
        }
    }

    // ── base URL (with provenance) ───────────────────────────────────────
    let (base_url, url_source) = if let Some(url) = overrides.base_url.clone() {
        (Some(url), Source::Flag)
    } else if let Some(url) = env_nonempty(BASE_URL_ENV) {
        (Some(url), Source::Env)
    } else if let Some(url) = store_value(&config, "llm_base_url") {
        (Some(url), Source::StoreConfig)
    } else if let Some(preset) = preset {
        (Some(preset.base_url.to_string()), Source::Preset)
    } else {
        (None, Source::Autodetect)
    };

    // ── protocol ─────────────────────────────────────────────────────────
    let protocol = if let Some(raw) = overrides
        .protocol
        .clone()
        .or_else(|| env_nonempty(PROTOCOL_ENV))
        .or_else(|| store_value(&config, "llm_protocol"))
    {
        Some(parse_protocol(&raw)?)
    } else {
        preset.map(|p| p.protocol)
    };

    // ── model ────────────────────────────────────────────────────────────
    let model = overrides
        .model
        .clone()
        .or_else(|| env_nonempty(MODEL_ENV))
        .or_else(|| store_value(&config, "llm_model"));

    // ── key: ENVIRONMENT ONLY ────────────────────────────────────────────
    let key =
        env_nonempty(KEY_ENV).or_else(|| preset.and_then(|p| p.key_env).and_then(env_nonempty));
    // An Anthropic endpoint with no API key in the environment falls back to
    // the user's `ant auth login` session — Anthropic's own published handoff
    // for third-party HTTP clients. An explicit key still wins, matching the
    // vendor's documented precedence (and avoiding the trap where both an API
    // key and a bearer token are sent and the API rejects the request).
    let mut oauth = false;
    let key = match key {
        Some(key) => Some(key),
        None if protocol == Some(Protocol::Anthropic) => match super::ant::access_token()? {
            Some(token) => {
                oauth = true;
                Some(token)
            }
            None => None,
        },
        None => None,
    };

    let (base_url, protocol, model, source) = match base_url {
        Some(url) => {
            let url = url.trim_end_matches('/').to_string();
            let protocol = protocol.ok_or_else(|| {
                HarnessError::Config(
                    "an endpoint is configured but its protocol is not — set \
                     `--protocol openai|anthropic` (or DBMD_LLM_PROTOCOL / \
                     `llm_protocol` in .dbmd/config)"
                        .to_string(),
                )
            })?;
            let model = model.ok_or_else(|| {
                HarnessError::Config(
                    "no model configured — set `--model` (or DBMD_LLM_MODEL / \
                     `llm_model` in .dbmd/config)"
                        .to_string(),
                )
            })?;
            let source = match url_source {
                Source::Flag => "endpoint from --base-url".to_string(),
                Source::Env => format!("endpoint from {BASE_URL_ENV}"),
                Source::StoreConfig => "endpoint from .dbmd/config".to_string(),
                Source::Preset => format!("preset {}", preset.map(|p| p.name).unwrap_or_default()),
                Source::Autodetect => unreachable!("Some(url) never carries Autodetect"),
            };
            (url, protocol, model, source)
        }
        None => {
            let found = autodetect(model.as_deref())?;
            (found.base_url, Protocol::OpenAi, found.model, found.source)
        }
    };

    // ── the origin-binding rule ──────────────────────────────────────────
    // An endpoint selected by store-local config is untrusted (it syncs with
    // the store); refusing to pair it with an ambient key unless the user
    // explicitly bound that key to this exact origin closes the cloned-store
    // key-exfiltration hole. Mirrors the hub client's UnboundCredential rule.
    if key.is_some() && url_source == Source::StoreConfig {
        let origin = origin_of(&base_url);
        let bound = env_nonempty(KEY_ORIGIN_ENV);
        if bound.as_deref().map(|b| b.trim_end_matches('/')) != Some(origin.as_str()) {
            return Err(HarnessError::Config(format!(
                "refusing to send a credential to `{origin}`: the endpoint came \
                 from store-local .dbmd/config (which syncs with the store), so \
                 the ambient key must be explicitly bound — set \
                 {KEY_ORIGIN_ENV}={origin} to allow it"
            )));
        }
    }

    // ── scheme discipline ────────────────────────────────────────────────
    if base_url.starts_with("http://")
        && !is_loopback_url(&base_url)
        && env_nonempty(ALLOW_INSECURE_ENV).as_deref() != Some("1")
    {
        return Err(HarnessError::Config(format!(
            "refusing plain http to a non-loopback endpoint ({}) — use https, \
             or set {ALLOW_INSECURE_ENV}=1 for a trusted LAN server",
            origin_of(&base_url)
        )));
    }
    if !base_url.starts_with("http://") && !base_url.starts_with("https://") {
        return Err(HarnessError::Config(format!(
            "endpoint must be an http(s) URL, got `{base_url}`"
        )));
    }

    let effort_dialect = effort_dialect_for(preset_name.as_deref(), &base_url);

    let source = if oauth {
        format!("{source}, Anthropic OAuth via `ant`")
    } else {
        source
    };

    Ok(Provider {
        protocol,
        base_url,
        model,
        key,
        oauth,
        source,
        effort_dialect,
    })
}

/// Resolve the reasoning effort for a run.
///
/// Same precedence chain as every other knob — flag, then environment, then
/// `.dbmd/config` (a provider-scoped `llm_effort_codex` first, then a plain
/// `llm_effort` when no provider was explicitly overridden). `None` means the
/// caller sends no reasoning field at all and the provider keeps its own
/// default.
pub fn resolve_effort(
    store_root: &Path,
    overrides: &Overrides,
) -> Result<Option<Effort>, HarnessError> {
    if let Some(raw) = &overrides.effort {
        return Effort::parse(raw).map(Some);
    }
    if let Some(raw) = env_nonempty(EFFORT_ENV) {
        return Effort::parse(&raw).map(Some);
    }
    let config = store_config(store_root);
    let overriding_provider = overrides
        .provider
        .clone()
        .or_else(|| env_nonempty(PROVIDER_ENV));
    let provider_is_overridden = overriding_provider.is_some();
    let preset_name = overriding_provider.or_else(|| config_value(&config, "llm_provider"));
    if let Some(name) = preset_name.as_deref() {
        if let Some(raw) = config_value(&config, &format!("llm_effort_{name}")) {
            return Effort::parse(&raw).map(Some);
        }
    }
    if provider_is_overridden {
        return Ok(None);
    }
    match config_value(&config, "llm_effort") {
        Some(raw) => Effort::parse(&raw).map(Some),
        None => Ok(None),
    }
}

/// Which `reasoning_effort` vocabulary an endpoint speaks.
///
/// Ollama is the one compat server that diverges (`none`/`max` instead of
/// `minimal`, and thinking already on by default), and it is identifiable
/// either by preset name or by its well-known port — including when the
/// endpoint arrived through autodetect rather than `--provider ollama`.
/// Everything else gets the standard vocabulary plus the drop-and-retry
/// recovery in the adapter.
fn effort_dialect_for(preset_name: Option<&str>, base_url: &str) -> EffortDialect {
    if preset_name == Some("ollama") || origin_of(base_url).contains(":11434") {
        EffortDialect::Ollama
    } else {
        EffortDialect::Standard
    }
}

struct Detected {
    base_url: String,
    model: String,
    source: String,
}

/// Probe the well-known local servers (Ollama, LM Studio, llama.cpp) with a
/// short connect timeout. `wanted_model` (from flag/env/config) is trusted
/// verbatim when set; otherwise a single available model is chosen and
/// multiple models are an error listing the choices.
fn autodetect(wanted_model: Option<&str>) -> Result<Detected, HarnessError> {
    let agent = ureq::AgentBuilder::new()
        .timeout_connect(Duration::from_millis(400))
        .timeout_read(Duration::from_secs(3))
        .redirects(0)
        .build();

    let candidates: [(&str, &str, &str); 3] = [
        ("ollama", "http://127.0.0.1:11434", "/api/tags"),
        ("lmstudio", "http://127.0.0.1:1234", "/v1/models"),
        ("llama.cpp", "http://127.0.0.1:8080", "/v1/models"),
    ];
    for (name, origin, probe) in candidates {
        let Ok(response) = agent.get(&format!("{origin}{probe}")).call() else {
            continue;
        };
        let Ok(body) = response.into_string() else {
            continue;
        };
        let Ok(json) = serde_json::from_str::<serde_json::Value>(&body) else {
            continue;
        };
        let mut models: Vec<String> = Vec::new();
        if let Some(list) = json.get("models").and_then(|m| m.as_array()) {
            for entry in list {
                if let Some(model) = entry.get("name").and_then(|n| n.as_str()) {
                    models.push(model.to_string());
                }
            }
        }
        if let Some(list) = json.get("data").and_then(|d| d.as_array()) {
            for entry in list {
                if let Some(model) = entry.get("id").and_then(|i| i.as_str()) {
                    models.push(model.to_string());
                }
            }
        }
        let base_url = format!("{origin}/v1");
        let model = match wanted_model {
            Some(model) => model.to_string(),
            None if models.len() == 1 => models.remove(0),
            None if models.is_empty() => {
                return Err(HarnessError::Config(format!(
                    "found a local {name} server at {origin} but it has no \
                     models — pull one, or set `--model`"
                )));
            }
            None => {
                models.truncate(20);
                return Err(HarnessError::Config(format!(
                    "found a local {name} server at {origin} with several \
                     models — pick one with `--model` (or DBMD_LLM_MODEL): {}",
                    models.join(", ")
                )));
            }
        };
        return Ok(Detected {
            base_url,
            model,
            source: format!("autodetected local {name} at {origin}"),
        });
    }
    Err(HarnessError::Config(
        "no model endpoint configured and no local server found — set \
         `--provider <preset>` with its key, `--base-url` + `--protocol` + \
         `--model`, or start a local server (Ollama, LM Studio, llama.cpp)"
            .to_string(),
    ))
}

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

    #[test]
    fn origin_extraction() {
        assert_eq!(
            origin_of("https://api.example.com/v1/messages"),
            "https://api.example.com"
        );
        assert_eq!(origin_of("http://127.0.0.1:8080"), "http://127.0.0.1:8080");
    }

    #[test]
    fn loopback_detection() {
        assert!(is_loopback_url("http://127.0.0.1:11434/v1"));
        assert!(is_loopback_url("http://localhost:1234/v1"));
        assert!(!is_loopback_url("http://192.168.1.20:11434/v1"));
    }

    #[test]
    fn unknown_preset_lists_choices() {
        let error = find_preset("nope").expect_err("unknown");
        assert!(error.to_string().contains("ollama"));
    }
}