BREP_mcp_core 0.11.0

The BREP MCP server core: the Model Context Protocol surface generated from the CAD app's own registries, its sessions, script runner and transports (stdio and streamable HTTP). Embedded in the app behind `brep-app --mcp`; hosted headlessly by BREP_mcp.
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
//! The PLM fixture: a real `brep-plm serve` for one `test-mcp`
//! script.
//!
//! A script that declares `plm` gets, before its session starts:
//!
//! 1. `brep-plm serve --bind 127.0.0.1:0 --data <fresh dir>`, so every
//!    run starts from the server's own first-run seed and nothing leaks between
//!    scripts. The data directory is on tmpfs (`/dev/shm`) where there is one,
//!    and is removed when the fixture drops: the server runs SQLite at
//!    `synchronous=FULL`, and on a disk shared with parallel builds one fsync
//!    measured 1.2–1.7 s, twelve of them made start-up take 13 s, and a run
//!    timed out at 60 s. Durability is worthless to a throwaway fixture.
//!    `BREP_PLM_FIXTURE_DATA` names another parent directory (the run's
//!    `<out>/plm` is used where there is no tmpfs).
//! 2. The first-run administrator password, read from the one place the server
//!    ever shows it (its stdout), then a sign-in, and a token minted through
//!    that session exactly as a person mints one on the web page. Each account
//!    the script names is created the same way and gets its own token.
//! 3. The script's `setup` calls, through the API — never by writing the data
//!    directory, so the fixture can only build states the server would build.
//! 4. D5's two files in the session store (the app's config root):
//!    `plm.json` holding the URL and `plm-token` holding the app user's token
//!    (`app_token: false` writes `plm.json` alone: a machine not signed in).
//!
//! Steps then talk to the server with `plm_request` (handled by the runner, not
//! the app: it is the "second client" every PLM gate needs), and any string in
//! any step may use `${plm:NAME}`:
//!
//! - `${plm:url}` — the server's base URL;
//! - `${plm:token}` — the app user's token, `${plm:token:<user>}` any account's;
//! - any name a `bind` captured from an earlier answer.
//!
//! The server is killed when the fixture drops. Its stdout and stderr go to
//! `<out>/plm/serve.log`.
use crate::script::{PlmCall, PlmSpec};
use serde_json::{json, Value};
use std::collections::BTreeMap;
use std::io::{BufRead, BufReader, Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::{mpsc, Mutex};
use std::time::{Duration, Instant};

/// The password a script's account gets when it names none.
pub const DEFAULT_PASSWORD: &str = "test-mcp-password";

/// How long the server may take to print its address and answer.
const START_TIMEOUT: Duration = Duration::from_secs(60);

pub struct PlmFixture {
    /// Behind a lock: a `plm_server` step stops and starts it mid-script.
    child: Mutex<Child>,
    /// How to start the server again on the same address and data.
    respawn: Respawn,
    /// `http://127.0.0.1:<port>`, no trailing slash.
    pub url: String,
    /// Which binary is serving, and when it was built: `<path> (built <UTC>)`.
    /// The run prints it, because "the newer of debug and release" can pick a
    /// stale build without anyone noticing.
    pub binary: String,
    /// Username → token secret. `admin` is always present.
    tokens: BTreeMap<String, String>,
    /// Username → password, for a `browser` call's sign-in. `admin` too.
    passwords: BTreeMap<String, String>,
    /// The account the app was configured as (empty: none).
    app_user: String,
    /// Values `bind` captured, plus the built-ins.
    vars: Mutex<BTreeMap<String, Value>>,
    /// The server's data directory, removed on drop.
    data: PathBuf,
    agent: ureq::Agent,
}

/// What a `plm_server` start needs to bring the same server back: the same
/// binary, arguments, data directory and address (the app's `plm.json` names
/// that address, so a new port would read as a different server).
struct Respawn {
    bin: PathBuf,
    bind: String,
    args: Vec<String>,
    env: BTreeMap<String, String>,
    log: PathBuf,
}

/// Spawn `brep-plm serve` and drain both pipes into `log` (appended) for the
/// server's whole life, so a chatty server never blocks on a full pipe.
/// Stdout lines are also sent on the channel for the caller to read the
/// start-up lines from.
fn spawn_server(respawn: &Respawn, data: &Path) -> Result<(Child, mpsc::Receiver<String>), String> {
    let mut child = Command::new(&respawn.bin)
        .arg("serve")
        .arg("--bind")
        .arg(&respawn.bind)
        .arg("--data")
        .arg(data)
        .args(&respawn.args)
        // Never clone KiCad's libraries from GitLab; a script's `env` may override.
        .env("BREP_PLM_KICAD_GIT_BASE", "off")
        .envs(&respawn.env)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .map_err(|e| format!("spawn {}: {e}", respawn.bin.display()))?;
    let file = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&respawn.log)
        .map_err(|e| format!("{}: {e}", respawn.log.display()))?;
    let log = std::sync::Arc::new(Mutex::new(file));
    let (tx, rx) = mpsc::channel::<String>();
    let stdout = child.stdout.take().ok_or("no stdout pipe")?;
    let stderr = child.stderr.take().ok_or("no stderr pipe")?;
    {
        let log = log.clone();
        std::thread::spawn(move || {
            for line in BufReader::new(stdout).lines().map_while(Result::ok) {
                let _ = writeln!(log.lock().unwrap(), "{line}");
                let _ = tx.send(line);
            }
        });
    }
    std::thread::spawn(move || {
        for line in BufReader::new(stderr).lines().map_while(Result::ok) {
            let _ = writeln!(log.lock().unwrap(), "stderr: {line}");
        }
    });
    Ok((child, rx))
}

