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()
166 && (configured.staging != staging || !super::bootstrap::permits_config(&configured))
167 {
168 return Err(InstallError::Usage);
169 }
170 }
171 if staging
172 && let Some(root) = flags.get("--root")
173 && super::bootstrap::production_key(Path::new(root))?.is_some()
174 {
175 return Err(InstallError::Usage);
176 }
177 if !matches!(operation, "verify" | "deactivate" | "status") && !accepted {
178 return Err(InstallError::Usage);
179 }
180 let get = |name| flags.get(name).copied().ok_or(InstallError::Usage);
181 if operation == "rotate-root" {
182 if flags.len() != 4 {
183 return Err(InstallError::Usage);
184 }
185 let proof = serde_json::from_slice(&read_regular(Path::new(get("--transition")?), 4096)?)
186 .map_err(|_| InstallError::Manifest)?;
187 let result = install::rotate_root(
188 Path::new(get("--root")?),
189 get("--expected-active")?,
190 &super::trust_key(get("--trust-key-hex")?)?,
191 proof,
192 )?;
193 return serde_json::to_string_pretty(&result).map_err(|_| InstallError::State);
194 }
195 if operation == "sync" {
196 if flags.len() != 5 {
197 return Err(InstallError::Usage);
198 }
199 let result = super::catalog::sync_kind(
200 kind,
201 [get("--channel-url")?, get("--channel-signature-url")?],
202 &super::trust_key(get("--trust-key-hex")?)?,
203 Path::new(get("--root")?),
204 get("--expected-active")?,
205 super::catalog::Companions::Inline,
206 )?;
207 return serde_json::to_string_pretty(&result).map_err(|_| InstallError::State);
208 }
209 if matches!(
210 operation,
211 "status" | "activate-configured" | "sync-configured" | "rollback-configured"
212 ) {
213 if !flags.is_empty() {
214 return Err(InstallError::Usage);
215 }
216 let candidate = super::configuration::inspect_setup()?;
217 let result = if operation == "status" {
218 candidate.report()
219 } else if operation == "sync-configured" {
220 candidate.install_and_enable()?
221 } else if operation == "rollback-configured" {
222 candidate.rollback_and_enable()?
223 } else {
224 candidate.enable()?
225 };
226 return serde_json::to_string_pretty(&result).map_err(|_| InstallError::State);
227 }
228 if operation == "deactivate" {
229 if !flags.is_empty() {
230 return Err(InstallError::Usage);
231 }
232 let result = super::configuration::deactivate()?;
233 return serde_json::to_string_pretty(&result).map_err(|_| InstallError::State);
234 }
235 let selected = get("--manifest-sha256")?;
236 let encoded = get("--trust-key-hex")?;
237 let key = super::trust_key(encoded)?;
238 let result = if operation == "activate" {
239 if flags.len() != 4 || get("--expected-active")? != selected {
240 return Err(InstallError::Usage);
241 }
242 super::configuration::activate(Path::new(get("--root")?), selected, encoded)?
243 } else if operation == "invoke" {
244 if flags.len() != 5 || get("--expected-active")? != selected {
245 return Err(InstallError::Usage);
246 }
247 #[cfg(not(unix))]
248 return Err(InstallError::Exchange);
249 #[cfg(unix)]
250 {
251 let bytes = read_regular(
253 Path::new(get("--request")?),
254 lean_ctx_protocol::runtime_exchange::MAX_RUNTIME_EXCHANGE_BYTES,
255 )?;
256 let request = serde_json::from_slice(&bytes).map_err(|_| InstallError::Exchange)?;
257 let answer =
258 super::session::invoke(Path::new(get("--root")?), selected, &key, &request)?;
259 crate::core::execution_ledger::host::admit_foreign_document_narrative(
264 "runtime-invoke:",
265 &answer,
266 "engine-runtime-invoke",
267 )
268 .map_err(|_| InstallError::Exchange)?;
269 answer
270 }
271 } else if operation == "health" {
272 if flags.len() != 4 || get("--expected-active")? != selected {
273 return Err(InstallError::Usage);
274 }
275 super::health::check(Path::new(get("--root")?), selected, &key)?
276 } else if operation == "rollback" {
277 if flags.len() != 4 {
278 return Err(InstallError::Usage);
279 }
280 install::rollback(
281 Path::new(get("--root")?),
282 get("--expected-active")?,
283 selected,
284 &key,
285 )?
286 } else {
287 let expected = if operation == "verify" { 5 } else { 7 };
288 if flags.len() != expected + usize::from(companions.is_some()) {
289 return Err(InstallError::Usage);
290 }
291 let (archive, manifest, signature) = if operation == "download" {
292 get("--root")?;
293 get("--expected-active")?;
294 let fetched = super::channel::download(
295 [
296 get("--archive-url")?,
297 get("--manifest-url")?,
298 get("--signature-url")?,
299 ],
300 selected,
301 &key,
302 )?;
303 (fetched.archive, fetched.manifest, fetched.signature)
304 } else {
305 (
306 read_regular(Path::new(get("--archive")?), MAX_ARCHIVE)?,
307 read_regular(Path::new(get("--manifest")?), 65_536)?,
308 read_regular(Path::new(get("--signature")?), 64)?,
309 )
310 };
311 let package = verify_kind(kind, &archive, &manifest, &signature, selected, &key)?;
312 if operation == "verify" {
313 serde_json::json!({"status": "verified", "receipt": package.receipt,
314 "runtime_started": false, "release_approved": false})
315 } else {
316 let root = Path::new(get("--root")?);
317 let expected_active = get("--expected-active")?;
318 if let Some(companions) = companions {
319 install::install_with_companions(
320 root,
321 expected_active,
322 &package,
323 &archive,
324 &manifest,
325 &signature,
326 companions,
327 )?
328 } else {
329 install::install(
330 root,
331 expected_active,
332 &package,
333 &archive,
334 &manifest,
335 &signature,
336 )?
337 }
338 }
339 };
340 serde_json::to_string_pretty(&result).map_err(|_| InstallError::State)
341}