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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// SPDX-License-Identifier: Apache-2.0
//! Cross-instance leases through Hive (C5c).
//!
//! `ctx_agent lease_acquire` takes the local lease first (same-machine
//! contention needs no network), then asks Hive with one bounded request
//! ([`acquire`]): plan C rule 3 exception, at most [`BUDGET`] in total, no
//! retries, and only while this machine is enrolled with a Valid or Grace
//! lease that has `bus.relay` and the project is linked. Otherwise the
//! answer is [`Outcome::Local`] at once, without a request, and the agent is
//! told the lease does not exclude other machines. There is no background
//! upgrade.
//!
//! The Hive side of a local lease is kept in `<data>/hive/bus/leases.json`
//! (beside, not inside, the machine-wide lease store, whose schema other
//! processes read strictly). Releases are queued there and sent by the relay;
//! a lost release is covered by the service's own expiry. Tool calls and the
//! relay change it only through [`Bindings::mutate`], under `leases.json.lock`,
//! and never hold that lock across a request (RB8 E5): read, ask Hive,
//! then apply the answer to what is on disk by then.

use std::collections::BTreeMap;
use std::path::Path;
use std::time::{Duration, Instant};

use serde::{Deserialize, Serialize};
use serde_json::json;
use sha2::{Digest, Sha256};

use crate::core::hive::HiveError;
use crate::core::hive::auth::Session;
use crate::core::hive::config::{HiveBusMode, HiveConfig};
use crate::core::hive::state::{HiveStore, load_or_quarantine, lock_state, write_private};
use crate::core::hive::vault::VaultMap;
use lean_ctx_protocol::hive::bus::{
    BUS_LEASE_RENEW_REQUEST_SCHEMA, BUS_LEASE_REQUEST_SCHEMA, BusLeaseV1, MAX_BUS_LEASE_MS,
};
use lean_ctx_protocol::hive::lease::{HiveRight, LeaseState};

/// Whole budget of one acquire or renew made from a tool call.
pub const BUDGET: Duration = Duration::from_secs(3);
const LEASES_FILE: &str = "leases.json";
/// Queued releases kept at most; the oldest go first, the service lets
/// their leases expire (RB8 M14).
const MAX_RELEASES: usize = 256;
/// A binding this long past its expiry is dropped: Hive forgot it too.
const STALE_BINDING: chrono::Duration = chrono::Duration::days(1);

/// What Hive answered, or why it was not asked.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
    Granted {
        lease_id: String,
        fencing_token: u64,
        expires_at: String,
    },
    /// Another agent (possibly on another machine) holds it.
    Held {
        agent_id: String,
        instance_id: String,
        expires_at: String,
    },
    /// An organization veto bars this agent from the resource.
    Vetoed,
    /// Hive no longer knows the lease (expired on the service clock or superseded).
    Superseded,
    /// Hive was not asked or did not answer in time: local only.
    Local(String),
}

/// The Hive lease behind a local one.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Binding {
    pub lease_id: String,
    pub fencing_token: u64,
    /// On disk as `project_id` (pre-VA name) for downgrades and mixed-version teams.
    #[serde(rename = "project_id", alias = "vault_id")]
    pub vault_id: String,
    pub expires_at: String,
}

/// A release still to send.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Release {
    pub lease_id: String,
    pub fencing_token: u64,
}

/// `leases.json`: local `lease_ref` → Hive lease, plus queued releases.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Bindings {
    #[serde(default)]
    pub leases: BTreeMap<String, Binding>,
    #[serde(default)]
    pub releases: Vec<Release>,
}

impl Bindings {
    /// The bindings on disk. A damaged file is moved aside (RB8 M12) and the
    /// bindings start empty: the service expires what this machine forgot.
    pub fn load(data_dir: &Path) -> Self {
        load_or_quarantine(&data_dir.join("bus").join(LEASES_FILE), |_: &Self| true)
            .unwrap_or_else(|error| {
                tracing::warn!("hive lease bindings: {error}");
                None
            })
            .unwrap_or_default()
    }

    fn save(&self, data_dir: &Path) -> Result<(), HiveError> {
        let bytes = serde_json::to_vec(self).map_err(|e| HiveError::Local(e.to_string()))?;
        write_private(&data_dir.join("bus"), LEASES_FILE, &bytes)
    }