/// An answer from the server, whatever its status.
pub struct Answer {
    pub status: u16,
    pub headers: Vec<(String, String)>,
    pub body: Vec<u8>,
}

impl Answer {
    fn json(&self) -> Option<Value> {
        serde_json::from_slice(&self.body).ok()
    }

    /// The shape a `plm_request` step returns: `status`, `json` (the body
    /// parsed, or null), `text` (the body when it is not JSON), `headers`.
    pub fn to_value(&self) -> Value {
        let parsed = self.json();
        let text = if parsed.is_none() { Value::String(String::from_utf8_lossy(&self.body).into_owned()) } else { Value::Null };
        let headers: serde_json::Map<String, Value> =
            self.headers.iter().map(|(k, v)| (k.to_ascii_lowercase(), Value::String(v.clone()))).collect();
        json!({ "status": self.status, "json": parsed.unwrap_or(Value::Null), "text": text, "headers": headers, "size": self.body.len() })
    }
}

/// Where the `brep-plm` binary is: `BREP_PLM_BIN`, else the newest debug or
/// release build in a checkout `target/` found at run time (`./build.sh
/// test-mcp` sets `BREP_PLM_BIN`; cargo tests run from a crate directory).
///
/// The search is resolved at run time, never from `env!("CARGO_MANIFEST_DIR")`:
/// a compile-time source path is written into every binary that links this
/// crate (the shipped brep-app among them), which leaks the build machine's
/// paths and fails the PLM local bundle's private-path audit.
pub fn server_binary() -> Result<PathBuf, String> {
    if let Some(bin) = std::env::var_os("BREP_PLM_BIN") {
        let bin = PathBuf::from(bin);
        return if bin.is_file() { Ok(bin) } else { Err(format!("BREP_PLM_BIN={} is not a file", bin.display())) };
    }
    let mut targets: Vec<PathBuf> = Vec::new();
    let mut push = |p: PathBuf| {
        if p.is_dir() && !targets.contains(&p) {
            targets.push(p);
        }
    };
    // The working directory (a crate directory under `cargo test`, or the
    // checkout root) and its parent: their `target/` and BREP_plm's own.
    if let Ok(cwd) = std::env::current_dir() {
        for dir in [Some(cwd.as_path()), cwd.parent()].into_iter().flatten() {
            push(dir.join("target"));
            push(dir.join("BREP_plm").join("target"));
        }
    }
    // The `target/` directory the running executable was built into.
    if let Ok(exe) = std::env::current_exe() {
        if let Some(target) = exe.ancestors().find(|a| a.file_name().is_some_and(|n| n == "target")) {
            push(target.to_path_buf());
        }
    }
    let name = if cfg!(windows) { "brep-plm.exe" } else { "brep-plm" };
    let modified = |p: &PathBuf| std::fs::metadata(p).and_then(|m| m.modified()).ok();
    targets
        .iter()
        .flat_map(|target| ["debug", "release"].map(|profile| target.join(profile).join(name)))
        .filter(|p| p.is_file())
        .max_by_key(modified)
        .ok_or_else(|| {
            let searched: Vec<String> = targets.iter().map(|t| t.display().to_string()).collect();
            format!(
                "no brep-plm binary under [{}] — build it with `cargo build --manifest-path BREP_plm/Cargo.toml --bin brep-plm` or set BREP_PLM_BIN",
                searched.join(", ")
            )
        })
}

