1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// SPDX-License-Identifier: Apache-2.0
//! User-global opt-in and independent trust pins for the optional local runtime.
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
/// The file next to `config.toml` that records `setup runtime deactivate`
/// (see [`IntelligenceRuntimeConfig::declined`]).
pub const DECLINED_MARKER: &str = "runtime-declined";
/// Where the decline of this configuration is recorded.
#[must_use]
pub fn declined_marker() -> Option<PathBuf> {
super::Config::path().and_then(|config| config.parent().map(|dir| dir.join(DECLINED_MARKER)))
}
/// Whether `marker` records a decline. A symlink counts as recorded (fail
/// closed: the runtime stays off) and is never followed.
#[must_use]
pub fn decline_recorded_at(marker: &Path) -> bool {
marker.symlink_metadata().is_ok()
}
/// Records (`true`) or clears (`false`) the decline at `marker`. Clearing
/// a missing marker succeeds.
pub fn record_decline_at(marker: &Path, declined: bool) -> std::io::Result<()> {
if !declined {
return match std::fs::remove_file(marker) {
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
other => other,
};
}
if decline_recorded_at(marker) {
return Ok(());
}
if let Some(dir) = marker.parent() {
std::fs::create_dir_all(dir)?;
}
let mut options = std::fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
match options.open(marker) {
Ok(mut file) => std::io::Write::write_all(
&mut file,
b"lean-ctx setup runtime deactivate; cleared by lean-ctx setup runtime install\n",
),
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => Ok(()),
Err(error) => Err(error),
}
}
/// [`record_decline_at`] for this configuration.
pub fn record_decline(declined: bool) -> std::io::Result<()> {
let marker = declined_marker().ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::NotFound, "no configuration directory")
})?;
record_decline_at(&marker, declined)
}
/// Never merged from a project file, including trusted workspaces.
/// A public signing key is not a credential; the package cannot provide its pin.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct IntelligenceRuntimeConfig {
pub enabled: bool,
/// Switched off with `setup runtime deactivate`, also before anything
/// was installed: setup, onboard, repair, bootstrap, harden and connect
/// never install or enable the runtime on their own. Only an explicit
/// `setup runtime install` (an activation) clears it.
///
/// Never written to `config.toml`: this section is `deny_unknown_fields`,
/// and 3.12.7 (a running daemon, a rollback) would refuse the whole file
/// and fall back to the defaults. The decline is the file
/// [`DECLINED_MARKER`] next to `config.toml`, which older versions
/// neither read nor rewrite; [`Self::with_recorded_decline`] merges it.
/// Still read from `config.toml`, so a configuration written by an early
/// 3.12.8 build keeps loading.
#[serde(skip_serializing)]
pub declined: bool,
pub accept_proprietary: bool,
pub staging: bool,
pub root: String,
pub manifest_sha256: String,
pub trust_key_hex: String,
pub channel_url: String,
pub channel_signature_url: String,
pub channel_root_key_hex: String,
/// Explicit user-global handoff to the private verifier, never project input.
pub license_configuration: String,
/// No effect: read-strategy policies are no longer learned by a runtime.
/// Kept so an existing config (`deny_unknown_fields`) still loads.
pub context_policy_apply: bool,
/// The projects of this user that read shared team artifacts, each with
/// its team identity. Empty by default: a project without an entry uses
/// no artifact. Global-only like the rest of this section, so a
/// repository cannot name itself. It carries no clearance: none is
/// configurable here.
pub team_artifact_projects: Vec<TeamArtifactProject>,
}
impl IntelligenceRuntimeConfig {
/// Switched off with `setup runtime deactivate`, before or after an
/// install (an install accepted once and no longer enabled counts too).
/// Read from the configuration alone: whether the installed package
/// still verifies does not change the person's choice.
#[must_use]
pub fn deactivated(&self) -> bool {
self.declined || (self.accept_proprietary && !self.enabled)
}
/// This configuration with the decline recorded next to `config.toml`.
#[must_use]
pub fn with_recorded_decline(mut self) -> Self {
self.declined |= declined_marker().is_some_and(|marker| decline_recorded_at(&marker));
self
}
/// Nothing configured, apart from a switch-off.
#[must_use]
pub fn is_unset(&self) -> bool {
Self {
declined: false,
..self.clone()
} == Self::default()
}
/// `setup runtime deactivate`: off, and kept off by setup.
pub fn switch_off(&mut self) {
self.enabled = false;
self.declined = true;
}
/// The configuration after an activation of `selected` (an explicit
/// install or activate): enabled, accepted and no longer switched off.
#[must_use]
pub fn activated(
previous: Self,
staging: bool,
root: String,
selected: &str,
trust_key_hex: &str,
) -> Self {
Self {
enabled: true,
declined: false,
accept_proprietary: true,
staging,
root,
manifest_sha256: selected.to_owned(),
trust_key_hex: trust_key_hex.to_owned(),
..previous
}
}
}
/// One `[[intelligence_runtime.team_artifact_projects]]` entry.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct TeamArtifactProject {
/// Absolute path of the project root.
pub root: String,
pub organization_id: String,
pub project_id: String,
/// The project's opaque repository identity — not a URL or a path.
pub repository_id: String,
}
#[cfg(test)]
mod tests {
use super::*;
/// `[intelligence_runtime]` as 3.12.7 parses it (MR !569 review): the
/// same fields, `deny_unknown_fields`. A field it does not know makes
/// 3.12.7 refuse the whole `config.toml` and run on the defaults.
#[allow(dead_code)]
#[derive(Deserialize)]
#[serde(default, deny_unknown_fields)]
#[derive(Default)]
struct Shape3127 {
enabled: bool,
accept_proprietary: bool,
staging: bool,
root: String,
manifest_sha256: String,
trust_key_hex: String,
channel_url: String,
channel_signature_url: String,
channel_root_key_hex: String,
license_configuration: String,
context_policy_apply: bool,
team_artifact_projects: Vec<toml::Value>,
}
#[test]
fn a_switched_off_runtime_stays_readable_by_3_12_7() {
let installed = IntelligenceRuntimeConfig::activated(
IntelligenceRuntimeConfig::default(),
false,
"/r".into(),
&"b".repeat(64),
"k",
);
for mut config in [IntelligenceRuntimeConfig::default(), installed] {
config.switch_off();
assert!(config.deactivated());
let written = toml::to_string(&config).unwrap();
assert!(!written.contains("declined"), "{written}");
let old: Shape3127 = toml::from_str(&written).unwrap();
assert!(!old.enabled, "3.12.7 sees the runtime off");
}
// A configuration from an early 3.12.8 build still loads here.
let early: IntelligenceRuntimeConfig = toml::from_str("declined = true\n").unwrap();
assert!(early.deactivated());
}
#[test]
fn the_decline_marker_records_and_clears() {
let dir = tempfile::tempdir().unwrap();
let marker = dir.path().join("config").join(DECLINED_MARKER);
assert!(!decline_recorded_at(&marker));
record_decline_at(&marker, false).unwrap();
record_decline_at(&marker, true).unwrap();
record_decline_at(&marker, true).unwrap();
assert!(decline_recorded_at(&marker));
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mode = std::fs::metadata(&marker).unwrap().permissions().mode();
assert_eq!(mode & 0o777, 0o600);
}
record_decline_at(&marker, false).unwrap();
assert!(!decline_recorded_at(&marker));
// A dangling symlink counts as a decline (fail closed), unfollowed.
#[cfg(unix)]
{
std::os::unix::fs::symlink(dir.path().join("nowhere"), &marker).unwrap();
assert!(decline_recorded_at(&marker));
}
}
}