lean-ctx 3.12.0

LeanCTX Engine: the open-source runtime for the Context Gateway for AI Systems. CLI, MCP server, and supported proxy integrations.
Documentation
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// SPDX-License-Identifier: Apache-2.0
//! Quota and policy throttles (Release B, Hive API §7.2 and §8.1).
//!
//! When Hive answers `429 quota_exceeded` (monthly transfer or bus
//! messages), `409 quota_exceeded` for a cooling-down Personal engine slot, or
//! `403 policy_denied`, the engine records a throttle in
//! `<data_dir>/throttles.json` and every path it applies to waits instead
//! of asking again: sync and pushes, bus publishes, task claims, connect.
//! The local work stays where it is (the replica's pending events, the bus
//! queue, the outbox) and goes once the throttle ends.
//!
//! Backoff rules (deterministic, never a busy loop):
//! - monthly quota (429): until `now + retry_after_s` (the body's value, or
//!   the `Retry-After` header), else `reset_at`, else the start of the next
//!   calendar month (UTC), the reset the contract names; at least
//!   [`MIN_WAIT_S`], at most [`MAX_WAIT_S`];
//! - Personal slot cool-down (409 `registered_engines`, `cooldown_days`): until
//!   `reset_at` (the web recovery frees it sooner, P7);
//! - policy refusal (403) of a bus publish or a task claim:
//!   [`POLICY_HOLD_S`] per member and path, then one try (an admin may have
//!   changed the policy meanwhile), and at once when a newer policy is
//!   installed ([`policy_changed`]); a sync refusal holds only its draft
//!   (outbox, same wait, also ended by a newer policy) or its vault sync,
//!   never the other vaults;
//! - other counted quotas (409 `private_vaults`, a slot without
//!   cool-down): no throttle; the refusal is shown and nothing retries it
//!   by itself.
//!
//! `lean-ctx hive status` lists the active throttles ([`status_lines`]).

use std::collections::BTreeMap;

use chrono::{DateTime, Datelike as _, TimeZone as _, Utc};
use serde::{Deserialize, Serialize};

use super::HiveError;
use super::state::{HiveStore, load_or_quarantine, lock_state, write_private};

pub(crate) const THROTTLE_FILE: &str = "throttles.json";
/// The shortest wait of a throttle: one tick of a loop is never a retry.
pub const MIN_WAIT_S: i64 = 60;
/// The longest wait (a reset further out is shortened to this).
pub const MAX_WAIT_S: i64 = 32 * 86_400;
/// How long a policy refusal holds its path.
pub const POLICY_HOLD_S: i64 = 15 * 60;
/// A quota throttle lets one call through this often before it ends, so a
/// quota that was raised or lowered in Hive is noticed (E2E-FINAL: sync
/// waited for the month's end after usage was lowered).
pub const QUOTA_PROBE_S: i64 = 3600;

/// The engine paths a throttle can hold.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Scope {
    /// Knowledge sync, event tail and push, snapshots (transfer).
    Sync,
    /// Bus publishes (bus messages).
    Bus,
    /// Task claims, renewals and results.
    Tasks,
    /// A new engine registration (`hive connect`).
    Connect,
}

impl Scope {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Sync => "sync",
            Self::Bus => "bus",
            Self::Tasks => "tasks",
            Self::Connect => "connect",
        }
    }
}

/// The paths a quota resource holds besides the one that met it.
fn resource_scopes(resource: &str) -> &'static [Scope] {
    match resource {
        // API §8.1: transfer counts the engine data routes (event push and
        // tail, snapshots and segments, knowledge, graphs, bus history).
        "transfer_bytes" => &[Scope::Sync],
        "bus_messages" => &[Scope::Bus],
        "registered_engines" => &[Scope::Connect],
        _ => &[],
    }
}

/// One recorded throttle.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Throttle {
    /// Sorted scope names it holds.
    pub scopes: Vec<String>,
    /// When it ends, second precision `Z`.
    pub until: String,
    /// The refusal, as `HiveError` shows it.
    pub line: String,
    /// A quota throttle's next probe ([`QUOTA_PROBE_S`]): from then one
    /// call may pass, and its success ends the throttle ([`passed`]).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub probe_at: Option<String>,
}

impl Throttle {
    fn holds(&self, scope: Scope) -> bool {
        self.scopes.iter().any(|s| s == scope.as_str())
    }

    fn active_at(&self, now: DateTime<Utc>) -> bool {
        DateTime::parse_from_rfc3339(&self.until).is_ok_and(|until| until > now)
    }