/// What the server's stdout told the fixture while it started.
#[derive(Default)]
struct Startup {
    password: Option<String>,
    /// From `brep-plm: listening on <addr>`: the address the OS gave `:0`.
    listening: Option<String>,
}

impl PlmFixture {
    /// Start and seed a server for `spec`, its data under `out_dir/plm`.
    /// `store` is the session's store directory, which receives D5's files.
    pub fn start(spec: &PlmSpec, out_dir: &Path, store: &Path) -> Result<Self, String> {
        let bin = server_binary()?;
        let root = out_dir.join("plm");
        if root.exists() {
            std::fs::remove_dir_all(&root).map_err(|e| format!("clear {}: {e}", root.display()))?;
        }
        std::fs::create_dir_all(&root).map_err(|e| format!("{}: {e}", root.display()))?;
        let data = data_dir(&root)?;
        let log_path = root.join("serve.log");
        let respawn = Respawn {
            bin: bin.clone(),
            // `:0`: the OS picks the port and the server reports it, so no
            // other process can take it between choosing and binding.
            bind: "127.0.0.1:0".into(),
            args: spec.args.clone(),
            env: spec.env.clone(),
            log: log_path.clone(),
        };
        let _ = std::fs::remove_file(&log_path);
        let (mut child, rx) = spawn_server(&respawn, &data)?;

        let fail = |child: &mut Child, why: String| -> String {
            let _ = child.kill();
            let _ = child.wait();
            let _ = std::fs::remove_dir_all(&data);
            format!("{why} (server log: {})", log_path.display())
        };

        let deadline = Instant::now() + START_TIMEOUT;
        let mut seen = Startup::default();
        while seen.listening.is_none() {
            let left = deadline.saturating_duration_since(Instant::now());
            match rx.recv_timeout(left) {
                Ok(line) => read_startup_line(&line, &mut seen),
                Err(mpsc::RecvTimeoutError::Timeout) => {
                    return Err(fail(
                        &mut child,
                        format!(
                            "brep-plm printed no `brep-plm: listening on` line within {} s (a binary from before that line cannot serve the fixture)",
                            START_TIMEOUT.as_secs()
                        ),
                    ))
                }
                Err(mpsc::RecvTimeoutError::Disconnected) => {
                    let status = child.wait().map(|s| s.to_string()).unwrap_or_default();
                    return Err(fail(&mut child, format!("brep-plm exited before serving ({status})")));
                }
            }
        }
        let Some(password) = seen.password else {
            return Err(fail(&mut child, "brep-plm did not print a first-run administrator password".into()));
        };
        let url = format!("http://{}", seen.listening.as_deref().unwrap_or_default());
        let agent = ureq::AgentBuilder::new().redirects(0).timeout(Duration::from_secs(60)).build();
        let respawn = Respawn { bind: seen.listening.clone().unwrap_or_default(), ..respawn };
        let mut fixture = Self {
            child: Mutex::new(child),
            respawn,
            url,
            binary: describe_binary(&bin),
            tokens: BTreeMap::new(),
            passwords: BTreeMap::new(),
            app_user: String::new(),
            vars: Mutex::new(BTreeMap::new()),
            data: data.clone(),
            agent,
        };
        // The line is printed once bound; wait for an answer of any kind
        // before seeding all the same.
        loop {
            match fixture.send("GET", "/api/me", &[], None) {
                Ok(_) => break,
                Err(e) if Instant::now() > deadline => return Err(fail(fixture.child.get_mut().unwrap(), format!("brep-plm never answered: {e}"))),
                Err(_) => std::thread::sleep(Duration::from_millis(50)),
            }
        }
        if let Err(e) = fixture.seed(spec, &password) {
            return Err(fail(fixture.child.get_mut().unwrap(), format!("seeding the PLM: {e}")));
        }
        if let Err(e) = fixture.configure_app(&spec.app_user, spec.app_token, store) {
            return Err(fail(fixture.child.get_mut().unwrap(), e));
        }
        Ok(fixture)
    }

