Skip to main content

caboodle/
adapter.rs

1// Compatibility: retained library imports, persisted paths, environment keys and
2// explicit old-command/version probes remain readable during the product rename.
3use std::{
4    env,
5    ffi::{OsStr, OsString},
6    fs,
7    path::{Path, PathBuf},
8    process::{Command, Output},
9    time::{SystemTime, UNIX_EPOCH},
10};
11
12use anyhow::{bail, Context, Result};
13use serde_json::{json, Value};
14
15use crate::model::{QuipuFlavor, ToolName};
16
17mod manifest;
18mod quipu_alias;
19pub(crate) use manifest::{
20    find_program, identify_member, prove_release, release_target, single_program, stage, verify_at,
21};
22
23pub trait Adapter {
24    fn name(&self) -> ToolName;
25    /// Release identity reviewed and pinned by this Tambo build.
26    fn desired_version(&self) -> String;
27    fn install(&self) -> Result<()>;
28    fn version(&self) -> Result<String>;
29    fn verify(&self) -> Result<()>;
30
31    /// Bind package verification to the executables PATH actually runs. Most
32    /// adapters already use PATH; Quipu and manifest members normally prefer
33    /// their managed Cargo install.
34    fn package_adapter(&self) -> Result<Option<Box<dyn Adapter>>> {
35        Ok(None)
36    }
37
38    fn is_current(&self, installed: &str) -> bool {
39        installed == self.desired_version()
40    }
41}
42
43/// Why `install` cannot fetch a reviewed artifact for this host, if it cannot.
44/// Read-only: it inspects the platform, never the network. The same target
45/// functions drive the installers, so the answer cannot drift from them.
46pub fn release_blocker(name: ToolName, quipu_flavor: QuipuFlavor) -> Option<String> {
47    let result = match name {
48        ToolName::Quipu if quipu_flavor == QuipuFlavor::Release => quipu_release_target(),
49        ToolName::Bobbin => bobbin_release_target(),
50        ToolName::Yupana => yupana_release_target(),
51        ToolName::Member(name) => match crate::members::get(name) {
52            Some(m) => manifest::release_target(m),
53            None => return Some(format!("unknown stack member {name}")),
54        },
55        // Camayoc is a source archive, Desire Path and the lancedb Quipu
56        // flavor are source builds: their blockers are missing prerequisites.
57        // Named, never a wildcard: a new variant must decide (aegis-1i5h1j).
58        ToolName::Quipu | ToolName::Camayoc | ToolName::DesirePath => return None,
59    };
60    result.err().map(|error| format!("{error:#}"))
61}
62
63/// Host commands the install and verification steps of one tool shell out to.
64pub fn prerequisites(name: ToolName, quipu_flavor: QuipuFlavor) -> &'static [&'static str] {
65    match name {
66        ToolName::Quipu if quipu_flavor == QuipuFlavor::Lancedb => &["cargo"],
67        ToolName::Quipu => &["curl", "tar"],
68        // Camayoc's bootstrap is bash + python3, and it talks to Quipu with curl.
69        // Checksums are computed in-process (aegis-em2.2): macOS ships sha256sum
70        // only in /sbin, which a LaunchAgent PATH need not contain.
71        ToolName::Camayoc => &["curl", "tar", "bash", "python3"],
72        ToolName::Bobbin => &["curl", "tar", "git"],
73        ToolName::Yupana => &["curl", "tar"],
74        ToolName::DesirePath => &["go"],
75        ToolName::Member(name) => crate::members::prerequisites(name),
76    }
77}
78
79/// The reviewed source build to run by hand where no release exists for a host.
80pub fn source_install_command(name: ToolName) -> Option<String> {
81    match name {
82        ToolName::Quipu => Some(format!("cargo {}", quipu_install_args(QuipuFlavor::Release).join(" "))),
83        ToolName::Yupana => Some(format!(
84            "cargo install --git https://github.com/scbrown/yupana --tag v{YUPANA_VERSION} --locked --features mcp"
85        )),
86        // A member installs only its reviewed release; there is no source path.
87        ToolName::Camayoc | ToolName::Bobbin | ToolName::DesirePath | ToolName::Member(_) => None,
88    }
89}
90
91/// Executables a tool puts on PATH, in the order a shell would look them up.
92pub fn programs(name: ToolName) -> &'static [&'static str] {
93    match name {
94        ToolName::Quipu => &["qp", "quipu", "quipu-server"],
95        ToolName::Camayoc => &[],
96        ToolName::Bobbin => &["bobbin"],
97        ToolName::Yupana => &["yupana"],
98        ToolName::DesirePath => &["dp"],
99        ToolName::Member(name) => crate::members::programs(name),
100    }
101}
102
103/// Every executable named `program` on `path`, in lookup order.
104pub fn which_all(program: &str, path: Option<&std::ffi::OsStr>) -> Vec<PathBuf> {
105    let Some(path) = path else {
106        return Vec::new();
107    };
108    let mut found: Vec<PathBuf> = Vec::new();
109    let mut targets: Vec<PathBuf> = Vec::new();
110    for candidate in env::split_paths(path).map(|directory| directory.join(program)) {
111        if !is_executable(&candidate) {
112            continue;
113        }
114        // A directory listed twice, or a symlink to a copy already seen, is the
115        // same binary rather than a shadowing one.
116        let target = candidate
117            .canonicalize()
118            .unwrap_or_else(|_| candidate.clone());
119        if !targets.contains(&target) {
120            targets.push(target);
121            found.push(candidate);
122        }
123    }
124    found
125}
126
127pub(crate) fn is_executable(path: &Path) -> bool {
128    let Ok(metadata) = path.metadata() else {
129        return false;
130    };
131    if !metadata.is_file() {
132        return false;
133    }
134    #[cfg(unix)]
135    {
136        use std::os::unix::fs::PermissionsExt;
137        metadata.permissions().mode() & 0o111 != 0
138    }
139    #[cfg(not(unix))]
140    true
141}
142
143/// What a shell actually runs for one managed program (aegis-70qlhs).
144///
145/// Versions are read from the MANAGED copy (`$CARGO_HOME/bin`), so a stale
146/// binary earlier on PATH can make `verify` report a tool current while the
147/// user runs something else. That happened: a fresh `cargo install` exited 0,
148/// the new binary sat on disk, and an older copy in `~/.local/bin` kept
149/// answering. Nothing said which copy ran.
150#[derive(Debug, PartialEq, Eq)]
151pub enum PathResolution {
152    /// PATH runs the managed copy, or there is no managed copy to compare.
153    Managed,
154    /// The program is not on PATH at all.
155    NotOnPath,
156    /// PATH runs a different file that reports the same `--version`.
157    SameBuild { runs: PathBuf, managed: PathBuf },
158    /// PATH runs a different file that reports a different `--version`.
159    Shadowed {
160        runs: PathBuf,
161        runs_version: String,
162        managed: PathBuf,
163        managed_version: String,
164    },
165}
166
167/// Resolve `program` the way a shell would, against the managed copy.
168pub fn path_resolution(program: &str, path: Option<&OsStr>) -> PathResolution {
169    let Some(managed) = managed_bin_dir().map(|dir| dir.join(program)) else {
170        return PathResolution::Managed;
171    };
172    if !is_executable(&managed) {
173        return PathResolution::Managed;
174    }
175    let Some(runs) = which_all(program, path).into_iter().next() else {
176        return PathResolution::NotOnPath;
177    };
178    let canonical = |p: &Path| p.canonicalize().unwrap_or_else(|_| p.to_path_buf());
179    if canonical(&runs) == canonical(&managed) {
180        return PathResolution::Managed;
181    }
182    let version = |p: &Path| {
183        p.to_str()
184            .and_then(|s| read_version(s).ok())
185            .unwrap_or_else(|| "unreadable".to_owned())
186    };
187    let (runs_version, managed_version) = (version(&runs), version(&managed));
188    if runs_version == managed_version {
189        PathResolution::SameBuild { runs, managed }
190    } else {
191        PathResolution::Shadowed {
192            runs,
193            runs_version,
194            managed,
195            managed_version,
196        }
197    }
198}
199
200/// Where the release and cargo installers write binaries.
201pub fn managed_bin_dir() -> Option<PathBuf> {
202    env::var_os("CARGO_HOME")
203        .map(PathBuf::from)
204        .or_else(|| env::var_os("HOME").map(|home| PathBuf::from(home).join(".cargo")))
205        .map(|home| home.join("bin"))
206}
207
208pub fn adapter(name: ToolName, quipu_flavor: QuipuFlavor) -> Box<dyn Adapter> {
209    match name {
210        ToolName::Quipu => Box::new(Quipu {
211            flavor: quipu_flavor,
212            external: None,
213        }),
214        ToolName::Camayoc => Box::new(Camayoc),
215        ToolName::Bobbin => Box::new(Bobbin),
216        ToolName::Yupana => Box::new(Yupana),
217        ToolName::DesirePath => Box::new(DesirePath),
218        ToolName::Member(name) => Box::new(manifest::ManifestAdapter(
219            crate::members::get(name)
220                .expect("a Member name is only minted for an embedded manifest"),
221        )),
222    }
223}
224
225/// Resolve external binaries once so identity and round trips use the same files.
226pub fn adapter_for_plan(name: ToolName, plan: &crate::model::Plan) -> Result<Box<dyn Adapter>> {
227    if name != ToolName::Quipu || !plan.external_quipu {
228        return Ok(adapter(name, plan.quipu_flavor));
229    }
230    let path = env::var_os("PATH");
231    let resolve = |name| -> Result<PathBuf> {
232        which_all(name, path.as_deref())
233            .into_iter()
234            .next()
235            .with_context(|| format!("external Quipu requires {name} on PATH"))?
236            .canonicalize()
237            .with_context(|| format!("resolve external {name}"))
238    };
239    Ok(Box::new(Quipu {
240        flavor: plan.quipu_flavor,
241        external: Some((resolve("quipu")?, resolve("quipu-server")?)),
242    }))
243}
244
245fn output<P, I, S>(program: P, args: I, cwd: Option<&Path>) -> Result<Output>
246where
247    P: AsRef<OsStr>,
248    I: IntoIterator<Item = S>,
249    S: AsRef<OsStr>,
250{
251    let args: Vec<OsString> = args
252        .into_iter()
253        .map(|arg| arg.as_ref().to_os_string())
254        .collect();
255    let program = program.as_ref();
256    let mut command = Command::new(program);
257    command.args(&args);
258    if let Some(cwd) = cwd {
259        command.current_dir(cwd);
260    }
261    command.output().with_context(|| {
262        format!(
263            "run {} {}",
264            program.to_string_lossy(),
265            args.iter()
266                .map(|arg| arg.to_string_lossy())
267                .collect::<Vec<_>>()
268                .join(" ")
269        )
270    })
271}
272
273pub(crate) fn checked<P, I, S>(program: P, args: I, cwd: Option<&Path>) -> Result<Output>
274where
275    P: AsRef<OsStr>,
276    I: IntoIterator<Item = S>,
277    S: AsRef<OsStr>,
278{
279    let program = program.as_ref();
280    let result = output(program, args, cwd)?;
281    if !result.status.success() {
282        bail!(
283            "{} failed ({})\nstdout:\n{}\nstderr:\n{}",
284            program.to_string_lossy(),
285            result.status,
286            String::from_utf8_lossy(&result.stdout).trim(),
287            String::from_utf8_lossy(&result.stderr).trim()
288        );
289    }
290    Ok(result)
291}
292
293fn read_version(program: &str) -> Result<String> {
294    let result = checked(program, ["--version"], None)?;
295    let version = String::from_utf8_lossy(&result.stdout).trim().to_owned();
296    if version.is_empty() {
297        bail!("{} --version returned no version", program);
298    }
299    Ok(version)
300}
301
302fn read_cargo_version(program: &str) -> Result<String> {
303    let program_path = cargo_program(program);
304    read_version(
305        program_path
306            .to_str()
307            .with_context(|| format!("{} path is not valid UTF-8", program_path.display()))?,
308    )
309}
310
311fn cargo_program(program: &str) -> PathBuf {
312    let cargo_home = env::var_os("CARGO_HOME")
313        .map(PathBuf::from)
314        .or_else(|| env::var_os("HOME").map(|home| PathBuf::from(home).join(".cargo")));
315    if let Some(path) = cargo_home.map(|home| home.join("bin").join(program)) {
316        if path.is_file() {
317            return path;
318        }
319    }
320    PathBuf::from(program)
321}
322
323fn require_minimum_version(output: &str, minimum: (u64, u64, u64), program: &str) -> Result<()> {
324    let raw = output
325        .split_whitespace()
326        .nth(1)
327        .with_context(|| format!("{program} --version omitted a semantic version"))?;
328    let mut parts = raw.trim_start_matches('v').split('.');
329    let found = (
330        parts.next().and_then(|value| value.parse().ok()),
331        parts.next().and_then(|value| value.parse().ok()),
332        parts.next().and_then(|value| value.parse().ok()),
333    );
334    let found = match found {
335        (Some(major), Some(minor), Some(patch)) => (major, minor, patch),
336        _ => bail!("{program} returned an invalid semantic version: {raw}"),
337    };
338    if found < minimum {
339        bail!(
340            "{program} {}.{}.{} is too old; Tambo requires at least {}.{}.{}",
341            found.0,
342            found.1,
343            found.2,
344            minimum.0,
345            minimum.1,
346            minimum.2
347        );
348    }
349    Ok(())
350}
351
352struct Quipu {
353    flavor: QuipuFlavor,
354    external: Option<(PathBuf, PathBuf)>,
355}
356
357const QUIPU_VERSION: &str = "0.9.0";
358
359/// The reviewed feature set per flavor. Both flavors build the identical
360/// pinned revision — only the feature list differs — so choosing `lancedb`
361/// can never smuggle in an unreviewed Quipu.
362fn quipu_features(flavor: QuipuFlavor) -> &'static str {
363    match flavor {
364        QuipuFlavor::Release => "full",
365        QuipuFlavor::Lancedb => "full,lancedb",
366    }
367}
368
369impl Adapter for Quipu {
370    fn package_adapter(&self) -> Result<Option<Box<dyn Adapter>>> {
371        let path = env::var_os("PATH");
372        let resolve = |name| {
373            which_all(name, path.as_deref())
374                .into_iter()
375                .next()
376                .with_context(|| format!("package Quipu requires {name} on PATH"))
377        };
378        // Keep the package-manager symlinks rather than the old Cellar targets:
379        // brew may replace both targets during this same update.
380        Ok(Some(Box::new(Self {
381            flavor: self.flavor,
382            external: Some((resolve("quipu")?, resolve("quipu-server")?)),
383        })))
384    }
385
386    fn name(&self) -> ToolName {
387        ToolName::Quipu
388    }
389
390    fn desired_version(&self) -> String {
391        match self.flavor {
392            QuipuFlavor::Release => format!("quipu {QUIPU_VERSION}; quipu-server {QUIPU_VERSION}"),
393            QuipuFlavor::Lancedb => {
394                format!("quipu {QUIPU_VERSION}; quipu-server {QUIPU_VERSION} (+lancedb)")
395            }
396        }
397    }
398
399    fn is_current(&self, _installed: &str) -> bool {
400        // version() already enforces the reviewed minimum for both binaries;
401        // a newer compatible Quipu release is not a downgrade candidate.
402        // Flavor drift is invisible here — `quipu --version` cannot say which
403        // features were compiled in — so verify() is what catches it.
404        true
405    }
406
407    fn install(&self) -> Result<()> {
408        if self.external.is_some() {
409            bail!("external Quipu is not owned by Tambo; installation refused");
410        }
411        preflight_owned_quipu_alias()?;
412        match self.flavor {
413            QuipuFlavor::Release => install_quipu_release(),
414            // The published archive is the reviewed `full` build. LanceDB is
415            // an explicit non-default flavor and still needs a source build
416            // until Quipu publishes a separately identifiable artifact.
417            QuipuFlavor::Lancedb => {
418                let mut args: Vec<OsString> = quipu_install_args(self.flavor)
419                    .iter()
420                    .map(OsString::from)
421                    .collect();
422                args.push("--root".into());
423                args.push(
424                    owned_quipu_bin()?
425                        .parent()
426                        .context("cargo bin has no root")?
427                        .as_os_str()
428                        .into(),
429                );
430                checked("cargo", args, None)?;
431                ensure_owned_quipu_alias()
432            }
433        }
434    }
435
436    fn version(&self) -> Result<String> {
437        // `cargo install` writes these binaries under CARGO_HOME, which may be
438        // later on PATH than a legacy Quipu install. Read back the location we
439        // actually update so a successful install cannot be reported as stale.
440        let client = if let Some((client, _)) = &self.external {
441            read_version(
442                client
443                    .to_str()
444                    .context("external Quipu path is not UTF-8")?,
445            )?
446        } else {
447            read_cargo_version("quipu")?
448        };
449        require_minimum_version(&client, (0, 3, 27), "quipu")?;
450        let server = if let Some((_, server)) = &self.external {
451            read_version(
452                server
453                    .to_str()
454                    .context("external server path is not UTF-8")?,
455            )?
456        } else {
457            read_cargo_version("quipu-server")?
458        };
459        require_minimum_version(&server, (0, 3, 27), "quipu-server")?;
460        Ok(match &self.external {
461            Some((c, s)) => format!(
462                "{client}; {server}; external executables: {} | {}",
463                c.display(),
464                s.display()
465            ),
466            None => format!("{client}; {server}"),
467        })
468    }
469
470    fn verify(&self) -> Result<()> {
471        let client = self
472            .external
473            .as_ref()
474            .map(|(c, _)| c.clone())
475            .map(Ok)
476            .unwrap_or_else(selected_quipu_client)?;
477        verify_quipu_alias(&client)?;
478        self.verify_core()
479    }
480}
481
482impl Quipu {
483    fn verify_core(&self) -> Result<()> {
484        if self.flavor == QuipuFlavor::Lancedb {
485            verify_quipu_lancedb_feature()?;
486        }
487        let client = self
488            .external
489            .as_ref()
490            .map(|(client, _)| client.as_path())
491            .unwrap_or_else(|| Path::new("quipu"));
492        let root = tempfile::tempdir().context("create quipu verification directory")?;
493        let db = root.path().join("verify.db");
494        let episode = root.path().join("episode.json");
495        let marker = "tambo-verify-roundtrip";
496        fs::write(
497            &episode,
498            format!(
499                r#"{{"name":"Tambo verification","episode_body":"functional round trip","source":"caboodle","group_id":"caboodle-verification","nodes":[{{"name":"{marker}","type":"Verification","description":"Tambo isolated verification"}}],"edges":[]}}"#
500            ),
501        )
502        .context("write quipu verification episode")?;
503
504        let query = format!(
505            "SELECT ?s ?label WHERE {{ ?s <http://www.w3.org/2000/01/rdf-schema#label> ?label . FILTER(?label = \"{marker}\") }}"
506        );
507        let before = checked(
508            client,
509            [
510                OsStr::new("read"),
511                OsStr::new(&query),
512                OsStr::new("--db"),
513                db.as_os_str(),
514            ],
515            None,
516        )?;
517        if String::from_utf8_lossy(&before.stdout).contains(marker) {
518            bail!("quipu negative control unexpectedly found the verification node");
519        }
520
521        checked(
522            client,
523            [
524                OsStr::new("episode"),
525                episode.as_os_str(),
526                OsStr::new("--base-ns"),
527                OsStr::new("https://example.org/tambo/verification/"),
528                OsStr::new("--db"),
529                db.as_os_str(),
530            ],
531            None,
532        )?;
533        let after = checked(
534            client,
535            [
536                OsStr::new("read"),
537                OsStr::new(&query),
538                OsStr::new("--db"),
539                db.as_os_str(),
540            ],
541            None,
542        )?;
543        if !String::from_utf8_lossy(&after.stdout).contains(marker) {
544            bail!("quipu episode landed but read-back did not find the verification node");
545        }
546        // The stack's shared vocabulary rides with Quipu: the pinned Quechua
547        // release must load and resolve a term, with an absent-term control.
548        crate::vocabulary::verify_at(client).context("Quechua vocabulary verification")?;
549        Ok(())
550    }
551}
552
553/// Prove exactly the selected pair, using a throwaway client store, never a live server.
554pub(crate) fn verify_quipu_pair(client: &Path, server: &Path) -> Result<()> {
555    Quipu {
556        flavor: QuipuFlavor::Release,
557        external: Some((client.to_path_buf(), server.to_path_buf())),
558    }
559    .verify_core()?;
560    let help = checked(server, ["--help"], None)?;
561    let help = String::from_utf8_lossy(&help.stdout);
562    if !help.contains("--db") || !help.contains("--bind") {
563        bail!("Quipu server omitted isolated database/bind command contract");
564    }
565    Ok(())
566}
567
568pub(crate) fn quipu_release_target() -> Result<&'static str> {
569    quipu_release_target_for(env::consts::ARCH, env::consts::OS)
570}
571
572fn quipu_release_target_for(arch: &str, os: &str) -> Result<&'static str> {
573    match (arch, os) {
574        ("x86_64", "linux") => Ok("x86_64-unknown-linux-gnu"),
575        ("aarch64", "linux") => Ok("aarch64-unknown-linux-gnu"),
576        ("x86_64", "macos") => Ok("x86_64-apple-darwin"),
577        ("aarch64", "macos") => Ok("aarch64-apple-darwin"),
578        (arch, os) => bail!("Quipu has no checksummed Tambo release for {arch}-{os}"),
579    }
580}
581
582fn fetch_quipu_release(target: &str, download: &Path) -> Result<PathBuf> {
583    // release-please tags the crate package; the release workflow prefixes
584    // that complete tag again for both the asset and its top-level directory.
585    let tag = format!("quipu-ai-v{QUIPU_VERSION}");
586    let directory = format!("quipu-{tag}-{target}");
587    let archive_name = format!("{directory}.tar.gz");
588    let base = format!("https://github.com/scbrown/quipu/releases/download/{tag}");
589    let archive = download.join(&archive_name);
590    let checksum = download.join(format!("{archive_name}.sha256"));
591    download_https(&format!("{base}/{archive_name}"), &archive)?;
592    download_https(&format!("{base}/{archive_name}.sha256"), &checksum)?;
593    verify_checksum(&archive, &checksum)?;
594
595    let unpacked = download.join(directory);
596    checked(
597        "tar",
598        [
599            OsStr::new("-xzf"),
600            archive.as_os_str(),
601            OsStr::new("-C"),
602            download.as_os_str(),
603        ],
604        None,
605    )?;
606    for program in ["quipu", "quipu-server"] {
607        if !unpacked.join(program).is_file() {
608            bail!("Quipu release archive omitted {program}");
609        }
610    }
611    if !fs::symlink_metadata(unpacked.join("quipu"))?
612        .file_type()
613        .is_file()
614    {
615        bail!("Quipu primary archive executable must be regular");
616    }
617    crate::quipu_layout::alias(&unpacked.join("quipu-server"))?;
618    Ok(unpacked)
619}
620
621fn install_quipu_release() -> Result<()> {
622    let target = quipu_release_target()?;
623    let download = tempfile::tempdir().context("create Quipu download directory")?;
624    let unpacked = fetch_quipu_release(target, download.path())?;
625    let home = env::var_os("HOME").context("HOME is required to install Quipu")?;
626    let bin = env::var_os("CARGO_HOME")
627        .map(PathBuf::from)
628        .unwrap_or_else(|| PathBuf::from(home).join(".cargo"))
629        .join("bin");
630    fs::create_dir_all(&bin).with_context(|| format!("create {}", bin.display()))?;
631    // Old reviewed releases keep their two regular executables. A unified
632    // archive installs one regular artifact and one exact relative alias.
633    let unified = crate::quipu_layout::alias(&unpacked.join("quipu-server"))?.is_some();
634    for program in if unified {
635        &["quipu"][..]
636    } else {
637        &["quipu", "quipu-server"][..]
638    } {
639        crate::quipu_layout::replace_regular(&unpacked.join(program), &bin.join(program))?;
640    }
641    if unified {
642        crate::quipu_layout::replace_alias(&bin.join("quipu-server"))?;
643    }
644    ensure_owned_quipu_alias()
645}
646
647fn owned_quipu_bin() -> Result<PathBuf> {
648    env::var_os("CARGO_HOME")
649        .map(PathBuf::from)
650        .or_else(|| env::var_os("HOME").map(|h| PathBuf::from(h).join(".cargo")))
651        .map(|root| root.join("bin"))
652        .context("HOME or CARGO_HOME is required for owned Quipu installation")
653}
654
655pub(crate) fn preflight_owned_quipu_alias() -> Result<()> {
656    quipu_alias::preflight(&owned_quipu_bin()?).map_err(anyhow::Error::msg)
657}
658
659pub(crate) fn ensure_owned_quipu_alias() -> Result<()> {
660    quipu_alias::ensure(&owned_quipu_bin()?).map_err(anyhow::Error::msg)
661}
662
663/// Post-install self-healing for the owned Quipu directory only; an externally
664/// installed Quipu elsewhere is never touched (aegis-61f).
665#[cfg(unix)]
666pub fn repair_owned_quipu_alias() -> Result<String> {
667    use quipu_alias::Repair;
668    let bin = owned_quipu_bin()?;
669    let qp = bin.join("qp");
670    Ok(
671        match quipu_alias::repair(&bin).map_err(anyhow::Error::msg)? {
672            Repair::NoQuipu => format!("no owned quipu in {}; nothing to repair", bin.display()),
673            Repair::Current => format!("{} -> quipu already current", qp.display()),
674            Repair::Created => format!("created {} -> quipu", qp.display()),
675            Repair::Retargeted => format!("retargeted {} -> quipu", qp.display()),
676            Repair::ForeignPreserved => format!(
677                "{} is not a symlink; preserved it, qp NOT repaired",
678                qp.display()
679            ),
680            Repair::PackageManaged(package) => format!(
681                "quipu in {} is managed by apt (package {package}); qp left to the package",
682                bin.display()
683            ),
684        },
685    )
686}
687
688fn selected_quipu_client() -> Result<PathBuf> {
689    let owned = cargo_program("quipu");
690    if owned.is_file() {
691        return Ok(owned);
692    }
693    which_all("quipu", env::var_os("PATH").as_deref())
694        .into_iter()
695        .next()
696        .context("Quipu CLI is not on PATH")
697}
698
699pub(crate) fn ensure_existing_quipu_alias() -> Result<()> {
700    let client = selected_quipu_client()?;
701    let bin = client
702        .parent()
703        .context("selected Quipu CLI has no directory")?;
704    quipu_alias::ensure(bin).map_err(anyhow::Error::msg)
705}
706
707pub(crate) fn verify_existing_quipu_alias() -> Result<()> {
708    verify_quipu_alias(&selected_quipu_client()?)
709}
710
711fn verify_quipu_alias(client: &Path) -> Result<()> {
712    let bin = client.parent().context("Quipu CLI has no directory")?;
713    let alias = bin.join("qp");
714    let resolved = client.canonicalize()?;
715    let resolved_bin = resolved.parent().context("Quipu CLI has no directory")?;
716    // The relative link may sit beside the selected quipu or beside the file it
717    // resolves to: a package manager (Homebrew) links bin/ into its own tree,
718    // and the formula owns `qp -> quipu` there (aegis-6f1).
719    let relative =
720        |dir: &Path| fs::read_link(dir.join("qp")).ok().as_deref() == Some(Path::new("quipu"));
721    if !relative(bin) && !relative(resolved_bin) {
722        bail!(
723            "Quipu verification requires relative qp -> quipu in {}",
724            bin.display()
725        );
726    }
727    // The qp a user types (beside the selected quipu) must be the same program.
728    if alias.canonicalize().ok().as_deref() != Some(resolved.as_path()) {
729        bail!("qp and quipu do not resolve to the same executable");
730    }
731    if read_version(alias.to_str().context("qp path is not UTF-8")?)?
732        != read_version(client.to_str().context("Quipu path is not UTF-8")?)?
733    {
734        bail!("qp and quipu version outputs disagree");
735    }
736    Ok(())
737}
738
739fn quipu_install_args(flavor: QuipuFlavor) -> [&'static str; 8] {
740    [
741        "install",
742        "quipu-ai",
743        "--version",
744        QUIPU_VERSION,
745        "--locked",
746        "--features",
747        quipu_features(flavor),
748        "--bins",
749    ]
750}
751
752/// The plan asked for the lancedb flavor. `quipu --version` cannot say which
753/// features were compiled in, so ask the running server's per-feature compile
754/// map — the only read-back that can prove the flavor, as opposed to trusting
755/// that `cargo install --features lancedb` exited zero.
756fn verify_quipu_lancedb_feature() -> Result<()> {
757    let server = env::var("QUIPU_SERVER").unwrap_or_else(|_| "http://localhost:3030".to_owned());
758    let version = curl_get_json(&format!("{server}/version"))
759        .context("read the Quipu server per-feature compile map")?;
760    require_compiled_feature(&version, "lancedb")
761}
762
763fn require_compiled_feature(version: &Value, feature: &str) -> Result<()> {
764    let compiled = version
765        .get("features")
766        .and_then(Value::as_object)
767        .with_context(|| {
768            format!(
769                "Quipu /version returned no per-feature compile map; cannot prove the {feature} flavor"
770            )
771        })?;
772    if compiled.get(feature).and_then(Value::as_bool) != Some(true) {
773        bail!(
774            "the plan requires the {feature} flavor but the running Quipu was compiled without it; \
775             reinstall the reviewed revision with `cargo install quipu-ai --version {QUIPU_VERSION} \
776             --locked --features full,{feature} --bins`"
777        );
778    }
779    Ok(())
780}
781
782struct Bobbin;
783struct Yupana;
784struct DesirePath;
785
786struct Camayoc;
787const BOBBIN_VERSION: &str = "0.30.0";
788
789impl Adapter for Camayoc {
790    fn name(&self) -> ToolName {
791        ToolName::Camayoc
792    }
793
794    fn desired_version(&self) -> String {
795        format!("camayoc {CAMAYOC_REVISION}")
796    }
797
798    fn install(&self) -> Result<()> {
799        install_camayoc_bundle()
800    }
801
802    fn version(&self) -> Result<String> {
803        let revision = fs::read_to_string(camayoc_root()?.join("REVISION"))
804            .context("read installed Camayoc revision")?;
805        let revision = revision.trim();
806        if revision.is_empty() {
807            bail!("installed Camayoc revision is empty");
808        }
809        Ok(format!("camayoc {revision}"))
810    }
811
812    fn verify(&self) -> Result<()> {
813        let root = camayoc_root()?;
814        // Isolated like every other adapter (aegis-ro425e.2): bootstrap is pointed at a
815        // free localhost port inside a tempdir, so it starts its OWN quipu-server from
816        // <tempdir>/.quipu instead of loading ontology, shapes and a marker into whatever
817        // QUIPU_SERVER names. Bootstrapping the user's real server is a separate, explicit
818        // step: run the bundle's scripts/bootstrap.sh yourself.
819        let scratch = tempfile::tempdir().context("create Camayoc verification directory")?;
820        let port = free_local_port()?;
821        let server = format!("http://127.0.0.1:{port}");
822        let _guard = ScratchServer(scratch.path().join(".quipu/server.pid"));
823        let home = scratch.path().join("home");
824        fs::create_dir(&home).context("create Camayoc verification HOME")?;
825        let bootstrap = Command::new("bash")
826            .arg(root.join("scripts/bootstrap.sh"))
827            .current_dir(scratch.path())
828            .env("QUIPU_SERVER", &server)
829            .env("CLAUDE_PROJECT_DIR", scratch.path())
830            .env("HOME", &home)
831            .env_remove("XDG_CONFIG_HOME")
832            .env_remove("CLAUDE_CONFIG_DIR")
833            .env_remove("QUIPU_AUTH_TOKEN_FILE")
834            .env_remove("QUIPU_AUTH_TOKEN")
835            .output()
836            .context("run Camayoc bootstrap against a scratch Quipu")?;
837        if !bootstrap.status.success() {
838            bail!(
839                "Camayoc bootstrap failed against scratch Quipu {server} ({})\nstdout:\n{}\nstderr:\n{}",
840                bootstrap.status,
841                String::from_utf8_lossy(&bootstrap.stdout).trim(),
842                String::from_utf8_lossy(&bootstrap.stderr).trim()
843            );
844        }
845        if !scratch.path().join(".quipu/server.pid").exists() {
846            bail!("Camayoc bootstrap did not start a scratch Quipu at {server}; refusing to verify against a server Tambo does not own");
847        }
848        let token = fs::read_to_string(home.join(".config/quipu/token"))
849            .context("read scratch Camayoc credential")?;
850        if token.trim().is_empty() {
851            bail!("scratch Camayoc credential is empty");
852        }
853        let auth = tempfile::NamedTempFile::new_in(scratch.path())
854            .context("create scratch Camayoc curl config")?;
855        fs::write(auth.path(), curl_auth_header_line(token.trim())?)
856            .context("write scratch Camayoc curl config")?;
857        verify_camayoc_first_ingest(&root, &server, auth.path())
858    }
859}
860
861/// Stops the scratch quipu-server Camayoc's bootstrap started, on every exit path.
862struct ScratchServer(PathBuf);
863
864impl Drop for ScratchServer {
865    fn drop(&mut self) {
866        if let Ok(pid) = fs::read_to_string(&self.0) {
867            let pid = pid.trim();
868            if !pid.is_empty() && pid.chars().all(|c| c.is_ascii_digit()) {
869                let _ = Command::new("kill").arg(pid).status();
870            }
871        }
872    }
873}
874
875fn free_local_port() -> Result<u16> {
876    let listener =
877        std::net::TcpListener::bind("127.0.0.1:0").context("reserve a free localhost port")?;
878    Ok(listener.local_addr()?.port())
879}
880
881impl Adapter for Bobbin {
882    fn name(&self) -> ToolName {
883        ToolName::Bobbin
884    }
885
886    fn desired_version(&self) -> String {
887        format!("bobbin {BOBBIN_VERSION}")
888    }
889
890    fn is_current(&self, installed: &str) -> bool {
891        installed.strip_prefix("bobbin ").is_some_and(|rest| {
892            rest == BOBBIN_VERSION || rest.starts_with(&format!("{BOBBIN_VERSION} "))
893        })
894    }
895
896    fn install(&self) -> Result<()> {
897        install_bobbin_release()
898    }
899
900    fn version(&self) -> Result<String> {
901        read_version("bobbin")
902    }
903
904    fn verify(&self) -> Result<()> {
905        let root = tempfile::tempdir().context("create bobbin verification repository")?;
906        let nonce = SystemTime::now()
907            .duration_since(UNIX_EPOCH)
908            .context("system clock predates Unix epoch")?
909            .as_nanos();
910        let repo = format!("tambo-verify-{}-{nonce}", std::process::id());
911        let marker = format!("tambo_verify_marker_{}_{nonce}", std::process::id());
912        checked("git", ["init", "--quiet"], Some(root.path()))?;
913        checked("bobbin", ["init"], Some(root.path()))?;
914        fs::write(
915            root.path().join("control.rs"),
916            "/// Proves the negative-control index is live.\npub fn tambo_control() -> usize {\n    1\n}\n",
917        )
918        .context("write bobbin negative-control fixture")?;
919        checked(
920            "bobbin",
921            [
922                "index",
923                ".",
924                "--source",
925                ".",
926                "--repo",
927                &repo,
928                "--skip-calibrate",
929            ],
930            Some(root.path()),
931        )?;
932
933        let before = checked(
934            "bobbin",
935            ["grep", &marker, "--repo", &repo, "--json"],
936            Some(root.path()),
937        )?;
938        if bobbin_result_count(&before.stdout)? != 0 {
939            bail!("bobbin negative control unexpectedly found the fixture marker");
940        }
941
942        fs::write(
943            root.path().join("fixture.rs"),
944            format!(
945                "/// A unique Tambo verification routine.\npub fn {marker}() -> &'static str {{\n    \"indexed\"\n}}\n"
946            ),
947        )
948        .context("write bobbin verification fixture")?;
949        checked(
950            "bobbin",
951            [
952                "index",
953                ".",
954                "--source",
955                ".",
956                "--repo",
957                &repo,
958                "--skip-calibrate",
959            ],
960            Some(root.path()),
961        )?;
962        let after = checked(
963            "bobbin",
964            ["grep", &marker, "--repo", &repo, "--json"],
965            Some(root.path()),
966        )?;
967        if bobbin_result_count(&after.stdout)? == 0 {
968            bail!("bobbin indexed the fixture but search did not return its marker");
969        }
970        Ok(())
971    }
972}
973
974const YUPANA_VERSION: &str = "0.19.4";
975
976impl Adapter for Yupana {
977    fn name(&self) -> ToolName {
978        ToolName::Yupana
979    }
980
981    fn desired_version(&self) -> String {
982        format!("yupana {YUPANA_VERSION}")
983    }
984
985    fn install(&self) -> Result<()> {
986        let (target, checksum) = yupana_release_for(env::consts::ARCH, env::consts::OS)?;
987        let root = tempfile::tempdir().context("create Yupana download directory")?;
988        fetch_yupana_release(target, checksum, root.path())?;
989        let home = env::var_os("HOME").context("HOME is required to install Yupana")?;
990        let bin = env::var_os("CARGO_HOME")
991            .map(PathBuf::from)
992            .unwrap_or_else(|| PathBuf::from(home).join(".cargo"))
993            .join("bin");
994        fs::create_dir_all(&bin).with_context(|| format!("create {}", bin.display()))?;
995        fs::copy(root.path().join("yupana"), bin.join("yupana"))
996            .context("install checksummed Yupana binary")?;
997        Ok(())
998    }
999
1000    fn version(&self) -> Result<String> {
1001        let version = read_version("yupana")?;
1002        require_minimum_version(&version, (0, 6, 4), "yupana")?;
1003        Ok(version)
1004    }
1005
1006    fn verify(&self) -> Result<()> {
1007        let root = tempfile::tempdir().context("create Yupana verification repository")?;
1008        let state = root.path().join("state");
1009        fs::write(
1010            root.path().join("control.rs"),
1011            "pub fn tambo_yupana_control() -> usize { 1 }\n",
1012        )?;
1013        yupana_checked(root.path(), &state, ["analyze", "."])?;
1014        let before = yupana_checked(root.path(), &state, ["callers", "tambo_yupana_target", "."])?;
1015        if String::from_utf8_lossy(&before.stdout).contains("tambo_yupana_caller") {
1016            bail!("Yupana negative control unexpectedly found the fixture caller");
1017        }
1018        fs::write(
1019            root.path().join("fixture.rs"),
1020            "pub fn tambo_yupana_target() -> usize { 1 }\npub fn tambo_yupana_caller() -> usize { tambo_yupana_target() }\n",
1021        )?;
1022        yupana_checked(root.path(), &state, ["analyze", "."])?;
1023        let after = yupana_checked(root.path(), &state, ["callers", "tambo_yupana_target", "."])?;
1024        if !String::from_utf8_lossy(&after.stdout).contains("tambo_yupana_caller") {
1025            bail!("Yupana analyzed the fixture but callers did not return its caller");
1026        }
1027        Ok(())
1028    }
1029}
1030
1031fn yupana_release_target() -> Result<&'static str> {
1032    yupana_release_for(env::consts::ARCH, env::consts::OS).map(|(target, _)| target)
1033}
1034
1035// Digests of the published v0.19.4 archives, recomputed from the downloaded
1036// archives and verified against SHA256SUMS and each .sha256.
1037fn yupana_release_for(arch: &str, os: &str) -> Result<(&'static str, &'static str)> {
1038    match (arch, os) {
1039        ("x86_64", "linux") => Ok((
1040            "x86_64-linux-gnu",
1041            "3111542bbb3e1da6fe0f452cf729a7c3bdb0787f8b64cb87b92cde130e6a2db7",
1042        )),
1043        ("aarch64", "macos") => Ok((
1044            "aarch64-apple-darwin",
1045            "f3768ab3d661996c5ac7ec12d30c513687062823ae44b04d1d3229dc04c2b76b",
1046        )),
1047        ("x86_64", "macos") => Ok((
1048            "x86_64-apple-darwin",
1049            "01d0337ed3e26b0cc6ac833b41ba933ad8c852cb859249a9381b421881e86044",
1050        )),
1051        _ => bail!("Yupana v{YUPANA_VERSION} has no checksummed Tambo release for {arch}-{os}"),
1052    }
1053}
1054
1055fn fetch_yupana_release(target: &str, checksum: &str, root: &Path) -> Result<()> {
1056    let archive_name = format!("yupana-v{YUPANA_VERSION}-{target}.tar.gz");
1057    let archive = root.join(&archive_name);
1058    download_https(
1059        &format!(
1060            "https://github.com/scbrown/yupana/releases/download/v{YUPANA_VERSION}/{archive_name}"
1061        ),
1062        &archive,
1063    )?;
1064    verify_yupana_checksum(&archive, checksum)?;
1065    checked(
1066        "tar",
1067        [
1068            OsStr::new("-xzf"),
1069            archive.as_os_str(),
1070            OsStr::new("-C"),
1071            root.as_os_str(),
1072        ],
1073        None,
1074    )?;
1075    Ok(())
1076}
1077
1078fn verify_yupana_checksum(archive: &Path, expected: &str) -> Result<()> {
1079    if manifest::sha256_file(archive)? != expected {
1080        bail!("Yupana release checksum mismatch");
1081    }
1082    Ok(())
1083}
1084
1085fn yupana_checked<I, S>(cwd: &Path, state: &Path, args: I) -> Result<Output>
1086where
1087    I: IntoIterator<Item = S>,
1088    S: AsRef<OsStr>,
1089{
1090    let home = cwd.join("home");
1091    fs::create_dir_all(&home)?;
1092    let mut command = Command::new("yupana");
1093    command
1094        .args(args)
1095        .current_dir(cwd)
1096        .env("HOME", home)
1097        .env("XDG_STATE_HOME", state);
1098    let result = command
1099        .output()
1100        .context("run isolated Yupana verification")?;
1101    if !result.status.success() {
1102        bail!(
1103            "isolated Yupana verification failed ({}): {}",
1104            result.status,
1105            String::from_utf8_lossy(&result.stderr).trim()
1106        );
1107    }
1108    Ok(result)
1109}
1110
1111// Review and install the executable the user's shell actually selects. CARGO_HOME
1112// must not silently override a different dp on PATH (aegis-fnp1wp).
1113fn desire_path_program() -> Result<PathBuf> {
1114    if let Some(path) = env::var_os("PATH") {
1115        for directory in env::split_paths(&path) {
1116            let candidate = directory.join("dp");
1117            if candidate.is_file() {
1118                #[cfg(unix)]
1119                {
1120                    use std::os::unix::fs::PermissionsExt;
1121                    if candidate.metadata()?.permissions().mode() & 0o111 == 0 {
1122                        continue;
1123                    }
1124                }
1125                return fs::canonicalize(candidate).context("resolve Desire Path executable");
1126            }
1127        }
1128    }
1129    let home = env::var_os("HOME").context("HOME is required to install Desire Path")?;
1130    Ok(PathBuf::from(home).join(".local/bin/dp"))
1131}
1132
1133const DESIRE_PATH_REVISION: &str = "06812336fda6a6fd3fe4cbcae2d50a303e95a024";
1134const DESIRE_PATH_VERSION: &str = "v0.10.0";
1135
1136impl Adapter for DesirePath {
1137    fn name(&self) -> ToolName {
1138        ToolName::DesirePath
1139    }
1140
1141    fn desired_version(&self) -> String {
1142        format!("dp {DESIRE_PATH_VERSION} ({})", &DESIRE_PATH_REVISION[..7])
1143    }
1144
1145    fn is_current(&self, installed: &str) -> bool {
1146        let expected = self.desired_version();
1147        installed.trim_start_matches("dp ").trim_start_matches('v')
1148            == expected.trim_start_matches("dp ").trim_start_matches('v')
1149    }
1150
1151    fn install(&self) -> Result<()> {
1152        let executable = desire_path_program()?;
1153        let bin = executable
1154            .parent()
1155            .context("Desire Path needs an install directory")?;
1156        fs::create_dir_all(bin).with_context(|| format!("create {}", bin.display()))?;
1157        let ldflags = format!(
1158            "-X github.com/scbrown/desire-path/internal/cli.Version={DESIRE_PATH_VERSION} -X github.com/scbrown/desire-path/internal/cli.Commit={DESIRE_PATH_REVISION}"
1159        );
1160        let mut command = Command::new("go");
1161        command
1162            .args([
1163                "install",
1164                "-ldflags",
1165                &ldflags,
1166                &format!("github.com/scbrown/desire-path/cmd/dp@{DESIRE_PATH_REVISION}"),
1167            ])
1168            .env("GOBIN", bin);
1169        let result = command
1170            .output()
1171            .context("build pinned Desire Path revision")?;
1172        if !result.status.success() {
1173            bail!(
1174                "Desire Path install failed ({}): {}",
1175                result.status,
1176                String::from_utf8_lossy(&result.stderr).trim()
1177            );
1178        }
1179        Ok(())
1180    }
1181
1182    fn version(&self) -> Result<String> {
1183        let result = checked(desire_path_program()?, ["version"], None)?;
1184        let version = String::from_utf8_lossy(&result.stdout).trim().to_owned();
1185        // Identity only, like every other adapter: `is_current` and package
1186        // `standing` judge it against the pin. Refusing here made a brew-owned
1187        // dp that is AHEAD of the pin unreadable, so the member failed (aegis-fv4).
1188        if version.is_empty() {
1189            bail!("dp version returned no version");
1190        }
1191        Ok(version)
1192    }
1193
1194    fn verify(&self) -> Result<()> {
1195        let dp = desire_path_program()?;
1196        let root = tempfile::tempdir().context("create Desire Path verification directory")?;
1197        let db = root.path().join("desires.db");
1198        let marker = "tambo_desire_path_marker";
1199        let before = checked(
1200            &dp,
1201            [
1202                "--db",
1203                db.to_str().context("Desire Path DB path is not UTF-8")?,
1204                "--json",
1205                "list",
1206            ],
1207            Some(root.path()),
1208        )?;
1209        if String::from_utf8_lossy(&before.stdout).contains(marker) {
1210            bail!("Desire Path negative control unexpectedly found the marker");
1211        }
1212        let mut child = Command::new(&dp)
1213            .args([
1214                "--db",
1215                db.to_str().unwrap(),
1216                "ingest",
1217                "--source",
1218                "claude-code",
1219            ])
1220            .current_dir(root.path())
1221            .stdin(std::process::Stdio::piped())
1222            .stdout(std::process::Stdio::piped())
1223            .stderr(std::process::Stdio::piped())
1224            .spawn()
1225            .context("start Desire Path ingest verification")?;
1226        use std::io::Write as _;
1227        child
1228            .stdin
1229            .take()
1230            .context("open Desire Path verification stdin")?
1231            .write_all(
1232                format!(r#"{{"tool_name":"{marker}","error":"fixture failure"}}"#).as_bytes(),
1233            )?;
1234        let ingested = child.wait_with_output()?;
1235        if !ingested.status.success() {
1236            bail!(
1237                "Desire Path ingest verification failed ({}): {}",
1238                ingested.status,
1239                String::from_utf8_lossy(&ingested.stderr).trim()
1240            );
1241        }
1242        let after = checked(
1243            &dp,
1244            ["--db", db.to_str().unwrap(), "--json", "list"],
1245            Some(root.path()),
1246        )?;
1247        if !String::from_utf8_lossy(&after.stdout).contains(marker) {
1248            bail!("Desire Path ingested the fixture but its reader path did not return it");
1249        }
1250        Ok(())
1251    }
1252}
1253
1254fn bobbin_result_count(stdout: &[u8]) -> Result<u64> {
1255    let response: serde_json::Value =
1256        serde_json::from_slice(stdout).context("parse bobbin search JSON")?;
1257    response["count"]
1258        .as_u64()
1259        .context("bobbin search JSON omitted numeric count")
1260}
1261
1262fn bobbin_release_target() -> Result<&'static str> {
1263    match (env::consts::ARCH, env::consts::OS) {
1264        ("x86_64", "linux") => Ok("x86_64-unknown-linux-gnu"),
1265        ("aarch64", "linux") => Ok("aarch64-unknown-linux-gnu"),
1266        ("x86_64", "macos") => Ok("x86_64-apple-darwin"),
1267        ("aarch64", "macos") => Ok("aarch64-apple-darwin"),
1268        (arch, os) => bail!("bobbin has no Tambo release target for {arch}-{os}"),
1269    }
1270}
1271
1272fn install_bobbin_release() -> Result<()> {
1273    let target = bobbin_release_target()?;
1274    let archive = format!("bobbin-v{BOBBIN_VERSION}-{target}.tar.gz");
1275    let base = format!("https://github.com/scbrown/bobbin/releases/download/v{BOBBIN_VERSION}");
1276    let download = tempfile::tempdir().context("create bobbin download directory")?;
1277    let archive_path = download.path().join(&archive);
1278    let sums_path = download.path().join("SHA256SUMS.txt");
1279    download_https(&format!("{base}/{archive}"), &archive_path)?;
1280    download_https(&format!("{base}/SHA256SUMS.txt"), &sums_path)?;
1281    verify_checksum(&archive_path, &sums_path)?;
1282
1283    let home = env::var_os("HOME").context("HOME is required to install bobbin")?;
1284    let install_root = PathBuf::from(&home)
1285        .join(".local/share/caboodle/bobbin")
1286        .join(format!("v{BOBBIN_VERSION}"));
1287    fs::create_dir_all(&install_root)
1288        .with_context(|| format!("create bobbin install root {}", install_root.display()))?;
1289    checked(
1290        "tar",
1291        [
1292            OsStr::new("-xzf"),
1293            archive_path.as_os_str(),
1294            OsStr::new("--strip-components=1"),
1295            OsStr::new("-C"),
1296            install_root.as_os_str(),
1297        ],
1298        None,
1299    )?;
1300
1301    let bin_dir = env::var_os("CARGO_HOME")
1302        .map(PathBuf::from)
1303        .unwrap_or_else(|| PathBuf::from(home).join(".cargo"))
1304        .join("bin");
1305    fs::create_dir_all(&bin_dir)
1306        .with_context(|| format!("create binary directory {}", bin_dir.display()))?;
1307    let link = bin_dir.join("bobbin");
1308    let target = install_root.join("bobbin");
1309    ensure_bobbin_link(&link, &target)?;
1310    Ok(())
1311}
1312
1313fn ensure_bobbin_link(link: &Path, target: &Path) -> Result<()> {
1314    if !target.is_file() {
1315        bail!("Bobbin release archive omitted {}", target.display());
1316    }
1317    match link.symlink_metadata() {
1318        Ok(metadata)
1319            if metadata.file_type().is_symlink()
1320                && fs::read_link(link).ok().as_deref() == Some(target) =>
1321        {
1322            return Ok(());
1323        }
1324        // A DANGLING symlink is Tambo's own debris, not a user's install, and
1325        // refusing to repair it deadlocks the tool against itself.
1326        //
1327        // Measured on Tambo CI, red on main 2026-08-31 -> 2026-09-02, one job,
1328        // every run:
1329        //
1330        //     bobbin: not installed (run bobbin --version:
1331        //             No such file or directory (os error 2)); installing
1332        //     Error: bobbin install step
1333        //     Caused by: refusing to replace existing
1334        //                /home/runner/.cargo/bin/bobbin; remove or repair it explicitly
1335        //
1336        // Those two lines are the SAME fact seen from two sides. Executing a
1337        // symlink whose target is gone fails with ENOENT, which is exactly why the
1338        // probe a moment earlier concluded bobbin was not installed — so the
1339        // installer refuses to repair the very condition that summoned it. The
1340        // state is then permanent: never detected as present, never allowed to be
1341        // written. Nothing a re-run can do changes it.
1342        //
1343        // The refusal is right for anything a person could still be using — a real
1344        // binary, a directory, a symlink that resolves. A dangling link is none of
1345        // those: following it is an error for every reader, so there is no working
1346        // installation here to protect.
1347        Ok(metadata) if metadata.file_type().is_symlink() && !link.exists() => {
1348            fs::remove_file(link).with_context(|| format!("remove dangling {}", link.display()))?;
1349        }
1350        Ok(_) => {
1351            bail!(
1352                "refusing to replace existing {}; preserve it at a new backup path such as {}.before-tambo, then rerun apply; see Installing > Replacing a cargo-installed Bobbin for backup and rollback commands",
1353                link.display(),
1354                link.display()
1355            );
1356        }
1357        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
1358        Err(error) => return Err(error).with_context(|| format!("inspect {}", link.display())),
1359    }
1360    #[cfg(unix)]
1361    std::os::unix::fs::symlink(target, link)
1362        .with_context(|| format!("link bobbin into {}", link.display()))?;
1363    #[cfg(not(unix))]
1364    bail!("bobbin release installation currently requires a Unix host");
1365    Ok(())
1366}
1367
1368// Official v0.1.11 source release; keep version, revision, release asset and
1369// digest paired. Digest is the release's SHA256SUMS line, re-derived by hashing
1370// the downloaded asset (aegis-mecge2.12).
1371pub(crate) const CAMAYOC_VERSION: &str = "0.1.11";
1372const CAMAYOC_REVISION: &str = "78e59e036605b974f958781e611fdacfef47964f";
1373const CAMAYOC_ARCHIVE_SHA256: &str =
1374    "51309e02f6b0563a18e7e297bed711a2bd962681d46d2eb33cedbc46f0b03571";
1375
1376pub(crate) fn camayoc_root() -> Result<PathBuf> {
1377    if let Some(path) = env::var_os("CABOODLE_CAMAYOC_ROOT") {
1378        return Ok(PathBuf::from(path));
1379    }
1380    let home = env::var_os("HOME").context("HOME is required to install Camayoc")?;
1381    Ok(PathBuf::from(home)
1382        .join(".local/share/caboodle/camayoc")
1383        .join(CAMAYOC_REVISION))
1384}
1385
1386fn install_camayoc_bundle() -> Result<()> {
1387    let root = camayoc_root()?;
1388    if root.join("REVISION").exists() {
1389        return Ok(());
1390    }
1391    let download = tempfile::tempdir().context("create Camayoc download directory")?;
1392    let archive = download.path().join("camayoc.tar.gz");
1393    download_https(
1394        &format!(
1395            "https://github.com/intigraph/camayoc/releases/download/v{CAMAYOC_VERSION}/camayoc-v{CAMAYOC_VERSION}.tar.gz"
1396        ),
1397        &archive,
1398    )?;
1399    if crate::embedding::sha256_file(&archive)? != CAMAYOC_ARCHIVE_SHA256 {
1400        bail!("Camayoc archive checksum mismatch");
1401    }
1402    fs::create_dir_all(&root)
1403        .with_context(|| format!("create Camayoc install root {}", root.display()))?;
1404    checked(
1405        "tar",
1406        [
1407            OsStr::new("-xzf"),
1408            archive.as_os_str(),
1409            OsStr::new("--strip-components=1"),
1410            OsStr::new("-C"),
1411            root.as_os_str(),
1412        ],
1413        None,
1414    )?;
1415    fs::write(root.join("REVISION"), format!("{CAMAYOC_REVISION}\n"))
1416        .context("write installed Camayoc revision")?;
1417    Ok(())
1418}
1419
1420/// The `camayoc` CLIs a shell runs, held against the reviewed bundle (aegis-mecge2.12).
1421///
1422/// Tambo installs the Camayoc SOURCE BUNDLE and reads its version from the
1423/// bundle's own `REVISION` file. The `camayoc`/`cy` CLI is installed by other
1424/// means (`uv tool install`), so nothing compared it with the pin: on the Mac
1425/// `~/.local/bin/camayoc` answered 0.1.4 while `tambo update` printed
1426/// "camayoc: current" and the consumer recorded members-converged.
1427///
1428/// Probed: the first `camayoc` on `path`, and `<home>/.local/bin/camayoc`
1429/// (uv's tool bin) even when `path` omits it, because a launchd job's PATH
1430/// usually does and a stale copy there is still what a login shell runs. Each
1431/// probe must report `camayoc <CAMAYOC_VERSION>` (the CLI's real `--version`
1432/// line); any other release, or no readable version, is NOT converged.
1433///
1434/// Returns the probed paths; empty means no `camayoc` CLI was found at all.
1435pub fn camayoc_cli_check(path: Option<&OsStr>, home: Option<&Path>) -> Result<Vec<PathBuf>> {
1436    let mut probes: Vec<PathBuf> = which_all("camayoc", path).into_iter().take(1).collect();
1437    if let Some(uv_bin) = home.map(|home| home.join(".local/bin/camayoc")) {
1438        let same = |seen: &PathBuf| {
1439            seen.canonicalize()
1440                .ok()
1441                .is_some_and(|seen| uv_bin.canonicalize().ok() == Some(seen))
1442        };
1443        if is_executable(&uv_bin) && !probes.iter().any(same) {
1444            probes.push(uv_bin);
1445        }
1446    }
1447    for runs in &probes {
1448        let reported = runs
1449            .to_str()
1450            .context("camayoc CLI path is not UTF-8")
1451            .and_then(read_version);
1452        let reported = match reported {
1453            Ok(reported) => reported,
1454            Err(error) => bail!(
1455                "camayoc NOT converged: {} has an unreadable version ({error:#}); \
1456                 the reviewed Camayoc release is v{CAMAYOC_VERSION}",
1457                runs.display()
1458            ),
1459        };
1460        if camayoc_cli_version(&reported) != Some(CAMAYOC_VERSION) {
1461            bail!(
1462                "camayoc NOT converged: {} reports `{}`, but the reviewed Camayoc \
1463                 release is v{CAMAYOC_VERSION}. Tambo installs the Camayoc bundle, not this CLI; \
1464                 upgrade it (`uv tool install --force git+https://github.com/intigraph/camayoc@v{CAMAYOC_VERSION}`) \
1465                 or remove the stale copy",
1466                runs.display(),
1467                reported.trim()
1468            );
1469        }
1470    }
1471    Ok(probes)
1472}
1473
1474/// `camayoc --version` prints `camayoc 0.1.11` (argparse `%(prog)s <version>`).
1475fn camayoc_cli_version(reported: &str) -> Option<&str> {
1476    reported
1477        .lines()
1478        .next()?
1479        .trim()
1480        .strip_prefix("camayoc ")
1481        .map(|v| v.trim().trim_start_matches('v'))
1482}
1483
1484fn verify_camayoc_first_ingest(root: &Path, server: &str, auth: &Path) -> Result<()> {
1485    let namespace = camayoc_aegis_namespace(&root.join("ontology/core.ttl"))?;
1486    let control = "tambo-camayoc-control-must-stay-absent";
1487    let marker = "tambo-camayoc-first-ingest-v1";
1488
1489    if label_count(server, &namespace, control, auth)? != 0 {
1490        bail!("Camayoc negative control unexpectedly exists");
1491    }
1492
1493    let turtle = format!(
1494        "@prefix aegis: <{namespace}> .\n@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n\
1495         aegis:{marker} a aegis:Verification ; rdfs:label \"{marker}\" ; \
1496         aegis:sourceKind \"observed\" ; aegis:falsifier \"the marker remains absent after ingest\" .\n"
1497    );
1498    let payload = json!({
1499        "turtle": turtle,
1500        "actor": "caboodle",
1501        "source": "Tambo Camayoc first-ingest verification"
1502    });
1503    if label_count(server, &namespace, marker, auth)? == 0 {
1504        let first = curl_json_request_with_config(
1505            &format!("{server}/knot"),
1506            Some(payload.to_string()),
1507            Some(auth),
1508        )?;
1509        if first.get("count").and_then(Value::as_u64).unwrap_or(0) == 0 {
1510            bail!("Camayoc first ingest wrote no triples");
1511        }
1512    }
1513    if label_count(server, &namespace, marker, auth)? == 0 {
1514        bail!("Camayoc first ingest was not retrievable");
1515    }
1516    curl_json_request_with_config(
1517        &format!("{server}/knot"),
1518        Some(payload.to_string()),
1519        Some(auth),
1520    )?;
1521    if label_count(server, &namespace, marker, auth)? != 1 {
1522        bail!("Camayoc idempotent replay wrote duplicate triples");
1523    }
1524    Ok(())
1525}
1526
1527pub(crate) fn camayoc_aegis_namespace(path: &Path) -> Result<String> {
1528    let body = fs::read_to_string(path)
1529        .with_context(|| format!("read Camayoc ontology {}", path.display()))?;
1530    body.lines()
1531        .find_map(|line| {
1532            line.trim()
1533                .strip_prefix("@prefix aegis: <")
1534                .and_then(|rest| rest.strip_suffix("> ."))
1535                .map(str::to_owned)
1536        })
1537        .context("Camayoc ontology does not declare the aegis namespace")
1538}
1539
1540fn label_count(server: &str, namespace: &str, label: &str, auth: &Path) -> Result<u64> {
1541    let query = format!(
1542        "SELECT ?s WHERE {{ ?s <http://www.w3.org/2000/01/rdf-schema#label> \"{label}\" . FILTER(STRSTARTS(STR(?s), \"{namespace}\")) }}"
1543    );
1544    let response = curl_json_request_with_config(
1545        &format!("{server}/query"),
1546        Some(json!({"query": query}).to_string()),
1547        Some(auth),
1548    )?;
1549    response["count"]
1550        .as_u64()
1551        .context("Quipu query response omitted numeric count")
1552}
1553
1554pub(crate) fn curl_json(url: &str, payload: &Value) -> Result<Value> {
1555    curl_json_request(url, Some(payload.to_string()))
1556}
1557
1558fn curl_get_json(url: &str) -> Result<Value> {
1559    curl_json_request(url, None)
1560}
1561
1562/// The curl config line carrying `QUIPU_AUTH_TOKEN`, in a temp file so the token
1563/// never reaches argv. curl parses a quoted config value with backslash
1564/// escapes, so `\` and `"` are escaped; a newline would end the line and let
1565/// the token inject a second directive, so it is refused rather than escaped.
1566fn quipu_auth_config() -> Result<Option<tempfile::NamedTempFile>> {
1567    crate::quipu_auth::config()
1568}
1569
1570pub(crate) fn curl_auth_header_line(token: &str) -> Result<String> {
1571    if token.contains(['\n', '\r']) {
1572        bail!("QUIPU_AUTH_TOKEN contains a line break; refusing to pass it to curl");
1573    }
1574    let escaped = token.replace('\\', "\\\\").replace('"', "\\\"");
1575    Ok(format!("header = \"Authorization: Bearer {escaped}\"\n"))
1576}
1577
1578/// POST an empty body to `/episode` with the caller's token and return the HTTP
1579/// status and body. Quipu checks the bearer before it parses the episode, so an
1580/// accepted token yields a validation error (400) and NOTHING is written, while a
1581/// missing or rejected token yields 401. That makes it an authenticated no-op:
1582/// the only way to prove the write path without writing (aegis-0c1qdi). The
1583/// token travels in a curl config file, never on the command line.
1584pub(crate) fn quipu_write_probe(server: &str) -> Result<(u16, String, bool)> {
1585    let body_file = tempfile::NamedTempFile::new().context("create write-probe body file")?;
1586    let auth = quipu_auth_config()?;
1587    let mut args: Vec<OsString> = [
1588        "--silent",
1589        "--max-time",
1590        "5",
1591        "--request",
1592        "POST",
1593        "--header",
1594        "Content-Type: application/json",
1595        "--data",
1596        "{}",
1597        "--write-out",
1598        "%{http_code}",
1599        "--output",
1600    ]
1601    .iter()
1602    .map(OsString::from)
1603    .collect();
1604    args.push(body_file.path().as_os_str().to_owned());
1605    if let Some(file) = &auth {
1606        args.push(OsString::from("--config"));
1607        args.push(file.path().as_os_str().to_owned());
1608    }
1609    args.push(OsString::from(format!(
1610        "{}/episode",
1611        server.trim_end_matches('/')
1612    )));
1613    let result = output("curl", args, None)?;
1614    let code = String::from_utf8_lossy(&result.stdout)
1615        .trim()
1616        .parse::<u16>()
1617        .unwrap_or(0);
1618    let body = fs::read_to_string(body_file.path()).unwrap_or_default();
1619    Ok((code, body, auth.is_some()))
1620}
1621
1622fn curl_json_request(url: &str, body: Option<String>) -> Result<Value> {
1623    let auth = quipu_auth_config()?;
1624    curl_json_request_with_config(url, body, auth.as_ref().map(|file| file.path()))
1625}
1626
1627fn curl_json_request_with_config(
1628    url: &str,
1629    body: Option<String>,
1630    auth: Option<&Path>,
1631) -> Result<Value> {
1632    let mut args = vec![
1633        OsString::from("--fail"),
1634        OsString::from("--silent"),
1635        OsString::from("--show-error"),
1636    ];
1637    if let Some(body) = body {
1638        args.extend([
1639            OsString::from("--request"),
1640            OsString::from("POST"),
1641            OsString::from("--header"),
1642            OsString::from("Content-Type: application/json"),
1643            OsString::from("--data"),
1644            OsString::from(body),
1645        ]);
1646    }
1647    if let Some(file) = &auth {
1648        args.push(OsString::from("--config"));
1649        args.push(file.as_os_str().to_owned());
1650    }
1651    args.push(OsString::from(url));
1652    let result = checked("curl", args, None)?;
1653    serde_json::from_slice(&result.stdout)
1654        .with_context(|| format!("parse JSON response from {url}"))
1655}
1656
1657pub(crate) fn download_https(url: &str, destination: &Path) -> Result<()> {
1658    // Retry only the GET, never its checksum or installation step. A blanket
1659    // curl --retry-all-errors would also retry a missing asset or bad local path.
1660    const ATTEMPTS: u32 = 3;
1661    for attempt in 1..=ATTEMPTS {
1662        // A failed/partial response must not become the next attempt's input,
1663        // or replace a previously downloaded artifact when retries run out.
1664        let pending = tempfile::NamedTempFile::new_in(
1665            destination
1666                .parent()
1667                .filter(|p| !p.as_os_str().is_empty())
1668                .unwrap_or(Path::new(".")),
1669        )
1670        .context("create pending download")?;
1671        let result = output(
1672            "curl",
1673            [
1674                OsStr::new("--disable"),
1675                OsStr::new("--fail"),
1676                OsStr::new("--location"),
1677                OsStr::new("--silent"),
1678                OsStr::new("--show-error"),
1679                OsStr::new("--proto"),
1680                OsStr::new("=https"),
1681                OsStr::new("--tlsv1.2"),
1682                OsStr::new("--connect-timeout"),
1683                OsStr::new("10"),
1684                OsStr::new("--max-time"),
1685                OsStr::new("120"),
1686                OsStr::new("--write-out"),
1687                OsStr::new("%{http_code}"),
1688                OsStr::new("--output"),
1689                pending.path().as_os_str(),
1690                OsStr::new(url),
1691            ],
1692            None,
1693        )?;
1694        if result.status.success() {
1695            pending
1696                .persist(destination)
1697                .context("publish downloaded artifact")?;
1698            if attempt > 1 {
1699                eprintln!("download {url}: succeeded on attempt {attempt}/{ATTEMPTS}");
1700            }
1701            return Ok(());
1702        }
1703        let http = String::from_utf8_lossy(&result.stdout)
1704            .trim()
1705            .parse::<u16>()
1706            .unwrap_or(0);
1707        let detail = format!(
1708            "download {url}: attempt {attempt}/{ATTEMPTS}, curl {}, HTTP {http:03}: {}",
1709            result.status,
1710            String::from_utf8_lossy(&result.stderr).trim()
1711        );
1712        if !retryable_download(result.status.code(), http) {
1713            bail!("{detail}; terminal failure (not retried)");
1714        }
1715        if attempt == ATTEMPTS {
1716            bail!("{detail}; retry limit exhausted");
1717        }
1718        let delay = 1 << (attempt - 1);
1719        eprintln!("{detail}; retrying in {delay}s");
1720        drop(pending);
1721        std::thread::sleep(std::time::Duration::from_secs(delay));
1722    }
1723    unreachable!("every final attempt returns")
1724}
1725
1726fn retryable_download(code: Option<i32>, http: u16) -> bool {
1727    // An observed client error wins even when reading its body timed out.
1728    if (400..500).contains(&http) {
1729        return false;
1730    }
1731    match code {
1732        Some(22) => (500..600).contains(&http),
1733        // curl transport errors: proxy/DNS resolution, connect, partial body,
1734        // timeout, empty reply, send/receive and HTTP/2 stream failures.
1735        Some(5 | 6 | 7 | 18 | 28 | 52 | 55 | 56 | 92) => true,
1736        // Certificate, local I/O, invocation and unknown errors stay fatal.
1737        _ => false,
1738    }
1739}
1740
1741fn verify_checksum(archive: &Path, sums: &Path) -> Result<()> {
1742    let filename = archive
1743        .file_name()
1744        .and_then(OsStr::to_str)
1745        .context("bobbin archive filename is not UTF-8")?;
1746    let expected = fs::read_to_string(sums)
1747        .context("read Bobbin checksum manifest")?
1748        .lines()
1749        .find_map(|line| {
1750            let mut fields = line.split_whitespace();
1751            let digest = fields.next()?;
1752            let listed = fields.next()?.trim_start_matches('*');
1753            (listed == filename).then(|| digest.to_owned())
1754        })
1755        .with_context(|| format!("checksum manifest does not cover {filename}"))?;
1756    if crate::embedding::sha256_file(archive)? != expected {
1757        bail!("checksum mismatch for {filename}");
1758    }
1759    Ok(())
1760}
1761
1762#[cfg(test)]
1763mod yupana_release_tests {
1764    use super::*;
1765
1766    #[test]
1767    fn published_platforms_resolve_and_missing_platforms_refuse() {
1768        for (arch, os, target) in [
1769            ("x86_64", "linux", "x86_64-linux-gnu"),
1770            ("aarch64", "macos", "aarch64-apple-darwin"),
1771            ("x86_64", "macos", "x86_64-apple-darwin"),
1772        ] {
1773            assert_eq!(yupana_release_for(arch, os).unwrap().0, target);
1774        }
1775        assert!(yupana_release_for("aarch64", "linux").is_err());
1776        assert!(yupana_release_for("x86_64", "windows").is_err());
1777        assert!(!prerequisites(ToolName::Yupana, QuipuFlavor::Release).contains(&"sha256sum"));
1778    }
1779
1780    #[test]
1781    fn checksum_refuses_corruption_without_an_external_hash_command() {
1782        let root = tempfile::tempdir().unwrap();
1783        let file = root.path().join("archive");
1784        fs::write(&file, b"abc").unwrap();
1785        let expected = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
1786        verify_yupana_checksum(&file, expected).unwrap();
1787        fs::write(&file, b"abd").unwrap();
1788        assert!(verify_yupana_checksum(&file, expected).is_err());
1789    }
1790
1791    // aegis-em2.2: on the Mac the consumer LaunchAgent's PATH has no /sbin,
1792    // where macOS keeps sha256sum, so every Camayoc install failed there.
1793    #[test]
1794    fn camayoc_needs_no_external_hash_command() {
1795        for flavor in [QuipuFlavor::Release, QuipuFlavor::Lancedb] {
1796            assert!(!prerequisites(ToolName::Camayoc, flavor).contains(&"sha256sum"));
1797        }
1798    }
1799
1800    /// Runs in a child copy of this test binary whose PATH is EMPTY, so no
1801    /// external hash command can be found; env mutation in-process would race
1802    /// the other tests (aegis-em2.2).
1803    #[test]
1804    fn checksums_verify_and_refuse_with_an_empty_path() {
1805        const CHILD: &str = "TAMBO_EMPTY_PATH_CHECKSUM_CHILD";
1806        if env::var_os(CHILD).is_some() {
1807            assert_eq!(env::var_os("PATH").as_deref(), Some(OsStr::new("")));
1808            let root = tempfile::tempdir().unwrap();
1809            let archive = root.path().join("bobbin.tar.gz");
1810            let sums = root.path().join("SHA256SUMS");
1811            let abc = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
1812            fs::write(&archive, b"abc").unwrap();
1813            fs::write(&sums, format!("{abc}  bobbin.tar.gz\n")).unwrap();
1814            verify_checksum(&archive, &sums).unwrap();
1815            verify_yupana_checksum(&archive, abc).unwrap();
1816            fs::write(&archive, b"abd").unwrap();
1817            let mismatch = verify_checksum(&archive, &sums).unwrap_err();
1818            assert!(
1819                format!("{mismatch:#}").contains("checksum mismatch"),
1820                "{mismatch:#}"
1821            );
1822            assert!(verify_yupana_checksum(&archive, abc).is_err());
1823            return;
1824        }
1825        let output = Command::new(env::current_exe().unwrap())
1826            .args([
1827                "--exact",
1828                "adapter::yupana_release_tests::checksums_verify_and_refuse_with_an_empty_path",
1829                "--nocapture",
1830                "--test-threads=1",
1831            ])
1832            .env("PATH", "")
1833            .env(CHILD, "1")
1834            .output()
1835            .unwrap();
1836        let stdout = String::from_utf8_lossy(&output.stdout);
1837        assert!(
1838            output.status.success() && stdout.contains("1 passed"),
1839            "empty-PATH child failed:\n{stdout}\n{}",
1840            String::from_utf8_lossy(&output.stderr)
1841        );
1842    }
1843
1844    #[test]
1845    fn manifest_checksum_is_computed_in_process() {
1846        let root = tempfile::tempdir().unwrap();
1847        let archive = root.path().join("bobbin.tar.gz");
1848        let sums = root.path().join("SHA256SUMS");
1849        fs::write(&archive, b"abc").unwrap();
1850        fs::write(
1851            &sums,
1852            "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad  bobbin.tar.gz\n",
1853        )
1854        .unwrap();
1855        verify_checksum(&archive, &sums).unwrap();
1856        fs::write(&archive, b"abd").unwrap();
1857        assert!(verify_checksum(&archive, &sums).is_err());
1858    }
1859
1860    #[test]
1861    #[ignore = "downloads all three published archives; explicit release acceptance"]
1862    fn real_yupana_archives_match_pins_and_unpack() {
1863        for (arch, os) in [
1864            ("x86_64", "linux"),
1865            ("aarch64", "macos"),
1866            ("x86_64", "macos"),
1867        ] {
1868            let (target, checksum) = yupana_release_for(arch, os).unwrap();
1869            let root = tempfile::tempdir().unwrap();
1870            fetch_yupana_release(target, checksum, root.path()).unwrap();
1871            assert!(root.path().join("yupana").is_file());
1872            assert_eq!(
1873                fs::read_link(root.path().join("hank")).unwrap(),
1874                Path::new("yupana")
1875            );
1876            eprintln!("verified published {target}: {checksum}");
1877        }
1878    }
1879}
1880
1881#[cfg(test)]
1882mod flavor_tests {
1883    use super::{
1884        fetch_quipu_release, quipu_install_args, quipu_release_target, quipu_release_target_for,
1885        require_compiled_feature,
1886    };
1887    use crate::model::QuipuFlavor;
1888    use serde_json::json;
1889
1890    #[test]
1891    fn install_args_add_only_the_lancedb_feature_to_the_reviewed_pin() {
1892        let release = quipu_install_args(QuipuFlavor::Release);
1893        let lancedb = quipu_install_args(QuipuFlavor::Lancedb);
1894        assert_eq!(release[6], "full");
1895        assert_eq!(lancedb[6], "full,lancedb");
1896        // Everything but the feature list must be identical, so the lancedb
1897        // flavor stays pinned to the same reviewed revision.
1898        assert_eq!(release[..6], lancedb[..6]);
1899        assert_eq!(release[7..], lancedb[7..]);
1900        assert!(release.contains(&"--locked"));
1901    }
1902
1903    #[test]
1904    fn default_quipu_release_has_a_supported_host_artifact() {
1905        if (std::env::consts::ARCH, std::env::consts::OS) == ("x86_64", "linux") {
1906            assert_eq!(quipu_release_target().unwrap(), "x86_64-unknown-linux-gnu");
1907        }
1908    }
1909
1910    #[test]
1911    fn quipu_release_supports_published_linux_and_macos_architectures() {
1912        for (arch, os, target) in [
1913            ("x86_64", "linux", "x86_64-unknown-linux-gnu"),
1914            ("aarch64", "linux", "aarch64-unknown-linux-gnu"),
1915            ("x86_64", "macos", "x86_64-apple-darwin"),
1916            ("aarch64", "macos", "aarch64-apple-darwin"),
1917        ] {
1918            assert_eq!(quipu_release_target_for(arch, os).unwrap(), target);
1919        }
1920        assert!(quipu_release_target_for("x86_64", "windows").is_err());
1921    }
1922
1923    #[test]
1924    #[ignore = "downloads published artifacts; explicit release acceptance only"]
1925    fn real_aarch64_quipu_release_resolves_and_verifies() {
1926        for os in ["macos", "linux"] {
1927            let target = quipu_release_target_for("aarch64", os).unwrap();
1928            let download = tempfile::tempdir().unwrap();
1929            let unpacked = fetch_quipu_release(target, download.path()).unwrap();
1930            assert!(unpacked.join("quipu").is_file());
1931            assert!(unpacked.join("quipu-server").is_file());
1932            eprintln!("verified published Quipu checksums and both binaries for {target}");
1933        }
1934    }
1935
1936    #[test]
1937    fn compile_map_proof_requires_a_true_lancedb_entry() {
1938        require_compiled_feature(&json!({"features": {"lancedb": true}}), "lancedb").unwrap();
1939
1940        let absent = require_compiled_feature(&json!({"features": {"onnx": true}}), "lancedb")
1941            .unwrap_err()
1942            .to_string();
1943        assert!(absent.contains("compiled without it"), "{absent}");
1944
1945        let disabled =
1946            require_compiled_feature(&json!({"features": {"lancedb": false}}), "lancedb")
1947                .unwrap_err()
1948                .to_string();
1949        assert!(disabled.contains("compiled without it"), "{disabled}");
1950
1951        // A server too old to report the map proves nothing; that is a
1952        // refusal, not a pass.
1953        let unmapped = require_compiled_feature(&json!({"version": "0.3.27"}), "lancedb")
1954            .unwrap_err()
1955            .to_string();
1956        assert!(
1957            unmapped.contains("no per-feature compile map"),
1958            "{unmapped}"
1959        );
1960    }
1961}
1962
1963#[cfg(all(test, unix))]
1964mod tests {
1965    use super::ensure_bobbin_link;
1966    use std::{fs, os::unix::fs::symlink};
1967
1968    fn fake_camayoc(dir: &std::path::Path, script: &str) {
1969        use std::os::unix::fs::PermissionsExt;
1970        let program = dir.join("camayoc");
1971        fs::write(&program, script).unwrap();
1972        fs::set_permissions(&program, fs::Permissions::from_mode(0o755)).unwrap();
1973    }
1974
1975    // aegis-mecge2.12: the Mac's ~/.local/bin/camayoc answered 0.1.4 while the
1976    // bundle REVISION matched, and Tambo called that converged.
1977    #[test]
1978    fn stale_camayoc_cli_on_path_is_not_converged() {
1979        let dir = tempfile::tempdir().unwrap();
1980        fake_camayoc(dir.path(), "#!/bin/sh\necho 'camayoc 0.1.4'\n");
1981        let error = super::camayoc_cli_check(Some(dir.path().as_os_str()), None)
1982            .unwrap_err()
1983            .to_string();
1984        assert!(error.contains("camayoc NOT converged"), "{error}");
1985        assert!(error.contains("camayoc 0.1.4"), "{error}");
1986        assert!(
1987            error.contains(&format!("v{}", super::CAMAYOC_VERSION)),
1988            "{error}"
1989        );
1990    }
1991
1992    // The launchd consumer's PATH omits ~/.local/bin; the stale uv copy there
1993    // must still fail the check rather than read as "absent".
1994    #[test]
1995    fn stale_uv_camayoc_off_path_is_not_converged() {
1996        let home = tempfile::tempdir().unwrap();
1997        let uv_bin = home.path().join(".local/bin");
1998        fs::create_dir_all(&uv_bin).unwrap();
1999        fake_camayoc(&uv_bin, "#!/bin/sh\necho 'camayoc 0.1.4'\n");
2000        let empty = tempfile::tempdir().unwrap();
2001        let error = super::camayoc_cli_check(Some(empty.path().as_os_str()), Some(home.path()))
2002            .unwrap_err()
2003            .to_string();
2004        assert!(error.contains("camayoc NOT converged"), "{error}");
2005        assert!(error.contains(".local/bin/camayoc"), "{error}");
2006    }
2007
2008    // A current CLI first on PATH does not hide a stale uv copy.
2009    #[test]
2010    fn current_path_camayoc_does_not_hide_stale_uv_copy() {
2011        let home = tempfile::tempdir().unwrap();
2012        let uv_bin = home.path().join(".local/bin");
2013        fs::create_dir_all(&uv_bin).unwrap();
2014        fake_camayoc(&uv_bin, "#!/bin/sh\necho 'camayoc 0.1.4'\n");
2015        let path_dir = tempfile::tempdir().unwrap();
2016        fake_camayoc(
2017            path_dir.path(),
2018            &format!("#!/bin/sh\necho 'camayoc {}'\n", super::CAMAYOC_VERSION),
2019        );
2020        assert!(
2021            super::camayoc_cli_check(Some(path_dir.path().as_os_str()), Some(home.path())).is_err()
2022        );
2023    }
2024
2025    #[test]
2026    fn reviewed_camayoc_cli_is_converged_once_when_path_and_uv_agree() {
2027        let home = tempfile::tempdir().unwrap();
2028        let uv_bin = home.path().join(".local/bin");
2029        fs::create_dir_all(&uv_bin).unwrap();
2030        // The exact line the released v0.1.11 CLI prints for --version.
2031        fake_camayoc(&uv_bin, "#!/bin/sh\necho 'camayoc 0.1.11'\n");
2032        assert_eq!(super::CAMAYOC_VERSION, "0.1.11");
2033        let runs = super::camayoc_cli_check(Some(uv_bin.as_os_str()), Some(home.path())).unwrap();
2034        assert_eq!(runs, vec![uv_bin.join("camayoc")]);
2035    }
2036
2037    #[test]
2038    fn unreadable_camayoc_cli_version_is_not_converged() {
2039        let dir = tempfile::tempdir().unwrap();
2040        fake_camayoc(dir.path(), "#!/bin/sh\nexit 3\n");
2041        let error = super::camayoc_cli_check(Some(dir.path().as_os_str()), None)
2042            .unwrap_err()
2043            .to_string();
2044        assert!(error.contains("unreadable version"), "{error}");
2045    }
2046
2047    #[test]
2048    fn absent_camayoc_cli_is_only_a_note() {
2049        let dir = tempfile::tempdir().unwrap();
2050        let home = tempfile::tempdir().unwrap();
2051        assert!(
2052            super::camayoc_cli_check(Some(dir.path().as_os_str()), Some(home.path()))
2053                .unwrap()
2054                .is_empty()
2055        );
2056    }
2057
2058    #[test]
2059    fn failed_commands_preserve_both_diagnostic_streams_and_status() {
2060        for script in [
2061            "printf 'bootstrap rejected'; exit 2",
2062            "printf 'bootstrap rejected' >&2; exit 2",
2063            "printf 'probe started'; printf 'bootstrap rejected' >&2; exit 2",
2064            "exit 2",
2065        ] {
2066            let error = super::checked("sh", ["-c", script], None)
2067                .unwrap_err()
2068                .to_string();
2069            assert!(error.contains("exit status: 2"), "{error}");
2070            assert!(error.contains("stdout:\n"), "{error}");
2071            assert!(error.contains("stderr:\n"), "{error}");
2072            if script.contains("bootstrap rejected") {
2073                assert!(error.contains("bootstrap rejected"), "{error}");
2074            }
2075            if script.contains("probe started") {
2076                assert!(error.contains("probe started"), "{error}");
2077            }
2078        }
2079        let output = super::checked("sh", ["-c", "printf 'successful output'"], None).unwrap();
2080        assert_eq!(output.stdout, b"successful output");
2081    }
2082
2083    #[test]
2084    fn bobbin_link_accepts_its_restored_cache_entry_but_refuses_foreign_paths() {
2085        let root = tempfile::tempdir().unwrap();
2086        let target = root.path().join("release/bobbin");
2087        fs::create_dir_all(target.parent().unwrap()).unwrap();
2088        fs::write(&target, "binary fixture").unwrap();
2089
2090        let link = root.path().join("bin/bobbin");
2091        fs::create_dir_all(link.parent().unwrap()).unwrap();
2092        symlink(&target, &link).unwrap();
2093        ensure_bobbin_link(&link, &target).unwrap();
2094        assert_eq!(fs::read_link(&link).unwrap(), target);
2095
2096        let foreign = root.path().join("foreign");
2097        fs::write(&foreign, "do not replace").unwrap();
2098        fs::remove_file(&link).unwrap();
2099        symlink(&foreign, &link).unwrap();
2100        let error = ensure_bobbin_link(&link, &target).unwrap_err().to_string();
2101        assert!(error.contains("refusing to replace existing"));
2102        assert_eq!(fs::read_link(&link).unwrap(), foreign);
2103    }
2104
2105    #[test]
2106    fn bobbin_link_repairs_its_own_dangling_link_but_still_refuses_a_real_file() {
2107        // The CI deadlock, both arms. A dangling link makes `bobbin --version`
2108        // fail with ENOENT, so the probe reports "not installed" and calls the
2109        // installer -- which then refused to touch it. Never present, never
2110        // writable, and no re-run could break the tie.
2111        let root = tempfile::tempdir().unwrap();
2112        let target = root.path().join("release/bobbin");
2113        fs::create_dir_all(target.parent().unwrap()).unwrap();
2114        fs::write(&target, "binary fixture").unwrap();
2115        let link = root.path().join("bin/bobbin");
2116        fs::create_dir_all(link.parent().unwrap()).unwrap();
2117
2118        symlink(root.path().join("gone/bobbin"), &link).unwrap();
2119        assert!(link.symlink_metadata().is_ok(), "the link itself exists");
2120        assert!(
2121            !link.exists(),
2122            "but it resolves to nothing -- this is the ENOENT"
2123        );
2124        ensure_bobbin_link(&link, &target).unwrap();
2125        assert_eq!(fs::read_link(&link).unwrap(), target);
2126
2127        // and the repair must not have widened into "replace whatever is there":
2128        // a REGULAR FILE is somebody's binary and is still refused.
2129        fs::remove_file(&link).unwrap();
2130        fs::write(&link, "a real bobbin somebody installed").unwrap();
2131        let error = ensure_bobbin_link(&link, &target).unwrap_err().to_string();
2132        assert!(error.contains("refusing to replace existing"));
2133        assert_eq!(
2134            fs::read_to_string(&link).unwrap(),
2135            "a real bobbin somebody installed"
2136        );
2137    }
2138}
2139
2140#[cfg(test)]
2141mod auth_config_tests {
2142    use super::curl_auth_header_line;
2143
2144    #[test]
2145    fn curl_auth_line_escapes_quotes_and_refuses_line_breaks() {
2146        assert_eq!(
2147            curl_auth_header_line("abc").unwrap(),
2148            "header = \"Authorization: Bearer abc\"\n"
2149        );
2150        assert_eq!(
2151            curl_auth_header_line(r#"a"b\c"#).unwrap(),
2152            "header = \"Authorization: Bearer a\\\"b\\\\c\"\n"
2153        );
2154        // A newline would end the config line and let the token add a directive.
2155        assert!(curl_auth_header_line("tok\nurl = \"http://elsewhere\"").is_err());
2156        assert!(curl_auth_header_line("tok\r").is_err());
2157    }
2158}
2159
2160/// Verify one embedded release member without installing it.
2161pub fn verify_member(name: &str) -> Result<()> {
2162    let member = crate::members::get(name).context("unknown reviewed member")?;
2163    manifest::verify_release_member(member)
2164}
2165
2166#[cfg(all(test, unix))]
2167mod quipu_alias_layout_tests {
2168    use super::verify_quipu_alias;
2169    use std::os::unix::fs::symlink;
2170    use std::{fs, path::Path};
2171
2172    fn script(path: &Path, version: &str) {
2173        crate::test_exec::write_executable(path, &format!("#!/bin/sh\necho '{version}'\n"));
2174    }
2175
2176    #[test]
2177    fn package_managed_layout_passes_with_qp_beside_the_resolved_quipu() {
2178        // Homebrew: bin/quipu and bin/qp link into the Cellar, where the
2179        // formula owns the relative qp -> quipu.
2180        let root = tempfile::tempdir().unwrap();
2181        let cellar = root.path().join("Cellar/quipu/1.0/bin");
2182        let bin = root.path().join("bin");
2183        fs::create_dir_all(&cellar).unwrap();
2184        fs::create_dir(&bin).unwrap();
2185        script(&cellar.join("quipu"), "quipu 1.0");
2186        symlink("quipu", cellar.join("qp")).unwrap();
2187        symlink("../Cellar/quipu/1.0/bin/quipu", bin.join("quipu")).unwrap();
2188        symlink("../Cellar/quipu/1.0/bin/qp", bin.join("qp")).unwrap();
2189        verify_quipu_alias(&bin.join("quipu")).unwrap();
2190    }
2191
2192    #[test]
2193    fn a_qp_for_another_program_or_no_qp_still_fails() {
2194        let root = tempfile::tempdir().unwrap();
2195        let bin = root.path().join("bin");
2196        fs::create_dir(&bin).unwrap();
2197        script(&bin.join("quipu"), "quipu 1.0");
2198        let err = verify_quipu_alias(&bin.join("quipu")).unwrap_err();
2199        assert!(err.to_string().contains("requires relative qp -> quipu"));
2200        // A relative link exists beside the resolved file, but the qp on PATH
2201        // is a different program: refuse.
2202        let cellar = root.path().join("Cellar/bin");
2203        fs::create_dir_all(&cellar).unwrap();
2204        script(&cellar.join("quipu"), "quipu 1.0");
2205        symlink("quipu", cellar.join("qp")).unwrap();
2206        script(&root.path().join("other"), "quipu 1.0");
2207        let pkg = root.path().join("pkgbin");
2208        fs::create_dir(&pkg).unwrap();
2209        symlink("../Cellar/bin/quipu", pkg.join("quipu")).unwrap();
2210        symlink("../other", pkg.join("qp")).unwrap();
2211        let err = verify_quipu_alias(&pkg.join("quipu")).unwrap_err();
2212        assert!(err.to_string().contains("same executable"));
2213    }
2214
2215    #[test]
2216    fn owned_layout_with_qp_beside_quipu_still_passes() {
2217        let root = tempfile::tempdir().unwrap();
2218        script(&root.path().join("quipu"), "quipu 1.0");
2219        symlink("quipu", root.path().join("qp")).unwrap();
2220        verify_quipu_alias(&root.path().join("quipu")).unwrap();
2221    }
2222}