    /// The error a held path answers instead of calling Hive.
    #[must_use]
    pub fn refusal(&self) -> HiveError {
        HiveError::Refused(format!(
            "waiting for Hive until {}: {}",
            self.until, self.line
        ))
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct Throttles {
    /// Key (`quota:<resource>` or `policy:<member>:<scope>`) → throttle.
    #[serde(default)]
    items: BTreeMap<String, Throttle>,
}

fn stamp(at: DateTime<Utc>) -> String {
    at.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
}

/// 00:00 UTC on the first day of the month after `now`'s.
fn next_month(now: DateTime<Utc>) -> DateTime<Utc> {
    let (year, month) = if now.month() == 12 {
        (now.year() + 1, 1)
    } else {
        (now.year(), now.month() + 1)
    };
    Utc.with_ymd_and_hms(year, month, 1, 0, 0, 0)
        .single()
        .unwrap_or(now + chrono::Duration::days(31))
}

/// `until` itself when it is [`MIN_WAIT_S`]..=[`MAX_WAIT_S`] away (so a
/// `reset_at` is kept to the second), else the nearer bound.
fn clamp(now: DateTime<Utc>, until: DateTime<Utc>) -> DateTime<Utc> {
    let wait = (until - now).num_seconds();
    if (MIN_WAIT_S..=MAX_WAIT_S).contains(&wait) {
        until
    } else {
        now + chrono::Duration::seconds(wait.clamp(MIN_WAIT_S, MAX_WAIT_S))
    }
}

/// The throttle `error` sets at `now`, if it sets one: on the paths its
/// quota resource holds plus `scope`, the path that met it (`None`: an
/// HTTP call of unknown path, so only the resource's paths; a policy
/// refusal then sets nothing).
#[must_use]
pub fn of_error(
    scope: Option<Scope>,
    error: &HiveError,
    now: DateTime<Utc>,
) -> Option<(String, Throttle)> {
    let (key, scopes, until) = match error {
        HiveError::QuotaExceeded(q) if q.waits() => {
            let until = match error.retry_after_at(now) {
                // Clamped before the arithmetic: a huge `retry_after_s`
                // (up to u64::MAX) must not overflow chrono (a panic).
                Some(seconds) => {
                    let wait = i64::try_from(seconds)
                        .unwrap_or(MAX_WAIT_S)
                        .clamp(MIN_WAIT_S, MAX_WAIT_S);
                    now + chrono::Duration::seconds(wait)
                }
                None => next_month(now),
            };
            (
                format!("quota:{}", q.resource),
                resource_scopes(&q.resource),
                clamp(now, until),
            )
        }
        HiveError::QuotaExceeded(q) if q.cooling_slot() => {
            let reset = DateTime::parse_from_rfc3339(q.reset_at.as_deref()?)
                .ok()?
                .with_timezone(&Utc);
            (
                format!("quota:{}", q.resource),
                resource_scopes(&q.resource),
                clamp(now, reset),
            )
        }
        HiveError::PolicyDenied(d) => (
            format!(
                "policy:{}:{}",
                d.member.as_deref().unwrap_or("unknown"),
                scope?.as_str()
            ),
            &[][..],
            now + chrono::Duration::seconds(POLICY_HOLD_S),
        ),
        _ => return None,
    };
    let mut names: Vec<String> = scope
        .into_iter()
        .chain(scopes.iter().copied())
        .map(|s| s.as_str().to_string())
        .collect();
    names.sort();
    names.dedup();
    if names.is_empty() {
        return None;
    }
    let probe = now + chrono::Duration::seconds(QUOTA_PROBE_S);
    let probe_at = (key.starts_with("quota:") && until > probe).then(|| stamp(probe));
    Some((
        key,
        Throttle {
            scopes: names,
            until: stamp(until),
            line: error.to_string(),
            probe_at,
        },
    ))
}

fn read(store: &HiveStore) -> Throttles {
    load_or_quarantine(&store.data_dir().join(THROTTLE_FILE), |_: &Throttles| true)
        .ok()
        .flatten()
        .unwrap_or_default()
}

fn change(store: &HiveStore, edit: impl FnOnce(&mut Throttles)) -> Result<(), HiveError> {
    let dir = store.data_dir();
    let _lock = lock_state(dir, THROTTLE_FILE)?;
    let mut throttles = read(store);
    edit(&mut throttles);
    let bytes = serde_json::to_vec(&throttles).map_err(|e| HiveError::Local(e.to_string()))?;
    write_private(dir, THROTTLE_FILE, &bytes)
}

/// Record the throttle `error` sets on `scope` (expired ones are dropped
/// on the way) and pass the error on unchanged.
pub fn note(store: &HiveStore, scope: Scope, error: HiveError, now: DateTime<Utc>) -> HiveError {
    note_on(store, Some(scope), error, now)
}

/// [`note`] for an answer of any engine route: a quota holds the paths its
/// resource names (every authenticated call passes here, `Session`).
pub fn note_on(
    store: &HiveStore,
    scope: Option<Scope>,
    error: HiveError,
    now: DateTime<Utc>,
) -> HiveError {
    if let Some((key, throttle)) = of_error(scope, &error, now)
        && let Err(e) = change(store, |t| {
            t.items.retain(|_, v| v.active_at(now));
            t.items.insert(key, throttle);
        })
    {
        tracing::warn!("hive: could not record a throttle: {e}");
    }
    error
}

/// The active throttle holding `scope` at `now` (the one ending last).
#[must_use]
pub fn holding(store: &HiveStore, scope: Scope, now: DateTime<Utc>) -> Option<Throttle> {
    read(store)
        .items
        .into_values()
        .filter(|t| t.holds(scope) && t.active_at(now))
        .max_by(|a, b| a.until.cmp(&b.until))
}

/// `Err` with the throttle's refusal while `scope` is held. When only
/// quota throttles hold it and their probe is due, one call passes (the
/// next probe is an hour later): a success ends them ([`passed`]), a new
/// refusal records them again.
pub fn check(store: &HiveStore, scope: Scope, now: DateTime<Utc>) -> Result<(), HiveError> {
    let Some(held) = holding(store, scope, now) else {
        return Ok(());
    };
    let due = |t: &Throttle| {
        t.probe_at
            .as_deref()
            .and_then(|p| DateTime::parse_from_rfc3339(p).ok())
            .is_some_and(|p| p <= now)
    };
    let throttles = read(store);
    let active: Vec<(&String, &Throttle)> = throttles
        .items
        .iter()
        .filter(|(_, t)| t.holds(scope) && t.active_at(now))
        .collect();
    if !active
        .iter()
        .all(|(k, t)| k.starts_with("quota:") && due(t))
    {
        return Err(held.refusal());
    }
    let next = stamp(now + chrono::Duration::seconds(QUOTA_PROBE_S));
    let keys: Vec<String> = active.iter().map(|(k, _)| (*k).clone()).collect();
    let _ = change(store, |t| {
        for k in &keys {
            if let Some(v) = t.items.get_mut(k) {
                v.probe_at = Some(next.clone());
            }
        }
    });
    Ok(())
}

/// Whether a quota throttle (not only a policy hold) holds `scope` now.
#[must_use]
pub fn quota_holds(store: &HiveStore, scope: Scope, now: DateTime<Utc>) -> bool {
    read(store)
        .items
        .iter()
        .any(|(k, t)| k.starts_with("quota:") && t.holds(scope) && t.active_at(now))
}

/// The quota is back (a successful call, or an answer that shows it):
/// every quota throttle holding `scope` ends.
pub fn quota_lifted(store: &HiveStore, scope: Scope) {
    if read(store)
        .items
        .iter()
        .any(|(k, t)| k.starts_with("quota:") && t.holds(scope))
    {
        let _ = change(store, |t| {
            t.items
                .retain(|k, v| !(k.starts_with("quota:") && v.holds(scope)));
        });
    }
}

/// A successful call on `scope`: its policy holds and quota throttles are
/// over (a probe that passed, or a quota raised in Hive meanwhile).
pub fn passed(store: &HiveStore, scope: Scope) {
    let suffix = format!(":{}", scope.as_str());
    if read(store)
        .items
        .keys()
        .any(|k| k.starts_with("policy:") && k.ends_with(&suffix))
    {
        let _ = change(store, |t| {
            t.items
                .retain(|k, _| !(k.starts_with("policy:") && k.ends_with(&suffix)));
        });
    }
    quota_lifted(store, scope);
}

/// The workspace's plan changed (a newer lease): every quota throttle was
/// the old plan's answer and ends; policy holds stay.
pub fn plan_changed(store: &HiveStore) {
    if read(store).items.keys().any(|k| k.starts_with("quota:")) {
        let _ = change(store, |t| t.items.retain(|k, _| !k.starts_with("quota:")));
    }
}

/// A newer policy is in force: every policy hold is over, on every path
/// (each was the old policy's answer), and so is every outbox item's
/// policy wait (a refused knowledge draft); quota throttles stay.
pub fn policy_changed(store: &HiveStore) {
    if read(store).items.keys().any(|k| k.starts_with("policy:")) {
        let _ = change(store, |t| t.items.retain(|k, _| !k.starts_with("policy:")));
    }
    super::outbox::Outbox::at(store.data_dir()).release_policy_holds();
}

/// A new enrolment starts without the old connect throttle.
pub fn clear_connect(store: &HiveStore) {
    let _ = change(store, |t| {
        t.items.retain(|_, v| !v.holds(Scope::Connect));
    });
}

/// `hive status` lines of every active throttle, sorted by key (stable).
#[must_use]
pub fn status_lines(store: &HiveStore, now: DateTime<Utc>) -> Vec<String> {
    read(store)
        .items
        .values()
        .filter(|t| t.active_at(now))
        .map(|t| {
            format!(
                "Throttled:    {} until {}: {}",
                t.scopes.join(", "),
                t.until,
                t.line
            )
        })
        .collect()
}

#[cfg(test)]
#[path = "throttle_tests.rs"]
mod tests;