Skip to main content

domainforge_core/projection/cell/
profiles.rs

1//! Opinionated GodSpeed cell-environment profiles: versioned data, not
2//! renderer conditionals. `CellIr::from_ast` looks a profile up by id and
3//! seeds every collection from it before overlaying explicit SEA
4//! declarations (see `ir.rs`).
5
6/// A pinned tool/runtime default (`mise install`-able name -> version).
7#[derive(Debug, Clone, Copy)]
8pub struct PinnedTool {
9    pub name: &'static str,
10    pub version: &'static str,
11}
12
13/// A default ecosystem package with no required version (Devbox resolves
14/// `name@latest`).
15#[derive(Debug, Clone, Copy)]
16pub struct SystemDep {
17    pub name: &'static str,
18    pub version: Option<&'static str>,
19}
20
21/// The native dependency-closure contract a profile assumes by default.
22#[derive(Debug, Clone, Copy)]
23pub struct DependencyContract {
24    pub ecosystem: &'static str,
25    pub manifest: &'static str,
26    pub lockfile: &'static str,
27    pub install: &'static str,
28}
29
30#[derive(Debug, Clone)]
31pub struct CellProfileData {
32    pub id: &'static str,
33    pub version: &'static str,
34    pub system_dependencies: Vec<SystemDep>,
35    pub runtimes: Vec<PinnedTool>,
36    pub tools: Vec<PinnedTool>,
37    pub dependency_contract: Option<DependencyContract>,
38}
39
40// `Vec::new()` is not const, so the profile bodies are built by functions
41// rather than const items (the alternative — const arrays converted with
42// `.to_vec()` at lookup time — reads worse for four profiles this small).
43
44fn python_agent_v1() -> CellProfileData {
45    CellProfileData {
46        id: "python-agent-v1",
47        version: "1.0.0",
48        system_dependencies: vec![
49            SystemDep {
50                name: "git",
51                version: None,
52            },
53            SystemDep {
54                name: "ca-certificates",
55                version: None,
56            },
57            SystemDep {
58                name: "openssl",
59                version: None,
60            },
61            SystemDep {
62                name: "pkg-config",
63                version: None,
64            },
65            SystemDep {
66                name: "curl",
67                version: None,
68            },
69        ],
70        runtimes: vec![PinnedTool {
71            name: "python",
72            version: "3.13",
73        }],
74        tools: vec![
75            PinnedTool {
76                name: "uv",
77                version: "0.9.8",
78            },
79            PinnedTool {
80                name: "just",
81                version: "1.43.0",
82            },
83        ],
84        dependency_contract: Some(DependencyContract {
85            ecosystem: "python",
86            manifest: "pyproject.toml",
87            lockfile: "uv.lock",
88            install: "uv sync --frozen",
89        }),
90    }
91}
92
93fn typescript_agent_v1() -> CellProfileData {
94    CellProfileData {
95        id: "typescript-agent-v1",
96        version: "1.0.0",
97        system_dependencies: vec![
98            SystemDep {
99                name: "git",
100                version: None,
101            },
102            SystemDep {
103                name: "ca-certificates",
104                version: None,
105            },
106            SystemDep {
107                name: "openssl",
108                version: None,
109            },
110            SystemDep {
111                name: "curl",
112                version: None,
113            },
114        ],
115        runtimes: vec![PinnedTool {
116            name: "node",
117            version: "24",
118        }],
119        tools: vec![
120            PinnedTool {
121                name: "pnpm",
122                version: "9.15.0",
123            },
124            PinnedTool {
125                name: "just",
126                version: "1.43.0",
127            },
128        ],
129        dependency_contract: Some(DependencyContract {
130            ecosystem: "typescript",
131            manifest: "package.json",
132            lockfile: "pnpm-lock.yaml",
133            install: "pnpm install --frozen-lockfile",
134        }),
135    }
136}
137
138fn rust_agent_v1() -> CellProfileData {
139    CellProfileData {
140        id: "rust-agent-v1",
141        version: "1.0.0",
142        system_dependencies: vec![
143            SystemDep {
144                name: "git",
145                version: None,
146            },
147            SystemDep {
148                name: "ca-certificates",
149                version: None,
150            },
151            SystemDep {
152                name: "openssl",
153                version: None,
154            },
155            SystemDep {
156                name: "pkg-config",
157                version: None,
158            },
159            SystemDep {
160                name: "clang",
161                version: None,
162            },
163            SystemDep {
164                name: "lld",
165                version: None,
166            },
167        ],
168        runtimes: vec![PinnedTool {
169            name: "rust",
170            version: "1.92.0",
171        }],
172        tools: vec![
173            PinnedTool {
174                name: "cargo-nextest",
175                version: "0.9.100",
176            },
177            PinnedTool {
178                name: "just",
179                version: "1.43.0",
180            },
181        ],
182        dependency_contract: Some(DependencyContract {
183            ecosystem: "rust",
184            manifest: "Cargo.toml",
185            lockfile: "Cargo.lock",
186            install: "cargo fetch --locked",
187        }),
188    }
189}
190
191/// Deterministic union of python/typescript/rust: dedupe by (category, name);
192/// on a version disagreement within the union the higher pinned version wins
193/// (profile-data composition, not user-facing override merging).
194fn polyglot_agent_v1() -> CellProfileData {
195    fn merge_tools(mut acc: Vec<PinnedTool>, incoming: Vec<PinnedTool>) -> Vec<PinnedTool> {
196        for tool in incoming {
197            match acc.iter_mut().find(|t| t.name == tool.name) {
198                Some(existing) if version_key(tool.version) > version_key(existing.version) => {
199                    existing.version = tool.version;
200                }
201                Some(_) => {}
202                None => acc.push(tool),
203            }
204        }
205        acc
206    }
207    fn merge_deps(mut acc: Vec<SystemDep>, incoming: Vec<SystemDep>) -> Vec<SystemDep> {
208        for dep in incoming {
209            if !acc.iter().any(|d| d.name == dep.name) {
210                acc.push(dep);
211            }
212        }
213        acc
214    }
215
216    let (py, ts, rs) = (python_agent_v1(), typescript_agent_v1(), rust_agent_v1());
217    let system_dependencies = merge_deps(
218        merge_deps(py.system_dependencies, ts.system_dependencies),
219        rs.system_dependencies,
220    );
221    let runtimes = merge_tools(merge_tools(py.runtimes, ts.runtimes), rs.runtimes);
222    let tools = merge_tools(merge_tools(py.tools, ts.tools), rs.tools);
223
224    CellProfileData {
225        id: "polyglot-agent-v1",
226        version: "1.0.0",
227        system_dependencies,
228        runtimes,
229        tools,
230        // A polyglot cell has no single default dependency contract: each
231        // ecosystem's DependencySet must be declared explicitly in .sea.
232        dependency_contract: None,
233    }
234}
235
236/// Sort key for simple `major.minor.patch`-ish dotted-numeric version
237/// strings; non-numeric segments (e.g. a pre-release suffix) sort as 0 so
238/// the comparison never panics on unexpected input.
239fn version_key(v: &str) -> Vec<u64> {
240    v.split('.')
241        .map(|seg| {
242            seg.chars()
243                .take_while(|c| c.is_ascii_digit())
244                .collect::<String>()
245        })
246        .map(|digits| digits.parse::<u64>().unwrap_or(0))
247        .collect()
248}
249
250/// Look up a profile by id. Returns `None` for unknown profiles; callers
251/// turn that into `CELL001`.
252pub fn lookup(profile_id: &str) -> Option<CellProfileData> {
253    match profile_id {
254        "python-agent-v1" => Some(python_agent_v1()),
255        "typescript-agent-v1" => Some(typescript_agent_v1()),
256        "rust-agent-v1" => Some(rust_agent_v1()),
257        "polyglot-agent-v1" => Some(polyglot_agent_v1()),
258        _ => None,
259    }
260}
261
262pub const KNOWN_PROFILES: &[&str] = &[
263    "python-agent-v1",
264    "typescript-agent-v1",
265    "rust-agent-v1",
266    "polyglot-agent-v1",
267];
268
269#[cfg(test)]
270mod tests {
271    use super::*;
272
273    #[test]
274    fn all_known_profiles_resolve() {
275        for id in KNOWN_PROFILES {
276            assert!(lookup(id).is_some(), "profile {id} should resolve");
277        }
278    }
279
280    #[test]
281    fn unknown_profile_is_none() {
282        assert!(lookup("nonexistent-v1").is_none());
283    }
284
285    #[test]
286    fn polyglot_union_has_no_duplicate_names() {
287        let p = polyglot_agent_v1();
288        let mut names: Vec<&str> = p.system_dependencies.iter().map(|d| d.name).collect();
289        let before = names.len();
290        names.sort();
291        names.dedup();
292        assert_eq!(
293            names.len(),
294            before,
295            "system dependency union must be deduped"
296        );
297
298        let mut tool_names: Vec<&str> = p.tools.iter().map(|t| t.name).collect();
299        let before = tool_names.len();
300        tool_names.sort();
301        tool_names.dedup();
302        assert_eq!(tool_names.len(), before, "tool union must be deduped");
303    }
304
305    #[test]
306    fn polyglot_union_includes_all_three_runtimes() {
307        let p = polyglot_agent_v1();
308        let names: Vec<&str> = p.runtimes.iter().map(|r| r.name).collect();
309        assert!(names.contains(&"python"));
310        assert!(names.contains(&"node"));
311        assert!(names.contains(&"rust"));
312    }
313}