|
| 1 | +//! The catalog's own declaration — `<catalog>/catalog.kdl`. |
| 2 | +//! |
| 3 | +//! Every other file in a catalog describes an agent; this one describes the folder. Only the session |
| 4 | +//! registry is declarable today: |
| 5 | +//! |
| 6 | +//! ```kdl |
| 7 | +//! catalog { |
| 8 | +//! pty-root "/run/agents/pty" |
| 9 | +//! } |
| 10 | +//! ``` |
| 11 | +//! |
| 12 | +//! It is deliberately not a spec. `catalog` is not an `agent` node, so discovery lowers nothing from |
| 13 | +//! it, and `eval_spec::parse_spec` rejects `catalog` as a top-level node, so a catalog that declares |
| 14 | +//! a root is still dispatched as a catalog and never mistaken for a single-file team spec. |
| 15 | +
|
| 16 | +use std::path::{Path, PathBuf}; |
| 17 | + |
| 18 | +use kdl::KdlDocument; |
| 19 | + |
| 20 | +/// The catalog-level declaration, read from the catalog root. |
| 21 | +pub const CONFIG_FILE: &str = "catalog.kdl"; |
| 22 | + |
| 23 | +/// What `<catalog>/catalog.kdl` declares. An absent file leaves every field `None`. |
| 24 | +#[derive(Debug, Default, Clone, PartialEq, Eq)] |
| 25 | +pub struct CatalogConfig { |
| 26 | + /// The `pty` session registry holding this catalog's tasks. Relative values anchor at the |
| 27 | + /// catalog root; `$VAR`/`$CATALOG` are expanded at use. |
| 28 | + pub pty_root: Option<String>, |
| 29 | +} |
| 30 | + |
| 31 | +/// `<catalog>/catalog.kdl`. |
| 32 | +pub fn config_path(catalog_root: &Path) -> PathBuf { |
| 33 | + catalog_root.join(CONFIG_FILE) |
| 34 | +} |
| 35 | + |
| 36 | +/// Parse a catalog declaration. |
| 37 | +/// |
| 38 | +/// An unknown child of `catalog{}` is an error rather than ignored: a typo'd `pty_root` would |
| 39 | +/// silently resolve back to `<catalog>/pty` and reappear as a live agent whose task reads dead — |
| 40 | +/// exactly the split registry this declaration exists to prevent. Its value set is closed, so the |
| 41 | +/// lint cannot fire on a render-only field st2 ignores by design. |
| 42 | +/// |
| 43 | +/// Top-level nodes other than `catalog` are left alone: the same file may legitimately hold `agent` |
| 44 | +/// nodes, which discovery owns. |
| 45 | +pub fn parse(text: &str) -> anyhow::Result<CatalogConfig> { |
| 46 | + let doc = KdlDocument::parse(text).map_err(|e| anyhow::anyhow!("KDL parse error: {e}"))?; |
| 47 | + let mut config = CatalogConfig::default(); |
| 48 | + let mut seen = false; |
| 49 | + |
| 50 | + for node in doc.nodes().iter().filter(|n| n.name().value() == "catalog") { |
| 51 | + if seen { |
| 52 | + anyhow::bail!("catalog block declared more than once"); |
| 53 | + } |
| 54 | + seen = true; |
| 55 | + let Some(children) = node.children() else { |
| 56 | + continue; |
| 57 | + }; |
| 58 | + for child in children.nodes() { |
| 59 | + match child.name().value() { |
| 60 | + "pty-root" => { |
| 61 | + let value = child |
| 62 | + .get(0) |
| 63 | + .and_then(|v| v.as_string()) |
| 64 | + .filter(|v| !v.is_empty()) |
| 65 | + .ok_or_else(|| { |
| 66 | + anyhow::anyhow!("pty-root needs a non-empty path, e.g. pty-root \"/run/agents/pty\"") |
| 67 | + })?; |
| 68 | + config.pty_root = Some(value.to_string()); |
| 69 | + } |
| 70 | + other => anyhow::bail!("unknown catalog field '{other}' (expected pty-root)"), |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + Ok(config) |
| 75 | +} |
| 76 | + |
| 77 | +/// Read `<catalog>/catalog.kdl`. A missing file is the default declaration, not an error. |
| 78 | +pub fn load(catalog_root: &Path) -> anyhow::Result<CatalogConfig> { |
| 79 | + match std::fs::read_to_string(config_path(catalog_root)) { |
| 80 | + Ok(text) => parse(&text), |
| 81 | + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(CatalogConfig::default()), |
| 82 | + Err(e) => Err(e.into()), |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/// The session registry the CATALOG itself declares: `pty-root` if it declares one, else the native |
| 87 | +/// `<catalog>/pty`. This is what `st2 env`/`st2 pty`/`st2 shell` hand to bus-aware tools, so those |
| 88 | +/// describe the catalog rather than whatever registry the caller happens to be standing in. |
| 89 | +/// |
| 90 | +/// A malformed declaration falls back to the default instead of failing: this runs on every spawn, |
| 91 | +/// list, and kill, including teardown, and inventing a root there is worse than using the native |
| 92 | +/// one. `st2 validate` is where a bad declaration is reported. |
| 93 | +pub fn pty_root(catalog_root: &Path) -> PathBuf { |
| 94 | + match load(catalog_root).ok().and_then(|c| c.pty_root) { |
| 95 | + // Join, so a relative declaration anchors at the catalog instead of the caller's cwd. |
| 96 | + Some(declared) => catalog_root.join(crate::expand::expand_catalog(&declared, catalog_root)), |
| 97 | + None => catalog_root.join("pty"), |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#[cfg(test)] |
| 102 | +mod tests { |
| 103 | + use super::*; |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn an_undeclared_catalog_keeps_the_native_root() { |
| 107 | + let tmp = tempfile::tempdir().unwrap(); |
| 108 | + assert_eq!(load(tmp.path()).unwrap(), CatalogConfig::default()); |
| 109 | + assert_eq!(pty_root(tmp.path()), tmp.path().join("pty")); |
| 110 | + |
| 111 | + // A file that declares other things, but no pty root. |
| 112 | + std::fs::write(config_path(tmp.path()), "agent \"a\" { command \"true\" }\n").unwrap(); |
| 113 | + assert_eq!(pty_root(tmp.path()), tmp.path().join("pty")); |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn a_declared_root_is_expanded_and_anchored_at_the_catalog() { |
| 118 | + let tmp = tempfile::tempdir().unwrap(); |
| 119 | + std::fs::write( |
| 120 | + config_path(tmp.path()), |
| 121 | + "catalog {\n pty-root \"/run/agents/pty\"\n}\n", |
| 122 | + ) |
| 123 | + .unwrap(); |
| 124 | + assert_eq!(pty_root(tmp.path()), PathBuf::from("/run/agents/pty")); |
| 125 | + |
| 126 | + std::fs::write(config_path(tmp.path()), "catalog { pty-root \"$CATALOG/../shared\" }\n") |
| 127 | + .unwrap(); |
| 128 | + assert_eq!(pty_root(tmp.path()), tmp.path().join("../shared")); |
| 129 | + |
| 130 | + // A relative value belongs to the catalog, never to the caller's cwd. |
| 131 | + std::fs::write(config_path(tmp.path()), "catalog { pty-root \"registry\" }\n").unwrap(); |
| 132 | + assert_eq!(pty_root(tmp.path()), tmp.path().join("registry")); |
| 133 | + } |
| 134 | + |
| 135 | + #[test] |
| 136 | + fn a_mistyped_declaration_is_an_error_not_a_silent_default() { |
| 137 | + assert!(parse("catalog { pty_root \"/run/agents/pty\" }").is_err()); |
| 138 | + assert!(parse("catalog { pty-root }").is_err()); |
| 139 | + assert!(parse("catalog { pty-root \"\" }").is_err()); |
| 140 | + assert!(parse("catalog { pty-root \"/a\" }\ncatalog { pty-root \"/b\" }").is_err()); |
| 141 | + assert!(parse("this is (not kdl").is_err()); |
| 142 | + |
| 143 | + // Reported by `st2 validate`; the runtime path stays on the native root. |
| 144 | + let tmp = tempfile::tempdir().unwrap(); |
| 145 | + std::fs::write(config_path(tmp.path()), "catalog { pty_root \"/run/agents/pty\" }\n") |
| 146 | + .unwrap(); |
| 147 | + assert_eq!(pty_root(tmp.path()), tmp.path().join("pty")); |
| 148 | + } |
| 149 | +} |
0 commit comments