lean_ctx/core/intelligence_runtime/
mod.rs1mod bootstrap;
6mod catalog;
7mod channel;
8mod cli;
9pub mod cockpit;
10pub mod code_security;
11pub mod compliance_frameworks;
12mod configuration;
13pub(crate) mod dashboard_pro;
14mod exchange;
15pub(crate) mod org_policy_verifier;
16pub(crate) mod semantic_coverage;
17pub mod semantic_detectors;
18
19#[cfg(any(test, feature = "test-support"))]
21#[cfg_attr(
22 not(any(target_os = "macos", all(target_os = "linux", target_env = "gnu"))),
23 allow(dead_code)
24)]
25pub(crate) fn private_root_for_test() -> tempfile::TempDir {
26 let mut builder = tempfile::Builder::new();
27 builder.prefix("runtime-install-test-");
28 #[cfg(unix)]
29 {
30 use std::os::unix::fs::PermissionsExt;
31 builder.permissions(std::fs::Permissions::from_mode(0o700));
32 }
33 builder.tempdir().expect("private test root")
34}
35
36#[cfg(all(
38 any(test, feature = "test-support"),
39 any(target_os = "macos", all(target_os = "linux", target_env = "gnu"))
40))]
41pub(crate) fn sign_document(
42 document: &serde_json::Value,
43 key: &ed25519_dalek::SigningKey,
44 domain: &[u8],
45) -> (Vec<u8>, Vec<u8>, String) {
46 use ed25519_dalek::Signer;
47 let manifest = serde_json::to_vec(document).expect("manifest");
48 let mut payload = domain.to_vec();
49 payload.extend_from_slice(&manifest);
50 let signature = key.sign(&payload).to_bytes().to_vec();
51 let selected = sha256(&manifest);
52 (manifest, signature, selected)
53}
54
55#[cfg(all(
58 feature = "test-support",
59 not(test),
60 any(target_os = "macos", all(target_os = "linux", target_env = "gnu"))
61))]
62#[allow(dead_code, unused_imports, clippy::unwrap_used)]
63#[path = "company_journey_tests.rs"]
64pub mod company_journey_support;
65mod semantic_worker;
66pub use configuration::{DISCLOSURE, inspect_setup};
67mod health;
68mod install;
69mod license;
70pub mod lease;
72mod manifest;
73mod rotation;
74#[cfg(any(unix, windows))]
75mod session;
76#[cfg(windows)]
77mod session_windows;
78
79#[cfg(test)]
80pub(crate) mod tests;
81
82use std::io::Read;
83use std::path::Path;
84
85pub use cli::run;
86use manifest::{VerifiedPackage, verify, verify_kind};
87
88const EXECUTABLE_NAME: &str = if cfg!(windows) {
90 "leanctx-intelligence.exe"
91} else {
92 "leanctx-intelligence"
93};
94
95#[derive(Debug, Clone, Copy, PartialEq, Eq)]
101pub(crate) enum RuntimeKind {
102 Intelligence,
104 Cockpit,
106}
107
108impl RuntimeKind {
109 pub(crate) const fn engine_id(self) -> &'static str {
110 match self {
111 Self::Intelligence => "leanctx-intelligence",
112 Self::Cockpit => "leanctx-cockpit",
113 }
114 }
115
116 pub(crate) const fn store_name(self) -> &'static str {
119 match self {
120 Self::Intelligence => "intelligence-runtime",
121 Self::Cockpit => "cockpit-runtime",
122 }
123 }
124}
125
126#[derive(Debug, thiserror::Error)]
127pub enum InstallError {
128 #[error("invalid runtime installer arguments")]
129 Usage,
130 #[error("runtime download URL, transport or response rejected")]
131 Download,
132 #[error("runtime package I/O failed")]
133 Io(#[from] std::io::Error),
134 #[error("runtime metadata or package exceeds its size bound")]
135 Size,
136 #[error("runtime input is not a regular, non-symlink file")]
137 Input,
138 #[error("runtime manifest is not independently selected or compatible")]
139 Manifest,
140 #[error("runtime signature does not match the independent trust key")]
141 Signature,
142 #[error("runtime archive is invalid or incompatible with this host")]
143 Archive,
144 #[error("runtime installation directory is not private and canonical")]
145 Directory,
146 #[error("another runtime installation is active")]
147 Busy,
148 #[error("runtime selection changed or retained package is inconsistent")]
149 State,
150 #[error("runtime channel is older than the active one; a downgrade must be requested")]
151 Downgrade,
152 #[error("atomic runtime selection write failed")]
153 Commit,
154 #[error("runtime health check failed or exceeded its bound")]
155 Health,
156 #[error("runtime invocation is not supported by the authenticated package and this host")]
157 InvocationAdmission,
158 #[error("runtime invocation failed or exceeded its bound")]
159 Exchange,
160 #[error("runtime request or policy input was rejected")]
161 Policy,
162 #[error("runtime global configuration could not be updated")]
163 Configuration,
164}
165
166pub(crate) type Result<T> = std::result::Result<T, InstallError>;
167
168fn trust_key(encoded: &str) -> Result<[u8; 32]> {
169 if !is_digest(encoded, 64) {
170 return Err(InstallError::Usage);
171 }
172 let mut key = [0; 32];
173 for (index, byte) in key.iter_mut().enumerate() {
174 *byte = u8::from_str_radix(&encoded[index * 2..index * 2 + 2], 16)
175 .map_err(|_| InstallError::Usage)?;
176 }
177 Ok(key)
178}
179
180pub(crate) fn read_regular(path: &Path, limit: usize) -> Result<Vec<u8>> {
182 let metadata = std::fs::symlink_metadata(path)?;
183 if !regular_metadata(&metadata) {
184 return Err(InstallError::Input);
185 }
186 let mut options = std::fs::OpenOptions::new();
187 options.read(true);
188 #[cfg(unix)]
189 {
190 use std::os::unix::fs::OpenOptionsExt;
191 options.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK);
192 }
193 #[cfg(windows)]
194 {
195 use std::os::windows::fs::OpenOptionsExt;
196 use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_OPEN_REPARSE_POINT;
197 options.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT);
199 }
200 let file = options.open(path)?;
201 let metadata = file.metadata()?;
202 if !regular_metadata(&metadata) {
203 return Err(InstallError::Input);
204 }
205 if metadata.len() > limit as u64 {
206 return Err(InstallError::Size);
207 }
208 let mut bytes = Vec::new();
209 file.take(limit as u64 + 1).read_to_end(&mut bytes)?;
210 if bytes.len() > limit {
211 return Err(InstallError::Size);
212 }
213 Ok(bytes)
214}
215
216fn regular_metadata(metadata: &std::fs::Metadata) -> bool {
217 #[cfg(windows)]
218 {
219 use std::os::windows::fs::MetadataExt;
220 use windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_REPARSE_POINT;
221 if metadata.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0 {
223 return false;
224 }
225 }
226 metadata.is_file() && !metadata.file_type().is_symlink()
227}
228
229use super::updater::sha256_hex as sha256;
230
231pub(crate) fn is_digest(value: &str, length: usize) -> bool {
232 value.len() == length
233 && value
234 .bytes()
235 .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
236}