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
// SPDX-License-Identifier: Apache-2.0
//! Local vault resolutions that no longer fit their checkout (dogfood
//! 5.11.: one repository split across two vaults without a warning).
//!
//! `projects.json` ([`VaultMap`]) remembers which vault Cloud resolved for a
//! checkout. That memory goes stale when
//! - the link file now names another vault (`cloud link` after `cloud
//!   connect`, or a pulled commit that changed `.lean-ctx-cloud.json`), or
//! - the checkout's identity changed: a `.lean-ctx-id` that an earlier
//!   `cloud link` wrote (32 random hex characters) no longer outranks the git
//!   origin ([`crate::vault::project_identity`]), so every checkout of one
//!   origin is one repository again.
//!
//! A stale record is never used: it is dropped, and the next resolve maps
//! the checkout again (with the person's first-share consent where the gate
//! holds). When Cloud still answers with another vault than the link file
//! names, the resolve is refused ([`refuse_unlinked`]): nothing syncs into
//! a vault the link file does not name.

use std::path::Path;

use lean_ctx::core::pathutil::same_project;
use lean_ctx_protocol::cloud::status::CloudStatusVaultMismatchV1;
use lean_ctx_protocol::cloud::vault::{VaultRefV1, stable_identity};

use super::CloudError;
use super::link::read_link;
use super::vault::{VaultMap, VaultRecord, project_identity};

/// Why a recorded resolution does not fit the checkout any more.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StaleReason {
    /// The link file names `linked` (`ns/name`).
    Link { linked: String },
    /// The checkout's identity is not the one the record was resolved for.
    Identity,
}

/// A resolution of `root` to `resolved` (`ns/name`) that does not hold.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Stale {
    pub root: String,
    pub resolved: String,
    pub reason: StaleReason,
}

impl Stale {
    /// The linked vault, when the link file is the reason.
    pub fn linked(&self) -> Option<&str> {
        match &self.reason {
            StaleReason::Link { linked } => Some(linked),
            StaleReason::Identity => None,
        }
    }

    /// `vault_mismatch` of `cloud status --json`.
    pub fn wire(&self) -> CloudStatusVaultMismatchV1 {
        CloudStatusVaultMismatchV1 {
            project_root: self.root.clone(),
            resolved: self.resolved.clone(),
            linked: self.linked().map(str::to_string),
            reason: match self.reason {
                StaleReason::Link { .. } => "link",
                StaleReason::Identity => "identity",
            }
            .into(),
        }
    }

    /// One line for people (status, connect, link, sync).
    pub fn describe(&self) -> String {
        match &self.reason {
            StaleReason::Link { linked } => format!(
                "this machine resolved {} to the vault {}, but its link file names {linked}",
                self.root, self.resolved
            ),
            StaleReason::Identity => format!(
                "this machine resolved {} to the vault {} under an identity the checkout no longer has (a `.lean-ctx-id` written by an earlier `cloud link`; the git origin counts now)",
                self.root, self.resolved
            ),
        }
    }
}

fn address(record: &VaultRecord) -> String {
    format!("{}/{}", record.ns, record.name)
}

/// The record `root` uses (its own, or its main checkout's), its key and
/// the recorded root it was found by.
fn recorded<'a>(map: &'a VaultMap, root: &Path) -> Option<(&'a str, &'a VaultRecord, String)> {
    let find = |root: &str| {
        map.vaults.iter().find_map(|(key, record)| {
            record
                .roots
                .iter()
                .find(|known| same_project(known, root))
                .map(|known| (key.as_str(), record, known.clone()))
        })
    };
    let root_str = root.to_string_lossy();
    find(&root_str).or_else(|| {
        let main = super::link::main_worktree_root(root)?;
        find(&main.to_string_lossy())
    })
}

/// Whether the recorded resolution of `root` is stale. Local files only;
/// changes nothing.
pub fn stale_for_root(map: &VaultMap, root: &Path) -> Option<Stale> {
    let (key, record, _) = recorded(map, root)?;
    let resolved = address(record);
    let stale = |reason| Stale {
        root: root.to_string_lossy().into_owned(),
        resolved: resolved.clone(),
        reason,
    };
    // A damaged link file is refused where the link is used; here it says
    // nothing about the record.
    if let Ok(Some(link)) = read_link(root)
        && (link.vault.ns != record.ns || link.vault.name != record.name)
    {
        return Some(stale(StaleReason::Link {
            linked: format!("{}/{}", link.vault.ns, link.vault.name),
        }));
    }
    let current = project_identity(root).map(|identity| stable_identity(&identity));
    if current.is_some_and(|current| current != key) {
        return Some(stale(StaleReason::Identity));
    }
    None
}

