lean-ctx-cloud 3.12.9

The LeanCTX Cloud connector: enrollment, bus, knowledge sync, tasks and policy on top of LeanCTX | OSS.
Documentation
// SPDX-License-Identifier: Apache-2.0
//! The steps of `lean-ctx cloud connect` after the device flow (CHECKLIST
//! 2.1.1): the person confirms once per workspace what this machine shares
//! with it, then the current project is mapped to its vault.
//!
//! The answer is kept in `<config_dir>/cloud/sharing.json` (0600), so later
//! sessions and later connects to the same workspace ask nothing. A "no"
//! switches every outbound channel off in `[cloud]`. Pulling the workspace's
//! knowledge and policy goes on.
//! Nothing here talks to Cloud.

use std::path::Path;

use lean_ctx_time::UtcDateTime;
use serde::{Deserialize, Serialize};

use super::config::{CloudBusMode, CloudConfig};
use super::state::SHARING_FILE;
use super::vault::{VaultMap, project_identity};
use super::{CloudError, CloudStore};

pub const SHARING_SCHEMA: &str = "leanctx.cloud.sharing/v1";

/// What the person answered for one workspace.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SharingDecision {
    pub schema: String,
    /// The workspace (organization) the answer is for.
    pub org_id: String,
    pub shared: bool,
    pub decided_at: String,
    /// Where it was answered: `browser` for the device approval page
    /// (C0 2.1); absent for the terminal.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
}

/// [`SharingDecision::source`] of an answer given on the approval page.
pub const SOURCE_BROWSER: &str = "browser";

/// The decision recorded for `org_id`. An answer for another workspace,
/// another schema or a damaged file counts as none, so it is asked again.
pub fn decision(store: &CloudStore, org_id: &str) -> Option<SharingDecision> {
    store
        .read_config::<SharingDecision>(SHARING_FILE)
        .ok()
        .flatten()
        .filter(|d| d.schema == SHARING_SCHEMA && d.org_id == org_id)
}

/// Record the person's answer for `org_id`, given in the terminal.
pub fn record(
    store: &CloudStore,
    org_id: &str,
    shared: bool,
    now: UtcDateTime,
) -> Result<SharingDecision, CloudError> {
    record_from(store, org_id, shared, now, None)
}

/// Record the person's answer for `org_id`; `source` names where it was
/// given ([`SOURCE_BROWSER`]), `None` for the terminal.
pub fn record_from(
    store: &CloudStore,
    org_id: &str,
    shared: bool,
    now: UtcDateTime,
    source: Option<&str>,
) -> Result<SharingDecision, CloudError> {
    let decision = SharingDecision {
        schema: SHARING_SCHEMA.into(),
        org_id: org_id.into(),
        shared,
        decided_at: now.to_rfc3339_opts(lean_ctx_time::SecondsFormat::Secs, true),
        source: source.map(str::to_string),
    };
    store.write_config(SHARING_FILE, &decision)?;
    Ok(decision)
}

/// What leaves this machine for the workspace under `config`, one line
/// each: exactly what the engine sends, nothing it does not. Empty when
/// nothing does. Engine metrics are asked separately once released
/// (`engine_metrics_notice`); usage totals and context receipts are not
/// sent by this version.
pub fn shared_items(config: &CloudConfig) -> Vec<&'static str> {
    if config.enabled == Some(false) {
        return Vec::new();
    }
    let mut items = Vec::new();
    if config.auto_capture {
        items.push(
            "knowledge from your sessions that passes the capture gate: single claims with a file, symbol, test, commit or link reference (never chats, tool output or whole files)",
        );
    }
    if config.bus == CloudBusMode::Auto {
        items.push(
            "your agents' presence (role, state and this machine's short name, never its domain) and the bus messages they send to the workspace",
        );
        if config.presence_names {
            items.push(
                "for your team only: what each agent is working on, from the status it sets itself, with paths and secrets removed (turn off with cloud.presence_names = false)",
            );
        }
        items.push("hourly collaboration friction counts, such as merge conflicts (numbers only)");
    }
    items.push(
        "task traffic your agents start: tasks they propose, and claims, one-line updates and results of tasks they work on under your worker opt-in",
    );
    items
}

/// [`shared_items`] as the C0 2.1 share codes the approval page words
/// (`knowledge`, `bus`), sorted. `usage` and `receipts` are not sent by
/// this version and are not announced.
pub fn share_codes(config: &CloudConfig) -> Vec<String> {
    if config.enabled == Some(false) {
        return Vec::new();
    }
    let mut codes = Vec::new();
    if config.bus == CloudBusMode::Auto {
        codes.push("bus".to_string());
    }
    if config.auto_capture {
        codes.push("knowledge".to_string());
    }
    codes
}