    /// Load, change and save under the bindings lock (RB8 E5). Stale
    /// bindings and releases past `MAX_RELEASES` go (M14). Never call it
    /// around a request.
    pub fn mutate<T>(data_dir: &Path, change: impl FnOnce(&mut Self) -> T) -> Result<T, HiveError> {
        let _lock = lock_state(&data_dir.join("bus"), LEASES_FILE)?;
        let mut bindings = Self::load(data_dir);
        let before = bindings.clone();
        let out = change(&mut bindings);
        bindings.prune(chrono::Utc::now());
        if bindings != before {
            bindings.save(data_dir)?;
        }
        Ok(out)
    }

    fn prune(&mut self, now: chrono::DateTime<chrono::Utc>) {
        self.leases.retain(|_, b| {
            chrono::DateTime::parse_from_rfc3339(&b.expires_at)
                .map_or(true, |at| at.to_utc() + STALE_BINDING > now)
        });
        let excess = self.releases.len().saturating_sub(MAX_RELEASES);
        self.releases.drain(..excess);
    }
}

/// The machine-independent Hive `resource_ref` of a lease target in a
/// project: no path leaves the engine, and every checkout of the project
/// names a file the same way.
pub fn resource_ref(vault_id: &str, kind: &str, target: &str) -> String {
    let digest = Sha256::digest(
        format!("leanctx.hive.bus-lease.v1\n{vault_id}\n{kind}\n{target}").as_bytes(),
    );
    hex::encode(digest)
}

/// `(kind, normalized target)` of a `ctx_agent` lease target: `symbol:<name>`
/// or a path inside `project_root` (repo-relative, `/`-separated). `None`
/// for a path outside the project.
pub fn normalize_target(project_root: &Path, raw: &str) -> Option<(&'static str, String)> {
    let raw = raw.trim();
    if let Some(symbol) = raw.strip_prefix("symbol:") {
        let symbol = symbol.trim();
        return (!symbol.is_empty()).then(|| ("symbol", symbol.to_string()));
    }
    let path = Path::new(raw);
    // Anchored, not only `is_absolute`: on Windows a rooted path without a
    // drive (`/home/ada/x`, as another checkout reports it) is not absolute,
    // yet it names a place below its root, never a relative target.
    let anchored = path.has_root()
        || matches!(
            path.components().next(),
            Some(std::path::Component::Prefix(_))
        );
    let relative = if anchored {
        path.strip_prefix(project_root).ok()?
    } else {
        path
    };
    let mut parts: Vec<String> = Vec::new();
    for component in relative.components() {
        match component {
            std::path::Component::Normal(part) => parts.push(part.to_string_lossy().into_owned()),
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                parts.pop()?;
            }
            _ => return None,
        }
    }
    (!parts.is_empty()).then(|| ("file", parts.join("/")))
}

/// A connected machine and the Hive project of a local root.
pub(super) struct Connected {
    pub(super) store: HiveStore,
    pub(super) session: Session,
    pub(super) vault_id: String,
}

/// Whether Hive applies to `project_root`; `Err` says why not (no request made).
pub(super) fn connected(project_root: &Path) -> Result<Connected, String> {
    let config = crate::core::config::Config::load().hive.clone();
    connected_with(
        &config,
        HiveStore::open().map_err(|e| e.to_string())?,
        project_root,
    )
}

fn connected_with(
    config: &HiveConfig,
    store: HiveStore,
    project_root: &Path,
) -> Result<Connected, String> {
    if config.bus == HiveBusMode::Off || config.enabled == Some(false) {
        return Err("the Hive bus is off".into());
    }
    let instance = store
        .instance()
        .ok()
        .flatten()
        .ok_or_else(|| "this machine is not connected to Hive".to_string())?;
    let root = project_root.to_string_lossy();
    let vault_id = VaultMap::load(store.data_dir())
        .for_root(&root)
        .map(|p| p.vault_id.clone())
        .ok_or_else(|| "this repository is not linked to a vault".to_string())?;
    let session = config
        .root_key(&instance.url)
        .and_then(|root| Session::load(store.clone(), root))
        .map_err(|e| e.to_string())?;
    if !matches!(
        session.lease.state_at(chrono::Utc::now()),
        LeaseState::Valid | LeaseState::Grace
    ) {
        return Err("the Hive lease has lapsed".into());
    }
    if !session.lease.lease.grants(HiveRight::BusRelay) {
        return Err("the Hive lease does not include the bus".into());
    }
    Ok(Connected {
        store,
        session,
        vault_id,
    })
}

