lean-ctx-cloud 3.12.9

The LeanCTX Cloud connector: enrollment, bus, knowledge sync, tasks and policy on top of LeanCTX | OSS.
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
// SPDX-License-Identifier: Apache-2.0
//! Cross-instance leases through Cloud (C5c).
//!
//! `ctx_agent lease_acquire` takes the local lease first (same-machine
//! contention needs no network), then asks Cloud off the tool call
//! ([`acquire`], connector rule "Cloud never blocks a tool call"): one
//! request of at most [`BUDGET`] in the in-process queue
//! ([`crate::background`]), 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, and the
//! agent is told the lease does not exclude other machines.
//!
//! Until Cloud answered `granted` the Cloud side is pending
//! ([`Outcome::Pending`]): no fencing token, nothing bound or announced.
//! What Cloud answered (granted, held by another agent, vetoed, unreachable)
//! comes with the agent's next `ctx_agent` call, and a refusal releases the
//! local lease. A renewal or release needs Cloud's `lease_id`, so it is
//! queued behind the grant. `POST /v1/bus/leases` is idempotent for the
//! holder (a re-acquire extends the same lease and token).
//!
//! The Cloud side of a local lease is kept in `<data>/cloud/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 Cloud,
//! then apply the answer to what is on disk by then.

use crate::CloudConfigExt as _;
use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;
use std::sync::{Mutex, MutexGuard, PoisonError};
use std::time::{Duration, Instant};

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

use crate::CloudError;
use crate::auth::Session;
use crate::config::{CloudBusMode, CloudConfig};
use crate::state::{CloudStore, load_or_quarantine, lock_state, write_private};
use lean_ctx_protocol::cloud::bus::{
    BUS_LEASE_RENEW_REQUEST_SCHEMA, BUS_LEASE_REQUEST_SCHEMA, BusLeaseV1, MAX_BUS_LEASE_MS,
};
use lean_ctx_protocol::cloud::lease::{CloudRight, LeaseState};

/// Whole budget of one acquire or renew a tool call queued.
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: Cloud forgot it too.
const STALE_BINDING: lean_ctx_time::Duration = lean_ctx_time::Duration::days(1);

/// What Cloud 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,
    /// Cloud no longer knows the lease (expired on the service clock or superseded).
    Superseded,
    /// Cloud was not asked or did not answer in time: local only.
    Local(String),
    /// Cloud is being asked off the tool call; its answer comes with the
    /// agent's next `ctx_agent` call. No fencing token yet.
    Pending,
}

/// Local lease refs whose Cloud acquire is queued or running in this
/// process: their renewal and release wait behind it.
static PENDING: Mutex<BTreeSet<String>> = Mutex::new(BTreeSet::new());

fn pending() -> MutexGuard<'static, BTreeSet<String>> {
    PENDING.lock().unwrap_or_else(PoisonError::into_inner)
}

/// The queue caller of `agent_id`'s `ctx_agent` calls
/// ([`crate::background::with_answers`]).
pub fn caller(agent_id: &str) -> String {
    format!("ctx_agent:{agent_id}")
}