/// `config` with every outbound channel off: what a "no" means.
pub fn without_sharing(config: &mut CloudConfig) {
    config.auto_capture = false;
    config.metrics_upload = false;
    config.receipts_upload = false;
    config.bus = CloudBusMode::Off;
}

/// Every channel a "no" switches off is off: what [`without_sharing`]
/// leaves. A yes given while it holds shares nothing until they are on again.
pub fn switched_off_by_no(config: &CloudConfig) -> bool {
    !config.auto_capture
        && !config.metrics_upload
        && !config.receipts_upload
        && config.bus == CloudBusMode::Off
}

/// `config` with the channels a "no" switched off back at their defaults:
/// what a later yes means.
pub fn with_sharing(config: &mut CloudConfig) {
    let defaults = CloudConfig::default();
    config.auto_capture = defaults.auto_capture;
    config.metrics_upload = defaults.metrics_upload;
    config.receipts_upload = defaults.receipts_upload;
    config.bus = defaults.bus;
}

/// What connect does for the project at `root`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProjectStep {
    /// No stable identity (no git origin, id file or manifest): nothing to map.
    None,
    /// This machine already knows the vault (`ns/name`).
    Known(String),
    /// The person unlinked this folder (`cloud unlink`), or the list of
    /// unlinked folders is damaged: connect shares nothing and never says
    /// it syncs, also not through the main checkout's record (bd 40060).
    Unlinked,
    /// Ask Cloud for the vault: it maps a known project, or (1.x and v4)
    /// creates one on first sight.
    Resolve,
}