fn decode_lease(value: serde_json::Value) -> Result<BusLeaseV1, HiveError> {
    let lease: BusLeaseV1 = serde_json::from_value(value)
        .map_err(|e| HiveError::Protocol(format!("bus lease: {e}")))?;
    lease
        .validate()
        .map_err(|e| HiveError::Protocol(format!("bus lease: {e}")))?;
    Ok(lease)
}

fn outcome(answer: Result<BusLeaseV1, HiveError>) -> Outcome {
    match answer {
        Ok(BusLeaseV1 {
            granted: true,
            lease_id: Some(lease_id),
            fencing_token: Some(fencing_token),
            expires_at: Some(expires_at),
            ..
        }) => Outcome::Granted {
            lease_id,
            fencing_token,
            expires_at: expires_at.as_str().to_string(),
        },
        Ok(BusLeaseV1 {
            holder: Some(holder),
            ..
        }) => Outcome::Held {
            agent_id: holder.agent_id,
            instance_id: holder.instance_id,
            expires_at: holder.expires_at.as_str().to_string(),
        },
        Ok(_) => Outcome::Local("Hive sent an incomplete lease".into()),
        Err(HiveError::Vetoed) => Outcome::Vetoed,
        Err(HiveError::NotFound) => Outcome::Superseded,
        Err(HiveError::Offline(why)) => Outcome::Local(format!("Hive unreachable ({why})")),
        Err(error) => Outcome::Local(error.to_string()),
    }
}

/// Ask Hive for the lease the agent already holds locally (`local_ref`).
/// A grant is bound to `local_ref`; the caller releases the local lease on
/// anything but [`Outcome::Granted`] and [`Outcome::Local`].
pub fn acquire(
    project_root: &Path,
    agent_id: &str,
    target: &str,
    duration_ms: u64,
    local_ref: &str,
) -> Outcome {
    let connected = match connected(project_root) {
        Ok(connected) => connected,
        Err(why) => return Outcome::Local(why),
    };
    acquire_with(
        &connected,
        project_root,
        agent_id,
        target,
        duration_ms,
        local_ref,
    )
}

fn acquire_with(
    c: &Connected,
    project_root: &Path,
    agent_id: &str,
    target: &str,
    duration_ms: u64,
    local_ref: &str,
) -> Outcome {
    let Some((kind, normalized)) = normalize_target(project_root, target) else {
        return Outcome::Local("the target is outside the project".into());
    };
    let deadline = Instant::now() + BUDGET;
    let body = json!({
        "schema": BUS_LEASE_REQUEST_SCHEMA,
        "resource_kind": kind,
        "resource_ref": resource_ref(&c.vault_id, kind, &normalized),
        "vault_id": c.vault_id,
        "owner_agent_id": agent_id,
        "duration_ms": duration_ms.clamp(1, MAX_BUS_LEASE_MS),
    });
    let answer = c
        .session
        .bounded_client(deadline)
        .and_then(|client| client.post("/bus/leases", &body))
        .and_then(decode_lease);
    let result = outcome(answer);
    if let Outcome::Granted {
        lease_id,
        fencing_token,
        expires_at,
    } = &result
    {
        let binding = Binding {
            lease_id: lease_id.clone(),
            fencing_token: *fencing_token,
            vault_id: c.vault_id.clone(),
            expires_at: expires_at.clone(),
        };
        if let Err(error) = Bindings::mutate(c.store.data_dir(), |b| {
            b.leases.insert(local_ref.to_string(), binding);
        }) {
            // Unrecorded, the lease would never be renewed or released by us;
            // the service lets it expire.
            return Outcome::Local(format!("could not record the Hive lease: {error}"));
        }
    }
    result
}

