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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// SPDX-License-Identifier: Apache-2.0
//! `[cloud]` configuration.
//!
//! ```toml
//! [cloud]
//! # enabled = true # unset: on while this machine is connected
//! url = "https://cloud.leanctx.com"
//! # root_key = "<base64>" # self-hosted Cloud: its root public key
//! auto_capture = true # 3.11.5 name `auto_draft` is read as an alias
//! pull_interval_minutes = 5 # 1..=60
//! metrics_upload = true
//! receipts_upload = true
//! bus = "auto" # auto | off
//! vault_labels = true
//! presence_names = true # false: no "working on" line for your team
//!
//! [cloud.worker] # stricter local caps on the opt-in budget
//! # max_tasks_per_day = 3
//! # max_tokens_per_day = 200000
//! # max_usd_micros_per_day = 2000000
//! ```
//!
//! The Cloud connector reads this section and re-exports these types as
//! its own config. It also registers what a `config set`
//! of a `cloud.` key applies at once ([`register_off_switches`]).
use std::sync::OnceLock;
use serde::{Deserialize, Serialize};
use lean_ctx_protocol::cloud::CLOUD_DEFAULT_URL;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CloudBusMode {
#[default]
Auto,
Off,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct CloudConfig {
/// `None`: on while this machine is connected. `false` pauses all Cloud
/// traffic without disconnecting.
#[serde(skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>,
/// Cloud origin; any `https` URL for a self-hosted Cloud.
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
/// Root public key (base64 Ed25519) of a self-hosted Cloud.
#[serde(skip_serializing_if = "Option::is_none")]
pub root_key: Option<String>,
/// Run the capture gate on session learnings (3.11.5: `auto_draft`,
/// still read as an alias, `core::config::renamed`).
pub auto_capture: bool,
/// Minutes between scheduler passes; [`CloudConfig::pull_interval`]
/// clamps it to [`PULL_INTERVAL_BOUNDS`].
pub pull_interval_minutes: u32,
pub metrics_upload: bool,
pub receipts_upload: bool,
pub bus: CloudBusMode,
pub vault_labels: bool,
/// Team presence names: your team sees what your agents are working on
/// (the status line each agent sets itself, with paths and secrets
/// removed) and this machine's short name as `machine_name`. Sent only
/// to a Cloud that shows them to members of the same team; `false`
/// keeps presence to role, state and the Instance's short name, which
/// every presence carries (never a domain).
pub presence_names: bool,
/// The user agreed to install the proprietary Intelligence runtime that
/// the lease licenses (`cloud connect` asks, `cloud runtime install`
/// records it). Nothing installs it without this consent.
pub runtime_consent: bool,
/// Worker mode: local caps that only tighten the server opt-in budget.
pub worker: CloudWorkerConfig,
/// mz6: origin of the Cloud models gateway that serves the subscription
/// models (`leanctx` provider). `None`: `https://models.<cloud host>`.
/// Honoured only when its host is the Cloud host or a direct subdomain
/// of it (loopback for tests); any other value is refused and the
/// instance token is never sent (`core::cloud_models::models_origin`).
#[serde(skip_serializing_if = "Option::is_none")]
pub models_url: Option<String>,
/// mz6 owner question Q3 (open): whether Anthropic-shaped agents may
/// reach the subscription models through `shape-xlat` routing aliases.
/// Default `false`: OpenAI-shaped agents only, the rel2 launch scope;
/// `true` has no effect until Q3 is decided (routing refuses aliases to
/// `leanctx`).
pub models_anthropic_agents: bool,
/// How the bus treats a sender presenting as the Queen that is no
/// registered Queen session: `warn` (unset: pass, log, mark) or
/// `enforce` (refuse); any other value counts as `enforce`. Kept as
/// text so a wrong value never breaks the whole config.
#[serde(skip_serializing_if = "Option::is_none")]
pub queen_identity: Option<String>,
/// What remains of the pre-3.12 `[cloud]` table (hosted account sync):
/// the legacy opt-in the loader turns into `telemetry.enabled`. Read,
/// never written; the config migration retires the key.
#[serde(skip_serializing)]
pub contribute_enabled: bool,
}
impl Default for CloudConfig {
fn default() -> Self {
Self {
enabled: None,
url: None,
root_key: None,
auto_capture: true,
pull_interval_minutes: DEFAULT_PULL_INTERVAL_MINUTES,
metrics_upload: true,
receipts_upload: true,
bus: CloudBusMode::Auto,
vault_labels: true,
presence_names: true,
runtime_consent: false,
worker: CloudWorkerConfig::default(),
models_url: None,
models_anthropic_agents: false,
queen_identity: None,
contribute_enabled: false,
}
}
}
/// Default minutes between scheduler passes (SPEC-ENGINE §3.16).
pub const DEFAULT_PULL_INTERVAL_MINUTES: u32 = 5;
/// Allowed `pull_interval_minutes` (SPEC-ENGINE §3.16).
pub const PULL_INTERVAL_BOUNDS: std::ops::RangeInclusive<u32> = 1..=60;
impl CloudConfig {
/// `pull_interval_minutes` within [`PULL_INTERVAL_BOUNDS`].
pub fn pull_interval(&self) -> u32 {
self.pull_interval_minutes
.clamp(*PULL_INTERVAL_BOUNDS.start(), *PULL_INTERVAL_BOUNDS.end())
}
}
/// Applies the `[cloud]` off switches to the connector's local state.
pub type OffSwitches = fn(&CloudConfig);
static OFF_SWITCHES: OnceLock<OffSwitches> = OnceLock::new();
/// Register what a `config set` of a `cloud.` key applies at once (the
/// connector stops its local recording when a switch is off). The first
/// registration wins.
pub fn register_off_switches(apply: OffSwitches) {
let _ = OFF_SWITCHES.set(apply);
}
/// Apply the off switches in `config`. Without a registration there is no
/// connector state to stop, so nothing happens.
pub(crate) fn apply_off_switches(config: &CloudConfig) {
if let Some(apply) = OFF_SWITCHES.get() {
apply(config);
}
}
/// `[cloud.worker]`: stricter local caps on top of the server's budget.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct CloudWorkerConfig {
#[serde(skip_serializing_if = "Option::is_none")]
pub max_tasks_per_day: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_tokens_per_day: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_usd_micros_per_day: Option<u64>,
}
/// `[cloud]` in `lean-ctx config schema` / `config validate`.
pub fn schema_section() -> crate::core::config::schema::SectionSchema {
use crate::core::config::schema::{KeySchema, SectionSchema};
let defaults = CloudConfig::default();
let key = |ty: &str, default: serde_json::Value, description: &str, values: Option<&[&str]>| {
KeySchema {
ty: ty.into(),
default,
description: description.into(),
values: values.map(|v| v.iter().map(|s| (*s).to_string()).collect()),
env_override: None,
}
};
let keys = [
(
"enabled",
key(
"bool",
serde_json::Value::Null,
"Pause all LeanCTX traffic with false; unset means on while this computer is connected.",
None,
),
),
(
"url",
key(
"string",
serde_json::json!(CLOUD_DEFAULT_URL),
"LeanCTX origin (https; a self-hosted LeanCTX server may use any https URL).",
None,
),
),
(
"root_key",
key(
"string",
serde_json::Value::Null,
"Root public key (base64 Ed25519) of a self-hosted LeanCTX server.",
None,
),
),
(
"auto_capture",
key(
"bool",
serde_json::json!(defaults.auto_capture),
"Run the capture gate on session learnings and share what passes (renamed from auto_draft).",
None,
),
),
(
"pull_interval_minutes",
key(
"integer",
serde_json::json!(defaults.pull_interval_minutes),
"Minutes between LeanCTX sync passes (1-60; values outside are clamped).",
None,
),
),
(
"metrics_upload",
key(
"bool",
serde_json::json!(defaults.metrics_upload),
"Send hourly instance metrics to LeanCTX (counts only, including full re-fetches and unregulated program names; never content).",
None,
),
),
(
"receipts_upload",
key(
"bool",
serde_json::json!(defaults.receipts_upload),
"Upload context receipts to LeanCTX.",
None,
),
),
(
"bus",
key(
"string",
serde_json::json!("auto"),
"Relay the agent bus through LeanCTX (auto) or keep it local (off).",
Some(&["auto", "off"]),
),
),
(
"queen_identity",
KeySchema {
env_override: Some("LEAN_CTX_CLOUD_QUEEN_IDENTITY".into()),
..key(
"string",
serde_json::json!("warn"),
"A bus sender presenting as the app's coordinator session that is no registered coordinator session: warn (pass, log in bus/queen-warnings.json, mark for the receiver) or enforce (refuse); any other value counts as enforce. Enforce only once queen-sessions.json lists every coordinator session.",
Some(&["warn", "enforce"]),
)
},
),
(
"vault_labels",
key(
"bool",
serde_json::json!(defaults.vault_labels),
"Send checkout folder names as vault labels.",
None,
),
),
(
"presence_names",
key(
"bool",
serde_json::json!(defaults.presence_names),
"Show your team what your agents are working on: the status line each agent sets itself, with paths and secrets removed (only through a server that shows it to your team alone); false keeps presence to role, state and this machine's short name.",
None,
),
),
(
"runtime_consent",
key(
"bool",
serde_json::json!(defaults.runtime_consent),
"You agreed to install the proprietary Intelligence runtime your plan licenses (set by `cloud connect` or `cloud runtime install`).",
None,
),
),
(
"models_url",
key(
"string",
serde_json::Value::Null,
"Origin of the LeanCTX models gateway for the leanctx provider; unset means https://models.<cloud host>. Only the LeanCTX host or a direct subdomain of it is honoured.",
None,
),
),
(
"models_anthropic_agents",
key(
"bool",
serde_json::json!(defaults.models_anthropic_agents),
"Let Anthropic-shaped agents reach the subscription models through shape-xlat routing aliases; false means OpenAI-shaped agents only. Owner question Q3 is open: true takes effect only once it is decided.",
None,
),
),
(
"worker.max_tasks_per_day",
key(
"integer",
serde_json::Value::Null,
"Worker mode: at most this many claimed tasks per day on this machine; only tightens the opt-in budget set on the web.",
None,
),
),
(
"worker.max_tokens_per_day",
key(
"integer",
serde_json::Value::Null,
"Worker mode: stop claiming once the day's results reported this many tokens; only tightens the opt-in budget.",
None,
),
),
(
"worker.max_usd_micros_per_day",
key(
"integer",
serde_json::Value::Null,
"Worker mode: stop claiming once the day's results reported this cost (USD millionths); only tightens the opt-in budget.",
None,
),
),
];
SectionSchema {
description: "LeanCTX: workspace knowledge, policies and the shared agent bus.".into(),
keys: keys
.into_iter()
.map(|(name, key)| (name.to_string(), key))
.collect(),
}
}