lean_ctx/core/config/
intelligence.rs1use serde::{Deserialize, Serialize};
5use std::path::{Path, PathBuf};
6
7pub const DECLINED_MARKER: &str = "runtime-declined";
10
11#[must_use]
13pub fn declined_marker() -> Option<PathBuf> {
14 super::Config::path().and_then(|config| config.parent().map(|dir| dir.join(DECLINED_MARKER)))
15}
16
17#[must_use]
20pub fn decline_recorded_at(marker: &Path) -> bool {
21 marker.symlink_metadata().is_ok()
22}
23
24pub fn record_decline_at(marker: &Path, declined: bool) -> std::io::Result<()> {
27 if !declined {
28 return match std::fs::remove_file(marker) {
29 Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
30 other => other,
31 };
32 }
33 if decline_recorded_at(marker) {
34 return Ok(());
35 }
36 if let Some(dir) = marker.parent() {
37 std::fs::create_dir_all(dir)?;
38 }
39 let mut options = std::fs::OpenOptions::new();
40 options.write(true).create_new(true);
41 #[cfg(unix)]
42 std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
43 match options.open(marker) {
44 Ok(mut file) => std::io::Write::write_all(
45 &mut file,
46 b"lean-ctx setup runtime deactivate; cleared by lean-ctx setup runtime install\n",
47 ),
48 Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => Ok(()),
49 Err(error) => Err(error),
50 }
51}
52
53pub fn record_decline(declined: bool) -> std::io::Result<()> {
55 let marker = declined_marker().ok_or_else(|| {
56 std::io::Error::new(std::io::ErrorKind::NotFound, "no configuration directory")
57 })?;
58 record_decline_at(&marker, declined)
59}
60
61#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
64#[serde(default, deny_unknown_fields)]
65pub struct IntelligenceRuntimeConfig {
66 pub enabled: bool,
67 #[serde(skip_serializing)]
80 pub declined: bool,
81 pub accept_proprietary: bool,
82 pub staging: bool,
83 pub root: String,
84 pub manifest_sha256: String,
85 pub trust_key_hex: String,
86 pub channel_url: String,
87 pub channel_signature_url: String,
88 pub channel_root_key_hex: String,
89 pub license_configuration: String,
91 pub context_policy_apply: bool,
94 pub team_artifact_projects: Vec<TeamArtifactProject>,
100}
101
102impl IntelligenceRuntimeConfig {
103 #[must_use]
108 pub fn deactivated(&self) -> bool {
109 self.declined || (self.accept_proprietary && !self.enabled)
110 }
111
112 #[must_use]
114 pub fn with_recorded_decline(mut self) -> Self {
115 self.declined |= declined_marker().is_some_and(|marker| decline_recorded_at(&marker));
116 self
117 }
118
119 #[must_use]
121 pub fn is_unset(&self) -> bool {
122 Self {
123 declined: false,
124 ..self.clone()
125 } == Self::default()
126 }
127
128 pub fn switch_off(&mut self) {
130 self.enabled = false;
131 self.declined = true;
132 }
133
134 #[must_use]
137 pub fn activated(
138 previous: Self,
139 staging: bool,
140 root: String,
141 selected: &str,
142 trust_key_hex: &str,
143 ) -> Self {
144 Self {
145 enabled: true,
146 declined: false,
147 accept_proprietary: true,
148 staging,
149 root,
150 manifest_sha256: selected.to_owned(),
151 trust_key_hex: trust_key_hex.to_owned(),
152 ..previous
153 }
154 }
155}
156
157#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
159#[serde(default, deny_unknown_fields)]
160pub struct TeamArtifactProject {
161 pub root: String,
163 pub organization_id: String,
164 pub project_id: String,
165 pub repository_id: String,
167}
168
169#[cfg(test)]
170mod tests {
171 use super::*;
172
173 #[allow(dead_code)]
177 #[derive(Deserialize)]
178 #[serde(default, deny_unknown_fields)]
179 #[derive(Default)]
180 struct Shape3127 {
181 enabled: bool,
182 accept_proprietary: bool,
183 staging: bool,
184 root: String,
185 manifest_sha256: String,
186 trust_key_hex: String,
187 channel_url: String,
188 channel_signature_url: String,
189 channel_root_key_hex: String,
190 license_configuration: String,
191 context_policy_apply: bool,
192 team_artifact_projects: Vec<toml::Value>,
193 }
194
195 #[test]
196 fn a_switched_off_runtime_stays_readable_by_3_12_7() {
197 let installed = IntelligenceRuntimeConfig::activated(
198 IntelligenceRuntimeConfig::default(),
199 false,
200 "/r".into(),
201 &"b".repeat(64),
202 "k",
203 );
204 for mut config in [IntelligenceRuntimeConfig::default(), installed] {
205 config.switch_off();
206 assert!(config.deactivated());
207 let written = toml::to_string(&config).unwrap();
208 assert!(!written.contains("declined"), "{written}");
209 let old: Shape3127 = toml::from_str(&written).unwrap();
210 assert!(!old.enabled, "3.12.7 sees the runtime off");
211 }
212 let early: IntelligenceRuntimeConfig = toml::from_str("declined = true\n").unwrap();
214 assert!(early.deactivated());
215 }
216
217 #[test]
218 fn the_decline_marker_records_and_clears() {
219 let dir = tempfile::tempdir().unwrap();
220 let marker = dir.path().join("config").join(DECLINED_MARKER);
221 assert!(!decline_recorded_at(&marker));
222 record_decline_at(&marker, false).unwrap();
223 record_decline_at(&marker, true).unwrap();
224 record_decline_at(&marker, true).unwrap();
225 assert!(decline_recorded_at(&marker));
226 #[cfg(unix)]
227 {
228 use std::os::unix::fs::PermissionsExt;
229 let mode = std::fs::metadata(&marker).unwrap().permissions().mode();
230 assert_eq!(mode & 0o777, 0o600);
231 }
232 record_decline_at(&marker, false).unwrap();
233 assert!(!decline_recorded_at(&marker));
234 #[cfg(unix)]
236 {
237 std::os::unix::fs::symlink(dir.path().join("nowhere"), &marker).unwrap();
238 assert!(decline_recorded_at(&marker));
239 }
240 }
241}