lean_ctx/core/intelligence_runtime/
cli.rs1use std::collections::BTreeMap;
5use std::path::Path;
6
7use crate::core::config::Config;
8
9use super::{
10 InstallError, Result, RuntimeKind, install, manifest::MAX_ARCHIVE, read_regular, verify_kind,
11};
12
13pub fn plain_install(args: &[String]) -> Option<[String; 2]> {
19 (args == ["install"] || args == ["install", "--accept-proprietary"])
20 .then(|| ["sync-configured".into(), "--accept-proprietary".into()])
21}
22
23pub const USAGE: &str = "Usage:
26 lean-ctx setup runtime install install the runtime from the signed LeanCTX channel
27 lean-ctx setup runtime status show what is installed (JSON)
28 lean-ctx setup runtime deactivate switch the runtime off
29 lean-ctx setup runtime load-companions load the model set setup left for later
30 lean-ctx setup runtime sync-configured|activate-configured|rollback-configured --accept-proprietary
31 Staging packages (install|verify|download|sync|rollback|health --staging --accept-proprietary
32 with explicit --archive/--manifest/--signature or channel pins): docs/contracts/intelligence-runtime-install-v1.md";
33
34pub fn error_message(error: &InstallError) -> String {
37 match error {
38 InstallError::Usage => format!("{error}\n{USAGE}"),
39 other => other.to_string(),
40 }
41}
42
43pub fn human_note(args: &[String]) -> Option<&'static str> {
46 (args.first().map(String::as_str) == Some("status")).then_some(
47 "Note: \"release_approved\": false is a fixed field of every runtime document: build provenance never claims publication approval by itself. It does not mean the installed runtime is unverified; \"status\" and the signature checks say that.",
48 )
49}
50
51pub fn run(args: &[String]) -> Result<String> {
52 if let Some(configured) = plain_install(args) {
53 return run(&configured);
54 }
55 if args == ["load-companions"] {
56 let result = super::configuration::load_companions()?;
57 return serde_json::to_string_pretty(&result).map_err(|_| InstallError::State);
58 }
59 let operation = args
60 .first()
61 .map(String::as_str)
62 .ok_or(InstallError::Usage)?;
63 if !matches!(
64 operation,
65 "verify"
66 | "install"
67 | "download"
68 | "sync"
69 | "rotate-root"
70 | "rollback"
71 | "health"
72 | "invoke"
73 | "activate"
74 | "deactivate"
75 | "status"
76 | "activate-configured"
77 | "sync-configured"
78 | "rollback-configured"
79 ) {
80 return Err(InstallError::Usage);
81 }
82 let mut flags = BTreeMap::new();
83 let mut arguments = args[1..].iter();
84 let mut staging = false;
85 let mut accepted = false;
86 while let Some(flag) = arguments.next() {
87 match flag.as_str() {
88 "--staging" if !staging => staging = true,
89 "--accept-proprietary" if !accepted => accepted = true,
90 "--archive"
91 | "--manifest"
92 | "--signature"
93 | "--manifest-sha256"
94 | "--trust-key-hex"
95 | "--root"
96 | "--expected-active"
97 | "--request"
98 | "--archive-url"
99 | "--manifest-url"
100 | "--signature-url"
101 | "--channel-url"
102 | "--transition"
103 | "--semantic-models"
104 | "--kind"
105 | "--channel-signature-url" => {
106 let value = arguments.next().ok_or(InstallError::Usage)?;
107 if value.starts_with("--") || flags.insert(flag.as_str(), value.as_str()).is_some()
108 {
109 return Err(InstallError::Usage);
110 }
111 }
112 _ => return Err(InstallError::Usage),
113 }
114 }
115 let companions = flags.get("--semantic-models").copied().map(Path::new);
118 if companions.is_some() && operation != "install" {
119 return Err(InstallError::Usage);
120 }
121 let kind = match flags.remove("--kind") {
124 None | Some("intelligence") => RuntimeKind::Intelligence,
125 Some("cockpit") => RuntimeKind::Cockpit,
126 Some(_) => return Err(InstallError::Usage),
127 };
128 if let Some(root) = flags.get("--root") {
129 let cockpit_store = Path::new(root)
130 .file_name()
131 .and_then(|name| name.to_str())
132 .is_some_and(|name| name.starts_with(RuntimeKind::Cockpit.store_name()));
133 if cockpit_store != (kind == RuntimeKind::Cockpit) {
134 return Err(InstallError::Usage);
135 }
136 }
137 if kind == RuntimeKind::Cockpit {
138 match operation {
139 "status" | "sync-configured" | "activate" | "deactivate" => {
140 return serde_json::to_string_pretty(&super::cockpit::run_cli(
141 operation, staging, accepted, &flags,
142 )?)
143 .map_err(|_| InstallError::State);
144 }
145 "verify" | "install" | "download" | "sync" | "rotate-root" if companions.is_none() => {}
146 _ => return Err(InstallError::Usage),
147 }
148 }
149 let configured_operation = matches!(
150 operation,
151 "status" | "sync-configured" | "rollback-configured" | "activate-configured" | "deactivate"
152 );
153 if !staging && !configured_operation {
154 return Err(InstallError::Usage);
155 }
156 if configured_operation && !matches!(operation, "status" | "deactivate") {
157 let observed = Config::try_load_global()
158 .map_err(|_| InstallError::Configuration)?
159 .intelligence_runtime;
160 let configured = if observed.is_unset() {
161 super::bootstrap::config()?
162 } else {
163 observed
164 };
165 if !configured.is_unset()
168 && !configured.staging
169 && !staging
170 && !super::bootstrap::offers_production()
171 {
172 return Err(InstallError::NotOffered);
173 }
174 if !configured.is_unset()
175 && (configured.staging != staging || !super::bootstrap::permits_config(&configured))
176 {
177 return Err(InstallError::Usage);
178 }
179 }
180 if staging
181 && let Some(root) = flags.get("--root")
182 && super::bootstrap::production_key(Path::new(root))?.is_some()
183 {
184 return Err(InstallError::Usage);
185 }
186 if !matches!(operation, "verify" | "deactivate" | "status") && !accepted {
187 return Err(InstallError::Usage);
188 }
189 let get = |name| flags.get(name).copied().ok_or(InstallError::Usage);
190 if operation == "rotate-root" {
191 if flags.len() != 4 {
192 return Err(InstallError::Usage);
193 }
194 let proof = serde_json::from_slice(&read_regular(Path::new(get("--transition")?), 4096)?)
195 .map_err(|_| InstallError::Manifest)?;
196 let result = install::rotate_root(
197 Path::new(get("--root")?),
198 get("--expected-active")?,
199 &super::trust_key(get("--trust-key-hex")?)?,
200 proof,
201 )?;
202 return serde_json::to_string_pretty(&result).map_err(|_| InstallError::State);
203 }
204 if operation == "sync" {
205 if flags.len() != 5 {
206 return Err(InstallError::Usage);
207 }
208 let result = super::catalog::sync_kind(
209 kind,
210 [get("--channel-url")?, get("--channel-signature-url")?],
211 &super::trust_key(get("--trust-key-hex")?)?,
212 Path::new(get("--root")?),
213 get("--expected-active")?,
214 super::catalog::Companions::Inline,
215 )?;
216 return serde_json::to_string_pretty(&result).map_err(|_| InstallError::State);
217 }
218 if matches!(
219 operation,
220 "status" | "activate-configured" | "sync-configured" | "rollback-configured"
221 ) {
222 if !flags.is_empty() {
223 return Err(InstallError::Usage);
224 }
225 let candidate = super::configuration::inspect_setup()?;
226 let result = if operation == "status" {
227 candidate.report()
228 } else if operation == "sync-configured" {
229 candidate.install_and_enable()?
230 } else if operation == "rollback-configured" {
231 candidate.rollback_and_enable()?
232 } else {
233 candidate.enable()?
234 };
235 return serde_json::to_string_pretty(&result).map_err(|_| InstallError::State);
236 }
237 if operation == "deactivate" {
238 if !flags.is_empty() {
239 return Err(InstallError::Usage);
240 }
241 let result = super::configuration::deactivate()?;
242 return serde_json::to_string_pretty(&result).map_err(|_| InstallError::State);
243 }
244 let selected = get("--manifest-sha256")?;
245 let encoded = get("--trust-key-hex")?;
246 let key = super::trust_key(encoded)?;
247 let result = if operation == "activate" {
248 if flags.len() != 4 || get("--expected-active")? != selected {
249 return Err(InstallError::Usage);
250 }
251 super::configuration::activate(Path::new(get("--root")?), selected, encoded)?
252 } else if operation == "invoke" {
253 if flags.len() != 5 || get("--expected-active")? != selected {
254 return Err(InstallError::Usage);
255 }
256 #[cfg(not(unix))]
257 return Err(InstallError::Exchange);
258 #[cfg(unix)]
259 {
260 let bytes = read_regular(
262 Path::new(get("--request")?),
263 lean_ctx_protocol::runtime_exchange::MAX_RUNTIME_EXCHANGE_BYTES,
264 )?;
265 let request = serde_json::from_slice(&bytes).map_err(|_| InstallError::Exchange)?;
266 let answer =
267 super::session::invoke(Path::new(get("--root")?), selected, &key, &request)?;
268 crate::core::execution_ledger::host::admit_foreign_document_narrative(
273 "runtime-invoke:",
274 &answer,
275 "engine-runtime-invoke",
276 )
277 .map_err(|_| InstallError::Exchange)?;
278 answer
279 }
280 } else if operation == "health" {
281 if flags.len() != 4 || get("--expected-active")? != selected {
282 return Err(InstallError::Usage);
283 }
284 super::health::check(Path::new(get("--root")?), selected, &key)?
285 } else if operation == "rollback" {
286 if flags.len() != 4 {
287 return Err(InstallError::Usage);
288 }
289 install::rollback(
290 Path::new(get("--root")?),
291 get("--expected-active")?,
292 selected,
293 &key,
294 )?
295 } else {
296 let expected = if operation == "verify" { 5 } else { 7 };
297 if flags.len() != expected + usize::from(companions.is_some()) {
298 return Err(InstallError::Usage);
299 }
300 let (archive, manifest, signature) = if operation == "download" {
301 get("--root")?;
302 get("--expected-active")?;
303 let fetched = super::channel::download(
304 [
305 get("--archive-url")?,
306 get("--manifest-url")?,
307 get("--signature-url")?,
308 ],
309 selected,
310 &key,
311 )?;
312 (fetched.archive, fetched.manifest, fetched.signature)
313 } else {
314 (
315 read_regular(Path::new(get("--archive")?), MAX_ARCHIVE)?,
316 read_regular(Path::new(get("--manifest")?), 65_536)?,
317 read_regular(Path::new(get("--signature")?), 64)?,
318 )
319 };
320 let package = verify_kind(kind, &archive, &manifest, &signature, selected, &key)?;
321 if operation == "verify" {
322 serde_json::json!({"status": "verified", "receipt": package.receipt,
323 "runtime_started": false, "release_approved": false})
324 } else {
325 let root = Path::new(get("--root")?);
326 let expected_active = get("--expected-active")?;
327 if let Some(companions) = companions {
328 install::install_with_companions(
329 root,
330 expected_active,
331 &package,
332 &archive,
333 &manifest,
334 &signature,
335 companions,
336 )?
337 } else {
338 install::install(
339 root,
340 expected_active,
341 &package,
342 &archive,
343 &manifest,
344 &signature,
345 )?
346 }
347 }
348 };
349 serde_json::to_string_pretty(&result).map_err(|_| InstallError::State)
350}