/// The project step for `root`, from local state only.
pub fn project_step(data_dir: &Path, map: &VaultMap, root: &Path) -> ProjectStep {
    if project_identity(root).is_none() {
        return ProjectStep::None;
    }
    if crate::unlinked::contains(data_dir, root) {
        return ProjectStep::Unlinked;
    }
    match map.for_root(&root.to_string_lossy()) {
        Some(record) => ProjectStep::Known(format!("{}/{}", record.ns, record.name)),
        None => ProjectStep::Resolve,
    }
}

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

    fn store() -> (tempfile::TempDir, CloudStore) {
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("config"), dir.path().join("data"));
        (dir, store)
    }

    #[test]
    fn a_decision_holds_for_its_workspace_only() {
        let (_dir, store) = store();
        assert!(decision(&store, "org_a").is_none(), "nothing asked yet");
        let now = UtcDateTime::now();
        record(&store, "org_a", false, now).unwrap();
        assert!(decision(&store, "org_a").is_some_and(|d| !d.shared));
        assert!(decision(&store, "org_b").is_none());
        record(&store, "org_a", true, now).unwrap();
        let kept = decision(&store, "org_a").unwrap();
        assert!(kept.shared);
        assert!(
            decision(&store, "org_b").is_none(),
            "another workspace asks again"
        );
        assert_eq!(kept.source, None, "answered in the terminal");
        record(&store, "org_a", false, now).unwrap();
        assert!(!decision(&store, "org_a").unwrap().shared);
        // C0 2.1: the device page's answer keeps where it came from.
        record_from(&store, "org_a", true, now, Some(SOURCE_BROWSER)).unwrap();
        let browser = decision(&store, "org_a").unwrap();
        assert!(browser.shared);
        assert_eq!(browser.source.as_deref(), Some("browser"));
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt as _;
            let mode = std::fs::metadata(store.config_dir().join(SHARING_FILE))
                .unwrap()
                .permissions()
                .mode();
            assert_eq!(mode & 0o777, 0o600);
        }
    }

    #[test]
    fn a_damaged_or_foreign_record_counts_as_no_answer() {
        let (_dir, store) = store();
        store
            .write_config_bytes(SHARING_FILE, b"{not json")
            .unwrap();
        assert!(decision(&store, "org_a").is_none());
        let other = serde_json::json!({
            "schema": "leanctx.cloud.sharing/v0",
            "org_id": "org_a",
            "shared": true,
            "decided_at": "2026-10-25T00:00:00Z",
        });
        store.write_config(SHARING_FILE, &other).unwrap();
        assert!(decision(&store, "org_a").is_none());
    }

    #[test]
    fn disconnect_forgets_the_decision() {
        let (_dir, store) = store();
        record(&store, "org_a", true, UtcDateTime::now()).unwrap();
        store.forget(true).unwrap();
        assert!(decision(&store, "org_a").is_none());
    }

    #[test]
    fn the_summary_names_each_channel_that_is_on_and_a_no_turns_all_off() {
        let mut config = CloudConfig::default();
        // Exactly what is sent: knowledge, bus with friction
        // counts, task traffic; never usage totals or receipts, which this
        // version does not send.
        let items = shared_items(&config).join("\n");
        for sent in [
            "knowledge",
            "bus messages",
            "friction counts",
            "task traffic",
        ] {
            assert!(items.contains(sent), "{sent}: {items}");
        }
        for not_sent in ["usage totals", "receipts"] {
            assert!(!items.contains(not_sent), "{not_sent}: {items}");
        }
        assert!(
            items.contains("machine's short name, never its domain"),
            "{items}"
        );
        assert!(items.contains("paths and secrets removed"), "{items}");
        let unnamed = CloudConfig {
            presence_names: false,
            ..CloudConfig::default()
        };
        let unnamed = shared_items(&unnamed).join("\n");
        assert!(!unnamed.contains("working on"), "the opt-out is said");
        // The Instance's short name rides with presence either way.
        assert!(unnamed.contains("machine's short name"), "{unnamed}");
        assert_eq!(share_codes(&config), ["bus", "knowledge"]);
        without_sharing(&mut config);
        assert!(share_codes(&config).is_empty());
        let left = shared_items(&config).join("\n");
        assert!(!left.contains("knowledge from") && !left.contains("bus messages"));
        assert!(!config.auto_capture && !config.metrics_upload && !config.receipts_upload);
        assert_eq!(config.bus, CloudBusMode::Off);
        assert!(switched_off_by_no(&config));
        // A later yes (E2E 27.10. E2) turns the same channels back on.
        with_sharing(&mut config);
        assert_eq!(share_codes(&config), share_codes(&CloudConfig::default()));
        assert!(!switched_off_by_no(&config));
        // One channel switched off by hand is not a "no".
        let one_off = CloudConfig {
            bus: CloudBusMode::Off,
            ..CloudConfig::default()
        };
        assert!(!switched_off_by_no(&one_off));
        let paused = CloudConfig {
            enabled: Some(false),
            ..CloudConfig::default()
        };
        assert!(
            shared_items(&paused).is_empty(),
            "a paused LeanCTX Cloud connection sends nothing"
        );
    }

    #[test]
    fn the_project_step_maps_known_checkouts_and_resolves_new_ones() {
        let base = tempfile::tempdir().unwrap();
        let bare = base.path().join("bare");
        std::fs::create_dir_all(&bare).unwrap();
        let data = base.path().join("data");
        let map = VaultMap::default();
        assert_eq!(project_step(&data, &map, &bare), ProjectStep::None);

        let repo = base.path().join("repo");
        std::fs::create_dir_all(repo.join(".git")).unwrap();
        std::fs::write(
            repo.join(".git/config"),
            "[remote \"origin\"]\n\turl = git@github.com:acme/payments.git\n",
        )
        .unwrap();
        assert_eq!(project_step(&data, &map, &repo), ProjectStep::Resolve);

        let mut map = VaultMap::default();
        map.vaults.insert(
            "s1".into(),
            VaultRecord {
                vault_id: "v1".into(),
                ns: "acme".into(),
                name: "payments".into(),
                sealed: false,
                roots: vec![repo.to_string_lossy().into_owned()],
            },
        );
        assert_eq!(
            project_step(&data, &map, &repo),
            ProjectStep::Known("acme/payments".into())
        );
    }

    /// bd 40060: connect never says an unlinked worktree syncs through its
    /// main checkout's record.
    #[test]
    fn unlink_of_a_worktree_is_its_own_project_step() {
        let base = tempfile::tempdir().unwrap();
        let base = base.path().canonicalize().unwrap();
        let (main, wt) = crate::unlinked::fake_worktree(&base);
        std::fs::write(wt.join(".lean-ctx-id"), "payments-repo").unwrap();
        let data = base.join("data");
        let mut map = VaultMap::default();
        map.vaults.insert(
            "s1".into(),
            VaultRecord {
                vault_id: "v1".into(),
                ns: "acme".into(),
                name: "payments".into(),
                sealed: false,
                roots: vec![main.to_string_lossy().into_owned()],
            },
        );
        assert_eq!(
            project_step(&data, &map, &wt),
            ProjectStep::Known("acme/payments".into())
        );
        crate::unlinked::add(&data, &wt).unwrap();
        assert_eq!(project_step(&data, &map, &wt), ProjectStep::Unlinked);
    }
}