/// Drop the stale resolution of `root` from `map`, if any. A link file that
/// names another vault drops the whole record: one identity has one vault,
/// so every checkout of it resolves again. An identity change drops only
/// this checkout from the record of its old identity.
pub fn forget_stale(map: &mut VaultMap, root: &Path) -> Option<Stale> {
    let stale = stale_for_root(map, root)?;
    let (key, _, known) = recorded(map, root)?;
    let key = key.to_string();
    match stale.reason {
        StaleReason::Link { .. } => {
            map.vaults.remove(&key);
        }
        StaleReason::Identity => {
            if let Some(record) = map.vaults.get_mut(&key) {
                record.roots.retain(|r| r != &known);
                if record.roots.is_empty() {
                    map.vaults.remove(&key);
                }
            }
        }
    }
    Some(stale)
}

/// [`forget_stale`] for every checkout `map` knows.
pub fn forget_all_stale(map: &mut VaultMap) -> Vec<Stale> {
    let roots: Vec<String> = map
        .vaults
        .values()
        .flat_map(|record| record.roots.iter().cloned())
        .collect();
    roots
        .iter()
        .filter_map(|root| forget_stale(map, Path::new(root)))
        .collect()
}

/// The vault map at `data_dir` without stale resolutions; the file is
/// rewritten when one was dropped. What every pass that sends (sync, task
/// proposals) reads, so none sends into a vault the checkout left.
pub fn load_current(data_dir: &Path) -> Result<(VaultMap, Vec<Stale>), CloudError> {
    let mut map = VaultMap::load(data_dir);
    let dropped = forget_all_stale(&mut map);
    if !dropped.is_empty() {
        map.save(data_dir)
            .map_err(|e| CloudError::Local(format!("projects.json: {e}")))?;
        for stale in &dropped {
            tracing::info!(
                "cloud: dropped a stale vault resolution: {}",
                stale.describe()
            );
        }
    }
    Ok((map, dropped))
}

/// Drop the stale resolution of `root` in the map at `data_dir`.
pub fn forget_stale_at(data_dir: &Path, root: &Path) -> Result<Option<Stale>, CloudError> {
    let mut map = VaultMap::load(data_dir);
    let Some(stale) = forget_stale(&mut map, root) else {
        return Ok(None);
    };
    map.save(data_dir)
        .map_err(|e| CloudError::Local(format!("projects.json: {e}")))?;
    Ok(Some(stale))
}

/// The vault the checkout at `root` sends to, from the map at `data_dir`:
/// `None` when it has none, refused (`vault_mismatch`) when its record is
/// stale. The one lookup of every caller that writes to a vault (tasks, bus
/// posts and leases, docs, the live bus), so a record the link file or the
/// identity no longer backs is never written to, also before link, connect
/// or sync dropped it (a `git pull` that changed the link file).
pub fn vault_for_root(data_dir: &Path, root: &Path) -> Result<Option<VaultRecord>, CloudError> {
    // A folder the person stopped sharing (`cloud unlink`) has no vault,
    // also not through its main checkout or another checkout (rc4-6).
    if crate::unlinked::contains(data_dir, root) {
        return Ok(None);
    }
    vault_in(&VaultMap::load(data_dir), root).map(Option::<&VaultRecord>::cloned)
}

/// The map at `data_dir` without its stale records, the file unchanged:
/// what the live bus subscribes and announces from, so it never joins a
/// vault a checkout left.
pub fn current_view(data_dir: &Path) -> VaultMap {
    let mut map = VaultMap::load(data_dir);
    forget_all_stale(&mut map);
    map
}

/// [`vault_for_root`] in a loaded `map`.
pub fn vault_in<'a>(map: &'a VaultMap, root: &Path) -> Result<Option<&'a VaultRecord>, CloudError> {
    if let Some(stale) = stale_for_root(map, root) {
        return Err(refuse_stale(&stale));
    }
    Ok(map.for_root(&root.to_string_lossy()))
}