    /// Sign in as the first-run administrator, mint every account and token,
    /// then play `setup`.
    fn seed(&mut self, spec: &PlmSpec, password: &str) -> Result<(), String> {
        let login = self.send("POST", "/api/login", &[], Some(json_body(&json!({ "username": "admin", "password": password }))))?;
        if login.status != 200 {
            return Err(format!("admin sign-in answered {}: {}", login.status, String::from_utf8_lossy(&login.body)));
        }
        // The session cookie is the first `name=value` of Set-Cookie.
        let cookie = login
            .headers
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case("set-cookie"))
            .and_then(|(_, v)| v.split(';').next())
            .map(str::to_string)
            .ok_or("admin sign-in set no cookie")?;
        let me = self.send("GET", "/api/me", &[("Cookie", &cookie)], None)?;
        let csrf = me
            .json()
            .and_then(|v| v["csrf"].as_str().map(str::to_string))
            .ok_or_else(|| format!("/api/me gave the session no csrf ({})", me.status))?;
        let session = [("Cookie", cookie.as_str()), ("X-CSRF-Token", csrf.as_str())];
        let mint = |fixture: &Self, name: &str, scope: &str, user_id: Option<&str>| -> Result<String, String> {
            let mut body = json!({ "name": name, "scope": scope });
            if let Some(id) = user_id {
                body["user_id"] = json!(id);
            }
            let made = fixture.send("POST", "/api/tokens", &session, Some(json_body(&body)))?;
            made.json()
                .and_then(|v| v["token"].as_str().map(str::to_string))
                .ok_or_else(|| format!("minting a token for {name} answered {}: {}", made.status, String::from_utf8_lossy(&made.body)))
        };
        let admin = mint(self, "test-mcp admin", "full", None)?;
        self.tokens.insert("admin".into(), admin);
        self.passwords.insert("admin".into(), password.to_string());
        for user in &spec.users {
            let body = json!({
                "username": user.username,
                "password": user.password.as_deref().unwrap_or(DEFAULT_PASSWORD),
                "groups": user.groups,
            });
            let made = self.send("POST", "/api/users", &session, Some(json_body(&body)))?;
            if made.status >= 400 {
                return Err(format!("creating {} answered {}: {}", user.username, made.status, String::from_utf8_lossy(&made.body)));
            }
            let users = self.send("GET", "/api/users", &session, None)?.json().unwrap_or(Value::Null);
            let id = users
                .as_array()
                .and_then(|list| list.iter().find(|u| u["username"] == json!(user.username)))
                .and_then(|u| u["id"].as_str())
                .ok_or_else(|| format!("{} is not in /api/users after creating it", user.username))?
                .to_string();
            let token = mint(self, &format!("test-mcp {}", user.username), user.scope.as_deref().unwrap_or("full"), Some(&id))?;
            self.tokens.insert(user.username.clone(), token);
            self.passwords.insert(user.username.clone(), user.password.clone().unwrap_or_else(|| DEFAULT_PASSWORD.into()));
        }
        {
            let mut vars = self.vars.lock().unwrap();
            vars.insert("url".into(), Value::String(self.url.clone()));
            for (user, token) in &self.tokens {
                vars.insert(format!("token:{user}"), Value::String(token.clone()));
            }
        }
        for (i, call) in spec.setup.iter().enumerate() {
            let answer = self.call(call)?;
            let ok = match call.status {
                Some(want) => answer.status == want,
                None => answer.status < 400,
            };
            if !ok {
                return Err(format!(
                    "setup {i} ({} {}) answered {}: {}",
                    call.method,
                    call.path,
                    answer.status,
                    String::from_utf8_lossy(&answer.body)
                ));
            }
        }
        Ok(())
    }

    /// Write D5's files for `user` into the store (nothing for an empty name).
    fn configure_app(&mut self, user: &str, with_token: bool, store: &Path) -> Result<(), String> {
        self.app_user = user.to_string();
        if user.is_empty() {
            return Ok(());
        }
        let token = self.tokens.get(user).ok_or_else(|| format!("app_user `{user}` is not an account the fixture made"))?.clone();
        self.vars.lock().unwrap().insert("token".into(), Value::String(token.clone()));
        std::fs::create_dir_all(store).map_err(|e| format!("{}: {e}", store.display()))?;
        let config = store.join("plm.json");
        std::fs::write(&config, serde_json::to_string_pretty(&json!({ "url": self.url })).unwrap() + "\n")
            .map_err(|e| format!("{}: {e}", config.display()))?;
        if with_token {
            let token_path = store.join("plm-token");
            write_secret(&token_path, &format!("{token}\n")).map_err(|e| format!("{}: {e}", token_path.display()))?;
        }
        Ok(())
    }

    /// The token of `user` (`admin`, a created account).
    pub fn token(&self, user: &str) -> Option<&str> {
        self.tokens.get(user).map(String::as_str)
    }

    /// Replace every `${plm:NAME}` in the strings of `value`. An unknown name
    /// is an error, so a typo cannot quietly send the literal text.
    pub fn substitute(&self, value: Value) -> Result<Value, String> {
        let vars = self.vars.lock().unwrap();
        substitute_vars(value, &vars)
    }

    /// Make one call (a `setup` entry or a `plm_request` step) and apply its
    /// `bind`s. Only a request that got no answer is an `Err`.
    pub fn call(&self, call: &PlmCall) -> Result<Answer, String> {
        let path = self.substitute(Value::String(call.path.clone()))?;
        let path = path.as_str().unwrap_or_default().to_string();
        let body = match (&call.body, &call.raw) {
            (Some(_), Some(_)) => return Err("a PLM call takes `body` or `raw`, not both".into()),
            (Some(body), None) => Some(json_body(&self.substitute(body.clone())?)),
            (None, Some(raw)) => {
                let raw = self.substitute(Value::String(raw.clone()))?;
                Some(raw.as_str().unwrap_or_default().as_bytes().to_vec())
            }
            (None, None) => None,
        };
        let who = call.as_user.as_deref().unwrap_or("admin");
        let bearer;
        let session;
        let mut headers: Vec<(&str, &str)> = Vec::new();
        if call.browser {
            session = self.browser_session(who)?;
            headers.push(("Cookie", &session.0));
            headers.push(("X-CSRF-Token", &session.1));
        } else if who != "anonymous" {
            let token = self.tokens.get(who).ok_or_else(|| format!("`as: {who}` is not an account the fixture made"))?;
            bearer = format!("Bearer {token}");
            headers.push(("Authorization", &bearer));
        }
        if let Some(content_type) = call.content_type.as_deref().filter(|_| call.raw.is_some()) {
            headers.push(("Content-Type", content_type));
        }
        let method = call.method.to_ascii_uppercase();
        let answer = self.send(&method, &path, &headers, body)?;
        if !call.bind.is_empty() {
            let parsed = answer
                .json()
                .ok_or_else(|| format!("{method} {path} answered {} with no JSON to bind from", answer.status))?;
            self.bind(&parsed, &call.bind).map_err(|e| format!("{method} {path} answered {}: {e}", answer.status))?;
        }
        Ok(answer)
    }

    /// A `plm_server` step: `stop` kills the server (the app's next request
    /// finds nothing listening, as when a server goes down or the network
    /// drops); `start` brings the same server back on the same address and
    /// data, and answers once it serves. Accounts, tokens and every write
    /// before the stop survive, as they do a real restart.
    pub fn server(&self, action: &str) -> Result<Value, String> {
        let mut child = self.child.lock().unwrap();
        match action {
            "stop" => {
                let _ = child.kill();
                let _ = child.wait();
                Ok(json!({ "stopped": true }))
            }
            "start" => {
                if child.try_wait().map_err(|e| e.to_string())?.is_none() {
                    return Err("`plm_server` start: the server is already running".into());
                }
                let (fresh, rx) = spawn_server(&self.respawn, &self.data)?;
                *child = fresh;
                let deadline = Instant::now() + START_TIMEOUT;
                let mut seen = Startup::default();
                while seen.listening.is_none() {
                    match rx.recv_timeout(deadline.saturating_duration_since(Instant::now())) {
                        Ok(line) => read_startup_line(&line, &mut seen),
                        Err(_) => return Err(format!("brep-plm did not serve again (server log: {})", self.respawn.log.display())),
                    }
                }
                while let Err(e) = self.send("GET", "/api/me", &[], None) {
                    if Instant::now() > deadline {
                        return Err(format!("brep-plm never answered after the restart: {e}"));
                    }
                    std::thread::sleep(Duration::from_millis(50));
                }
                Ok(json!({ "started": true, "url": self.url }))
            }
            other => Err(format!("`plm_server` action `{other}`: expected `stop` or `start`")),
        }
    }

    /// A fresh browser session of `who`: its cookie and CSRF token.
    fn browser_session(&self, who: &str) -> Result<(String, String), String> {
        let password = self.passwords.get(who).ok_or_else(|| format!("`as: {who}` has no password the fixture knows"))?;
        let login = self.send("POST", "/api/login", &[], Some(json_body(&json!({ "username": who, "password": password }))))?;
        let cookie = login
            .headers
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case("set-cookie"))
            .and_then(|(_, v)| v.split(';').next())
            .map(str::to_string)
            .ok_or_else(|| format!("{who}'s sign-in answered {} with no cookie", login.status))?;
        let csrf = login
            .json()
            .and_then(|v| v["csrf"].as_str().map(str::to_string))
            .ok_or_else(|| format!("{who}'s sign-in gave no csrf"))?;
        Ok((cookie, csrf))
    }

    /// Capture values of `from` under names (`name → JSON pointer`), for
    /// `${plm:name}` in later steps: a `plm_request`'s answer, or any step's
    /// result (the step's own `bind`). A pointer that finds nothing fails.
    pub fn bind(&self, from: &Value, names: &BTreeMap<String, String>) -> Result<(), String> {
        let mut vars = self.vars.lock().unwrap();
        for (name, pointer) in names {
            let found = from.pointer(pointer).ok_or_else(|| format!("bind `{name}`: nothing at `{pointer}`"))?;
            vars.insert(name.clone(), found.clone());
        }
        Ok(())
    }

    fn send(&self, method: &str, path: &str, headers: &[(&str, &str)], body: Option<Vec<u8>>) -> Result<Answer, String> {
        let mut request = self.agent.request(method, &format!("{}{}", self.url, path));
        if body.is_some() {
            let is_json = body.as_deref().map(|b| serde_json::from_slice::<Value>(b).is_ok()).unwrap_or(false);
            if is_json {
                request = request.set("Content-Type", "application/json");
            }
        }
        for (name, value) in headers {
            request = request.set(name, value);
        }
        let sent = match body {
            Some(bytes) => request.send_bytes(&bytes),
            None => request.call(),
        };
        let response = match sent {
            Ok(response) => response,
            Err(ureq::Error::Status(_, response)) => response,
            Err(ureq::Error::Transport(e)) => return Err(format!("{method} {path}: {e}")),
        };
        let status = response.status();
        let mut headers = Vec::new();
        for name in response.headers_names() {
            for value in response.all(&name) {
                headers.push((name.clone(), value.to_string()));
            }
        }
        let mut body = Vec::new();
        response.into_reader().read_to_end(&mut body).map_err(|e| format!("{method} {path}: reading the answer: {e}"))?;
        Ok(Answer { status, headers, body })
    }
}