/// Renew the Hive lease bound to `local_ref` (bounded like [`acquire`]).
/// A local-only lease gets [`Outcome::Local`] without a request.
pub fn renew(project_root: &Path, local_ref: &str, duration_ms: u64) -> Outcome {
    let Ok(store) = HiveStore::open() else {
        return Outcome::Local("no Hive state".into());
    };
    let Some(binding) = Bindings::load(store.data_dir())
        .leases
        .get(local_ref)
        .cloned()
    else {
        return Outcome::Local("this lease is local only".into());
    };
    let connected = match connected(project_root) {
        Ok(connected) => connected,
        Err(why) => return Outcome::Local(why),
    };
    let deadline = Instant::now() + BUDGET;
    let body = json!({
        "schema": BUS_LEASE_RENEW_REQUEST_SCHEMA,
        "fencing_token": binding.fencing_token,
        "duration_ms": duration_ms.clamp(1, MAX_BUS_LEASE_MS),
    });
    let path = format!("/bus/leases/{}/renew", binding.lease_id);
    let result = outcome(
        connected
            .session
            .bounded_client(deadline)
            .and_then(|client| client.post(&path, &body))
            .and_then(decode_lease),
    );
    // Applied to the binding as it is now, and only while it is still the
    // lease that was renewed (a release or a new grant may have come since).
    let applied = Bindings::mutate(store.data_dir(), |bindings| {
        let same = |b: &Binding| b.lease_id == binding.lease_id;
        match &result {
            Outcome::Granted { expires_at, .. } => {
                if let Some(b) = bindings.leases.get_mut(local_ref).filter(|b| same(b)) {
                    b.expires_at.clone_from(expires_at);
                }
            }
            Outcome::Superseded | Outcome::Vetoed
                if bindings.leases.get(local_ref).is_some_and(same) =>
            {
                bindings.leases.remove(local_ref);
            }
            _ => {}
        }
    });
    if let Err(error) = applied {
        tracing::debug!("hive lease bindings not saved: {error}");
    }
    result
}

/// The Hive lease bound to `local_ref`, if any.
pub fn binding(local_ref: &str) -> Option<Binding> {
    let store = HiveStore::open().ok()?;
    Bindings::load(store.data_dir())
        .leases
        .get(local_ref)
        .cloned()
}

/// Queue the release of the Hive lease bound to `local_ref` (the relay
/// sends it). No network in the tool call.
pub fn queue_release(local_ref: &str) {
    let Ok(store) = HiveStore::open() else {
        return;
    };
    let queued = Bindings::mutate(store.data_dir(), |bindings| {
        if let Some(b) = bindings.leases.remove(local_ref) {
            bindings.releases.push(Release {
                lease_id: b.lease_id,
                fencing_token: b.fencing_token,
            });
        }
    });
    if let Err(error) = queued {
        tracing::debug!("hive lease release not queued: {error}");
    }
}

/// The relay's half: send queued releases (blocking; run off the socket loop).
/// A release Hive no longer knows counts as done.
pub fn send_releases(store: &HiveStore, session: &Session) {
    let data = store.data_dir();
    // Sent without the lock; only the ones done leave the queue as it is by
    // then, so a release queued meanwhile stays (RB8 E5).
    let done: Vec<Release> = Bindings::load(data)
        .releases
        .into_iter()
        .filter(|r| {
            let path = format!(
                "/bus/leases/{}?fencing_token={}",
                r.lease_id, r.fencing_token
            );
            match session.with_client(|client| client.delete(&path)) {
                Ok(()) | Err(HiveError::NotFound) => true,
                Err(error) => matches!(
                    error,
                    HiveError::Forbidden | HiveError::Vetoed | HiveError::Protocol(_)
                ),
            }
        })
        .collect();
    if done.is_empty() {
        return;
    }
    if let Err(error) = Bindings::mutate(data, |b| b.releases.retain(|r| !done.contains(r))) {
        tracing::debug!("hive lease releases not saved: {error}");
    }
}

/// One line for `ctx_agent`: where the lease holds.
pub fn scope_line(outcome: &Outcome) -> String {
    match outcome {
        Outcome::Granted { fencing_token, .. } => {
            format!("scope=hive fencing_token={fencing_token}")
        }
        Outcome::Local(why) => {
            format!("scope=local ({why}; other machines are not excluded)")
        }
        Outcome::Held { .. } | Outcome::Vetoed | Outcome::Superseded => "scope=hive".into(),
    }
}

#[cfg(test)]
mod vault_rename_tests {
    use super::*;

    /// `Binding` of the engine before the VA rename.
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct PreVaBinding {
        lease_id: String,
        fencing_token: u64,
        project_id: String,
        expires_at: String,
    }