/// Refused (`vault_mismatch`) when `record`, which a caller is about to
/// sync for the checkout at `root`, is not what the checkout resolves to
/// now: its link file names another vault, or the map's record of `root`
/// at `data_dir` is stale. The guard of every vault sync (background pass
/// and `cloud sync`), whatever map snapshot the caller planned from.
pub fn refuse_stale_record(
    data_dir: &Path,
    record: &VaultRecord,
    root: &Path,
) -> Result<(), CloudError> {
    if let Ok(Some(link)) = read_link(root)
        && (link.vault.ns != record.ns || link.vault.name != record.name)
    {
        return Err(refuse_stale(&Stale {
            root: root.to_string_lossy().into_owned(),
            resolved: address(record),
            reason: StaleReason::Link {
                linked: format!("{}/{}", link.vault.ns, link.vault.name),
            },
        }));
    }
    match stale_for_root(&VaultMap::load(data_dir), root) {
        Some(stale) => Err(refuse_stale(&stale)),
        None => Ok(()),
    }
}

/// The refusal for a write into the stale resolution `stale`.
pub fn refuse_stale(stale: &Stale) -> CloudError {
    CloudError::Refused(format!(
        "vault_mismatch: {}. Nothing is sent to {} any more: run `lean-ctx cloud connect` here to resolve the project again",
        stale.describe(),
        stale.resolved
    ))
}