impl Drop for PlmFixture {
    fn drop(&mut self) {
        let child = self.child.get_mut().unwrap();
        let _ = child.kill();
        let _ = child.wait();
        let _ = std::fs::remove_dir_all(&self.data);
    }
}

/// A fresh, empty data directory for one server: under
/// `BREP_PLM_FIXTURE_DATA`, else `/dev/shm`, else `<root>/data`.
fn data_dir(root: &Path) -> Result<PathBuf, String> {
    let parent = std::env::var_os("BREP_PLM_FIXTURE_DATA")
        .map(PathBuf::from)
        .or_else(|| Some(PathBuf::from("/dev/shm")).filter(|p| cfg!(target_os = "linux") && p.is_dir()));
    let data = match parent {
        Some(parent) => {
            static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
            let n = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0);
            parent.join(format!("brep-mcp-plm-{}-{n}-{nanos}", std::process::id()))
        }
        None => root.join("data"),
    };
    if data.exists() {
        std::fs::remove_dir_all(&data).map_err(|e| format!("clear {}: {e}", data.display()))?;
    }
    std::fs::create_dir_all(&data).map_err(|e| format!("{}: {e}", data.display()))?;
    Ok(data)
}

/// `<path> (built 2026-09-25T15:04:05Z)`, from the file's mtime.
fn describe_binary(bin: &Path) -> String {
    let built = std::fs::metadata(bin)
        .and_then(|m| m.modified())
        .ok()
        .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
        .map(|d| utc_stamp(d.as_secs()))
        .unwrap_or_else(|| "at an unknown time".into());
    let path = bin.canonicalize().unwrap_or_else(|_| bin.to_path_buf());
    format!("{} (built {built})", path.display())
}