/// The Cloud 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` → Cloud 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!("cloud lease bindings: {error}");
                None
            })
            .unwrap_or_default()
    }

    fn save(&self, data_dir: &Path) -> Result<(), CloudError> {
        let bytes = serde_json::to_vec(self).map_err(|e| CloudError::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, CloudError> {
        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(lean_ctx_time::UtcDateTime::now());
        if bindings != before {
            bindings.save(data_dir)?;
        }
        Ok(out)
    }

    fn prune(&mut self, now: lean_ctx_time::UtcDateTime) {
        self.leases.retain(|_, b| {
            lean_ctx_time::OffsetDateTime::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 Cloud `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.cloud.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 Cloud project of a local root.
pub(super) struct Connected {
    pub(super) store: CloudStore,
    pub(super) session: Session,
    pub(super) vault_id: String,
}

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

fn connected_with(
    config: &CloudConfig,
    store: CloudStore,
    project_root: &Path,
) -> Result<Connected, String> {
    if config.bus == CloudBusMode::Off || config.enabled == Some(false) {
        return Err("the LeanCTX Cloud bus is off".into());
    }
    let instance = store
        .instance()
        .ok()
        .flatten()
        .ok_or_else(|| "this machine is not connected to LeanCTX Cloud".to_string())?;
    let vault_id = crate::resolution::vault_for_root(store.data_dir(), project_root)
        .map_err(|e| e.to_string())?
        .map(|p| p.vault_id)
        .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(lean_ctx_time::UtcDateTime::now()),
        LeaseState::Valid | LeaseState::Grace
    ) {
        return Err("the LeanCTX Cloud lease has lapsed".into());
    }
    if !session.lease.lease.grants(CloudRight::BusRelay) {
        return Err("the LeanCTX Cloud lease does not include the bus".into());
    }
    Ok(Connected {
        store,
        session,
        vault_id,
    })
}

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

fn outcome(answer: Result<BusLeaseV1, CloudError>) -> 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("LeanCTX Cloud sent an incomplete lease".into()),
        Err(CloudError::Vetoed) => Outcome::Vetoed,
        Err(CloudError::NotFound) => Outcome::Superseded,
        Err(CloudError::Offline(why)) => {
            Outcome::Local(format!("LeanCTX Cloud unreachable ({why})"))
        }
        Err(error) => Outcome::Local(error.to_string()),
    }
}

/// Ask Cloud, off the tool call, for the lease the agent already holds
/// locally (`local_ref`). [`Outcome::Local`] at once when Cloud does not
/// apply (no request); otherwise [`Outcome::Pending`], and `settle` runs in
/// the queue with Cloud's answer and returns the line the agent gets with
/// its next `ctx_agent` call. A grant is bound to `local_ref` before
/// `settle` runs; `settle` releases the local lease on a refusal.
pub fn acquire(
    project_root: &Path,
    agent_id: &str,
    target: &str,
    duration_ms: u64,
    local_ref: &str,
    settle: impl FnOnce(Outcome) -> Option<String> + Send + 'static,
) -> Outcome {
    let connected = match connected(project_root) {
        Ok(connected) => connected,
        Err(why) => return Outcome::Local(why),
    };
    if normalize_target(project_root, target).is_none() {
        return Outcome::Local("the target is outside the project".into());
    }
    let (root, agent, target, local) = (
        project_root.to_path_buf(),
        agent_id.to_string(),
        target.to_string(),
        local_ref.to_string(),
    );
    pending().insert(local.clone());
    crate::background::submit(&caller(agent_id), move || {
        let outcome = acquire_with(&connected, &root, &agent, &target, duration_ms, &local);
        pending().remove(&local);
        settle(outcome)
    });
    Outcome::Pending
}

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 LeanCTX Cloud lease: {error}"));
        }
    }
    result
}

/// Renew the Cloud lease bound to `local_ref`, off the tool call and behind
/// a pending acquire (it needs Cloud's `lease_id`). A local-only lease gets
/// [`Outcome::Local`] at once; otherwise [`Outcome::Pending`], and `settle`
/// gets Cloud's answer as [`acquire`]'s does.
pub fn renew(
    project_root: &Path,
    agent_id: &str,
    local_ref: &str,
    duration_ms: u64,
    settle: impl FnOnce(Outcome) -> Option<String> + Send + 'static,
) -> Outcome {
    let Ok(store) = CloudStore::open() else {
        return Outcome::Local("no LeanCTX Cloud state".into());
    };
    let bound = Bindings::load(store.data_dir())
        .leases
        .contains_key(local_ref);
    if !bound && !pending().contains(local_ref) {
        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 local = local_ref.to_string();
    crate::background::submit(&caller(agent_id), move || {
        settle(renew_with(&connected, &local, duration_ms))
    });
    Outcome::Pending
}

fn renew_with(c: &Connected, local_ref: &str, duration_ms: u64) -> Outcome {
    let data = c.store.data_dir();
    let Some(binding) = Bindings::load(data).leases.get(local_ref).cloned() else {
        return Outcome::Local("LeanCTX Cloud did not grant this lease".into());
    };
    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(
        c.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(data, |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!("cloud lease bindings not saved: {error}");
    }
    result
}

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

/// Queue the release of the Cloud lease bound to `local_ref` (the relay
/// sends it). No network in the tool call; while the acquire is still
/// pending, the release is queued behind it and applies to its grant.
pub fn release(agent_id: &str, local_ref: &str) {
    let Ok(store) = CloudStore::open() else {
        return;
    };
    // Checked first: a pending acquire that ends meanwhile has bound its
    // grant before it leaves `PENDING`, so one of the two releases sees it.
    let was_pending = pending().contains(local_ref);
    queue_release_in(store.data_dir(), local_ref);
    if was_pending {
        let (data, local) = (store.data_dir().to_path_buf(), local_ref.to_string());
        crate::background::submit(&caller(agent_id), move || {
            queue_release_in(&data, &local);
            None
        });
    }
}

fn queue_release_in(data_dir: &Path, local_ref: &str) {
    let queued = Bindings::mutate(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!("cloud lease release not queued: {error}");
    }
}

/// The relay's half: send queued releases (blocking; run off the socket loop).
/// A release Cloud no longer knows counts as done.
pub fn send_releases(store: &CloudStore, 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(CloudError::NotFound) => true,
                Err(error) => matches!(
                    error,
                    CloudError::Forbidden | CloudError::Vetoed | CloudError::Protocol(_)
                ),
            }
        })
        .collect();
    if done.is_empty() {
        return;
    }
    if let Err(error) = Bindings::mutate(data, |b| b.releases.retain(|r| !done.contains(r))) {
        tracing::debug!("cloud lease releases not saved: {error}");
    }
}

/// One line for `ctx_agent`: where the lease holds. The Cloud fencing token
/// stays in `leases.json` (0600) and is never printed, like a task claim's.
pub fn scope_line(outcome: &Outcome) -> String {
    match outcome {
        Outcome::Granted { .. } | Outcome::Held { .. } | Outcome::Vetoed | Outcome::Superseded => {
            "scope=cloud".into()
        }
        Outcome::Local(why) => {
            format!("scope=local ({why}; other machines are not excluded)")
        }
        Outcome::Pending => {
            "scope=pending (LeanCTX Cloud is asked off this call; until it grants, \
             other machines are not excluded; its answer comes with your next ctx_agent call)"
                .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::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.cloud.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!(matches!(
            out,
            Outcome::Granted {
                fencing_token: 4,
                ..
            }
        ));
        assert_eq!(
            scope_line(&out),
            "scope=cloud",
            "the token is never printed"
        );
        let held = serde_json::json!({"schema": "leanctx.cloud.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(CloudError::Vetoed)), Outcome::Vetoed);
        assert_eq!(outcome(Err(CloudError::NotFound)), Outcome::Superseded);
        let offline = outcome(Err(CloudError::Offline("timeout".into())));
        assert!(
            scope_line(&offline).starts_with("scope=local (LeanCTX Cloud unreachable (timeout)")
        );
        assert!(scope_line(&offline).ends_with("other machines are not excluded)"));
        let pending = scope_line(&Outcome::Pending);
        assert!(pending.starts_with("scope=pending (") && !pending.contains("fencing"));
    }

    #[test]
    fn without_an_enrollment_no_request_is_made() {
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("c"), dir.path().join("d"));
        let err = connected_with(&CloudConfig::default(), store.clone(), dir.path())
            .err()
            .unwrap();
        assert_eq!(err, "this machine is not connected to LeanCTX Cloud");
        let off = CloudConfig {
            bus: CloudBusMode::Off,
            ..CloudConfig::default()
        };
        assert_eq!(
            connected_with(&off, store, dir.path()).err().unwrap(),
            "the LeanCTX Cloud 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);
    }
}