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
// SPDX-License-Identifier: Apache-2.0
//! `[hive]` configuration.
//!
//! ```toml
//! [hive]
//! # enabled = true # unset: on while this machine is connected
//! url = "https://hive.leanctx.com"
//! # root_key = "<base64>" # self-hosted Hive: 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
//!
//! [hive.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 Hive connector reads this section and re-exports these types as
//! its own config. It also registers what a `config set`
//! of a `hive.` key applies at once ([`register_off_switches`]).
use std::sync::OnceLock;
use serde::{Deserialize, Serialize};
use lean_ctx_protocol::hive::HIVE_DEFAULT_URL;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HiveBusMode {
#[default]
Auto,
Off,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct HiveConfig {
/// `None`: on while this machine is connected. `false` pauses all Hive
/// traffic without disconnecting.
#[serde(skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>,
/// Hive origin; any `https` URL for a self-hosted Hive.
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
/// Root public key (base64 Ed25519) of a self-hosted Hive.
#[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; [`HiveConfig::pull_interval`]
/// clamps it to [`PULL_INTERVAL_BOUNDS`].
pub pull_interval_minutes: u32,
pub metrics_upload: bool,
pub receipts_upload: bool,
pub bus: HiveBusMode,
pub vault_labels: bool,
/// The user agreed to install the proprietary Intelligence runtime that
/// the lease licenses (`hive connect` asks, `hive 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: HiveWorkerConfig,
}
impl Default for HiveConfig {
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: HiveBusMode::Auto,
vault_labels: true,
runtime_consent: false,
worker: HiveWorkerConfig::default(),
}
}
}
/// 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 HiveConfig {
/// `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 `[hive]` off switches to the connector's local state.
pub type OffSwitches = fn(&HiveConfig);
static OFF_SWITCHES: OnceLock<OffSwitches> = OnceLock::new();
/// Register what a `config set` of a `hive.` 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: &HiveConfig) {
if let Some(apply) = OFF_SWITCHES.get() {
apply(config);
}
}
/// `[hive.worker]`: stricter local caps on top of the server's budget.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct HiveWorkerConfig {
#[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>,
}
/// `[hive]` 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 = HiveConfig::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 Hive traffic with false; unset means on while this machine is connected.",
None,
),
),
(
"url",
key(
"string",
serde_json::json!(HIVE_DEFAULT_URL),
"Hive origin (https; a self-hosted Hive may use any https URL).",
None,
),
),
(
"root_key",
key(
"string",
serde_json::Value::Null,
"Root public key (base64 Ed25519) of a self-hosted Hive.",
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 Hive sync passes (1-60; values outside are clamped).",
None,
),
),
(
"metrics_upload",
key(
"bool",
serde_json::json!(defaults.metrics_upload),
"Send hourly instance metrics to Hive (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 Hive.",
None,
),
),
(
"bus",
key(
"string",
serde_json::json!("auto"),
"Relay the agent bus through Hive (auto) or keep it local (off).",
Some(&["auto", "off"]),
),
),
(
"vault_labels",
key(
"bool",
serde_json::json!(defaults.vault_labels),
"Send checkout folder names as vault labels.",
None,
),
),
(
"runtime_consent",
key(
"bool",
serde_json::json!(defaults.runtime_consent),
"You agreed to install the proprietary Intelligence runtime your plan licenses (set by `hive connect` or `hive runtime install`).",
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 | Hive: workspace knowledge, policies and the shared agent bus."
.into(),
keys: keys
.into_iter()
.map(|(name, key)| (name.to_string(), key))
.collect(),
}
}