/// Seconds since the epoch as an ISO-8601 UTC stamp (Howard Hinnant's
/// days-to-civil), so the fixture needs no date crate.
fn utc_stamp(secs: u64) -> String {
    let days = (secs / 86_400) as i64;
    let rem = secs % 86_400;
    let z = days + 719_468;
    let era = z.div_euclid(146_097);
    let doe = z.rem_euclid(146_097);
    let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let day = doy - (153 * mp + 2) / 5 + 1;
    let month = if mp < 10 { mp + 3 } else { mp - 9 };
    let year = yoe + era * 400 + i64::from(month <= 2);
    format!("{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}Z", rem / 3_600, rem % 3_600 / 60, rem % 60)
}

fn json_body(value: &Value) -> Vec<u8> {
    serde_json::to_vec(value).unwrap_or_default()
}

/// Read one line of the server's start-up output.
fn read_startup_line(line: &str, seen: &mut Startup) {
    let trimmed = line.trim();
    if let Some(rest) = trimmed.strip_prefix("password") {
        seen.password = Some(rest.trim().to_string());
    } else if let Some(addr) = trimmed.strip_prefix("brep-plm: listening on ") {
        seen.listening = Some(addr.trim().trim_start_matches("http://").to_string());
    }
}

/// A string that is EXACTLY one variable becomes the variable's JSON value
/// (an object stays an object, a number a number), so an expectation can
/// compare a whole captured value; a variable inside longer text is spliced
/// in as text.
fn substitute_vars(value: Value, vars: &BTreeMap<String, Value>) -> Result<Value, String> {
    let text_of = |v: &Value| match v {
        Value::String(s) => s.clone(),
        other => other.to_string(),
    };
    if let Value::String(s) = &value {
        if let Some(name) = s.strip_prefix("${plm:").and_then(|rest| rest.strip_suffix('}')) {
            if !name.contains('}') && !name.contains("${") {
                if let Some(found) = vars.get(name) {
                    return Ok(found.clone());
                }
            }
        }
    }
    Ok(match value {
        Value::String(s) if s.contains("${plm:") => {
            let mut out = String::new();
            let mut rest = s.as_str();
            while let Some(start) = rest.find("${plm:") {
                out.push_str(&rest[..start]);
                let after = &rest[start + 6..];
                let end = after.find('}').ok_or_else(|| format!("unclosed `${{plm:` in `{s}`"))?;
                let name = &after[..end];
                let found = vars.get(name).ok_or_else(|| {
                    format!("`${{plm:{name}}}` is not known (have: {})", vars.keys().cloned().collect::<Vec<_>>().join(", "))
                })?;
                out.push_str(&text_of(found));
                rest = &after[end + 1..];
            }
            out.push_str(rest);
            Value::String(out)
        }
        Value::Array(a) => Value::Array(a.into_iter().map(|x| substitute_vars(x, vars)).collect::<Result<_, _>>()?),
        Value::Object(o) => {
            Value::Object(o.into_iter().map(|(k, x)| substitute_vars(x, vars).map(|x| (k, x))).collect::<Result<_, _>>()?)
        }
        other => other,
    })
}

/// A secret file: created 0600 on unix, so the token is never world-readable
/// even for the instant between create and chmod.
fn write_secret(path: &Path, text: &str) -> std::io::Result<()> {
    let mut options = std::fs::OpenOptions::new();
    options.write(true).create(true).truncate(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    options.open(path)?.write_all(text.as_bytes())
}