/// Cloud answered `answered` for the checkout at `root`: refused when its
/// link file names another vault. Cloud keeps the first binding of an
/// identity (another Instance of the workspace may have made it), so a
/// link to another vault cannot move it; syncing anyway would split the
/// repository silently.
pub fn refuse_unlinked(root: &Path, answered: &VaultRefV1) -> Result<(), CloudError> {
    let Some(link) = read_link(root)? else {
        return Ok(());
    };
    if link.vault == *answered {
        return Ok(());
    }
    Err(CloudError::Refused(format!(
        "vault_mismatch: LeanCTX Cloud keeps this repository in the vault {}/{} (bound there first, possibly by another Instance of the workspace), but {} names {}/{}. Nothing syncs for it until both agree: run `lean-ctx cloud link {}/{}` here, or move the repository to {}/{} in LeanCTX Cloud and run `lean-ctx cloud connect` again",
        answered.ns,
        answered.name,
        super::link::LINK_FILE,
        link.vault.ns,
        link.vault.name,
        answered.ns,
        answered.name,
        link.vault.ns,
        link.vault.name,
    )))
}

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

    fn checkout(base: &Path, name: &str, origin: Option<&str>) -> std::path::PathBuf {
        let root = base.join(name);
        std::fs::create_dir_all(root.join(".git")).unwrap();
        let config = origin.map_or(String::new(), |url| {
            format!("[remote \"origin\"]\n\turl = {url}\n")
        });
        std::fs::write(root.join(".git/config"), config).unwrap();
        root
    }

    fn record(map: &mut VaultMap, key: &str, vault: &str, roots: &[&Path]) {
        let (ns, name) = vault.split_once('/').unwrap();
        map.vaults.insert(
            key.into(),
            VaultRecord {
                vault_id: format!("id-{name}"),
                ns: ns.into(),
                name: name.into(),
                sealed: false,
                roots: roots
                    .iter()
                    .map(|r| r.to_string_lossy().into_owned())
                    .collect(),
            },
        );
    }

    fn stable(root: &Path) -> String {
        stable_identity(&project_identity(root).unwrap())
    }

    /// The guard of `place::sync_vault`: a record planned from an older map
    /// is refused once the link file names another vault, also when the map
    /// on disk no longer has it.
    #[test]
    fn a_sync_of_a_record_the_link_file_left_is_refused() {
        let base = tempfile::tempdir().unwrap();
        let data = base.path().join("data");
        std::fs::create_dir_all(&data).unwrap();
        let a = checkout(
            base.path(),
            "a",
            Some("git@github.com:yves/leanctx-cloud.git"),
        );
        let mut map = VaultMap::default();
        record(&mut map, &stable(&a), "yves/cloud", &[&a]);
        map.save(&data).unwrap();
        let planned = map.vaults.values().next().unwrap().clone();
        crate::link::link(&a, "yves/cloud").unwrap();
        assert!(refuse_stale_record(&data, &planned, &a).is_ok());

        crate::link::link(&a, "yves/leanctx-cloud").unwrap();
        let refused = refuse_stale_record(&data, &planned, &a).unwrap_err();
        assert!(refused.to_string().contains("vault_mismatch"), "{refused}");
        VaultMap::default().save(&data).unwrap();
        assert!(refuse_stale_record(&data, &planned, &a).is_err());
    }

    #[test]
    fn a_link_that_names_another_vault_drops_the_whole_record() {
        let base = tempfile::tempdir().unwrap();
        let a = checkout(
            base.path(),
            "a",
            Some("git@github.com:yves/leanctx-cloud.git"),
        );
        let b = checkout(
            base.path(),
            "b",
            Some("git@github.com:yves/leanctx-cloud.git"),
        );
        let mut map = VaultMap::default();
        record(&mut map, &stable(&a), "yves/cloud", &[&a, &b]);
        assert_eq!(
            stale_for_root(&map, &a),
            None,
            "no link file: nothing to compare"
        );

        super::super::link::link(&a, "yves/leanctx-cloud").unwrap();
        let stale = stale_for_root(&map, &a).expect("stale");
        assert_eq!(stale.resolved, "yves/cloud");
        assert_eq!(stale.linked(), Some("yves/leanctx-cloud"));
        assert!(
            stale
                .describe()
                .contains("link file names yves/leanctx-cloud")
        );
        assert_eq!(forget_stale(&mut map, &a), Some(stale));
        assert!(map.vaults.is_empty(), "b is the same repository");
        assert!(map.for_root(&b.to_string_lossy()).is_none());
    }

    #[test]
    fn an_identity_change_drops_only_that_checkout() {
        let base = tempfile::tempdir().unwrap();
        let a = checkout(base.path(), "a", Some("https://github.com/acme/pay.git"));
        let mut map = VaultMap::default();
        record(&mut map, "old-random-identity", "acme/pay", &[&a]);
        let other = base.path().join("elsewhere");
        record(&mut map, &stable(&a), "acme/pay", &[&other]);
        let stale = forget_stale(&mut map, &a).expect("stale");
        assert_eq!(stale.reason, StaleReason::Identity);
        assert!(!map.vaults.contains_key("old-random-identity"));
        assert!(map.vaults.contains_key(&stable(&a)));
        assert_eq!(forget_stale(&mut map, &a), None);
    }

    #[test]
    fn load_current_rewrites_the_map_once() {
        let base = tempfile::tempdir().unwrap();
        let data = base.path().join("data");
        std::fs::create_dir_all(&data).unwrap();
        let a = checkout(base.path(), "a", Some("https://github.com/acme/pay.git"));
        let mut map = VaultMap::default();
        record(&mut map, &stable(&a), "acme/old", &[&a]);
        map.save(&data).unwrap();
        super::super::link::link(&a, "acme/pay").unwrap();
        let (map, dropped) = load_current(&data).unwrap();
        assert_eq!(dropped.len(), 1);
        assert!(map.vaults.is_empty());
        assert!(VaultMap::load(&data).vaults.is_empty(), "saved");
        assert!(load_current(&data).unwrap().1.is_empty());
    }

    #[test]
    fn cloud_answering_another_vault_than_the_link_is_refused() {
        let base = tempfile::tempdir().unwrap();
        let a = checkout(base.path(), "a", Some("https://github.com/acme/pay.git"));
        let answered = VaultRefV1 {
            ns: "acme".into(),
            name: "old".into(),
        };
        assert!(
            refuse_unlinked(&a, &answered).is_ok(),
            "no link: LeanCTX Cloud decides"
        );
        super::super::link::link(&a, "acme/pay").unwrap();
        let refused = refuse_unlinked(&a, &answered).unwrap_err().to_string();
        assert!(refused.contains("vault_mismatch"), "{refused}");
        assert!(
            refused.contains("acme/old") && refused.contains("acme/pay"),
            "{refused}"
        );
        let linked = VaultRefV1 {
            ns: "acme".into(),
            name: "pay".into(),
        };
        assert!(refuse_unlinked(&a, &linked).is_ok());
    }
}