    #[test]
    fn lease_bindings_keep_pre_va_names() {
        let old = r#"{"lease_id":"lse_1","fencing_token":4,"project_id":"v1","expires_at":"2026-10-22T10:00:00Z"}"#;
        let read = crate::core::hive::vault::assert_pre_va_round_trip::<Binding, PreVaBinding>(
            old,
            "project_id",
            "vault_id",
        );
        assert_eq!((read.vault_id.as_str(), read.fencing_token), ("v1", 4));
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn every_checkout_names_a_file_the_same_way() {
        let a = Path::new("/home/ada/src/payments");
        let b = Path::new("/Users/bob/work/payments-clone");
        let (kind, ta) = normalize_target(a, "/home/ada/src/payments/src/lib.rs").unwrap();
        let (_, tb) = normalize_target(b, "./src/../src/lib.rs").unwrap();
        assert_eq!((kind, ta.as_str()), ("file", "src/lib.rs"));
        assert_eq!(ta, tb);
        let pid = "0123456789abcdef0123456789abcdef";
        let ref_a = resource_ref(pid, kind, &ta);
        assert_eq!(ref_a, resource_ref(pid, kind, &tb));
        assert_eq!(ref_a.len(), 64);
        assert!(!ref_a.contains("lib"));
        assert_ne!(
            ref_a,
            resource_ref("fedcba9876543210fedcba9876543210", kind, &ta)
        );
        assert_ne!(ref_a, resource_ref(pid, "symbol", &ta));
        assert_eq!(
            normalize_target(a, "symbol: Parser::parse"),
            Some(("symbol", "Parser::parse".into()))
        );
        assert_eq!(
            normalize_target(a, "/etc/passwd"),
            None,
            "outside the project"
        );
        assert_eq!(normalize_target(a, "../other/x.rs"), None);
        assert_eq!(normalize_target(a, "symbol:"), None);
    }

    #[test]
    fn answers_map_to_outcomes_and_the_scope_is_always_named() {
        let granted = serde_json::json!({"schema": "leanctx.hive.bus-lease/v1", "granted": true,
            "lease_id": "bl_1", "fencing_token": 4, "expires_at": "2026-10-21T12:10:00Z",
            "server_time": "2026-10-21T12:00:00Z"});
        let out = outcome(decode_lease(granted));
        assert_eq!(scope_line(&out), "scope=hive fencing_token=4");
        let held = serde_json::json!({"schema": "leanctx.hive.bus-lease/v1", "granted": false,
            "holder": {"agent_id": "beta", "instance_id": "inst_b", "expires_at": "2026-10-21T12:10:00Z"},
            "server_time": "2026-10-21T12:00:00Z"});
        assert!(
            matches!(outcome(decode_lease(held)), Outcome::Held { agent_id, .. } if agent_id == "beta")
        );
        assert_eq!(outcome(Err(HiveError::Vetoed)), Outcome::Vetoed);
        assert_eq!(outcome(Err(HiveError::NotFound)), Outcome::Superseded);
        let offline = outcome(Err(HiveError::Offline("timeout".into())));
        assert!(scope_line(&offline).starts_with("scope=local (Hive unreachable (timeout)"));
        assert!(scope_line(&offline).ends_with("other machines are not excluded)"));
    }

    #[test]
    fn without_an_enrollment_no_request_is_made() {
        let dir = tempfile::tempdir().unwrap();
        let store = HiveStore::at(dir.path().join("c"), dir.path().join("d"));
        let err = connected_with(&HiveConfig::default(), store.clone(), dir.path())
            .err()
            .unwrap();
        assert_eq!(err, "this machine is not connected to Hive");
        let off = HiveConfig {
            bus: HiveBusMode::Off,
            ..HiveConfig::default()
        };
        assert_eq!(
            connected_with(&off, store, dir.path()).err().unwrap(),
            "the Hive bus is off"
        );
    }

    #[test]
    fn bindings_round_trip_and_queue_releases() {
        let dir = tempfile::tempdir().unwrap();
        let mut b = Bindings::default();
        b.leases.insert(
            "lease-ref:1".into(),
            Binding {
                lease_id: "bl_1".into(),
                fencing_token: 2,
                vault_id: "p".into(),
                expires_at: "2026-10-21T12:10:00Z".into(),
            },
        );
        b.save(dir.path()).unwrap();
        assert_eq!(Bindings::load(dir.path()), b);
    }
}