lean-ctx-cloud 3.12.8

The LeanCTX Cloud connector: enrollment, bus, knowledge sync, tasks and policy on top of LeanCTX | OSS.
Documentation
// SPDX-License-Identifier: Apache-2.0
//! What writes signed events into a workspace vault: the instance key, a
//! registered workspace worker and the place as C0 2.0 names it. Every
//! engine path that queues vault events (sync capture, `cloud maintain`,
//! `knowledge contradict|retract`) builds its signer and place here, so the
//! C0 spellings (`hlc.node` and place id as hyphenated UUIDs) cannot drift
//! between paths again (dogfood 25.10.: the maintain CLI signed with the raw
//! `inst_<hex>` id and Cloud refused `invalid_event shape /hlc/node`).

use crate::CloudError;
use crate::auth::Session;
use crate::events::build::{Place, Signer};
use crate::instance_key::InstanceKeys;
use crate::replica::Replica;
use crate::vault::VaultRecord;
use crate::workers::card::{Availability, Budget, WorkerCard};
use crate::workers::{attach, canonical_instance_id, register};

use super::place::{CAPTURE_HOST, place_id};

/// The instance key and a registered workspace worker of one role.
pub struct VaultWriter {
    pub keys: InstanceKeys,
    /// The canonical instance id (hyphenated UUID).
    pub instance_id: String,
    pub worker_id: String,
}

impl VaultWriter {
    /// Attach the `role` worker of this instance and make sure Cloud knows
    /// it (E10), as the capture worker of a sync.
    pub fn open(session: &Session, role: &str) -> Result<Self, CloudError> {
        let keys = InstanceKeys::load(&session.store)?;
        let instance_id = canonical_instance_id(&session.instance.instance_id);
        let worker = attach::attach(&session.store, &instance_id, CAPTURE_HOST, role)?.worker;
        let worker = register::ensure_registered(
            session,
            &worker,
            &WorkerCard::new(
                CAPTURE_HOST,
                None,
                Vec::new(),
                Budget::default(),
                Availability::Online,
            ),
        )?;
        Ok(Self {
            keys,
            instance_id,
            worker_id: worker.worker_id,
        })
    }

    pub fn signer(&self) -> Signer<'_> {
        Signer::workspace(self.keys.signing_key(), &self.instance_id, &self.worker_id)
    }
}

/// `vault:<place id>`, the place reference of the E5/E6 routes.
pub fn vault_ref(record: &VaultRecord) -> String {
    format!("vault:{}", place_id(&record.vault_id))
}

/// The vault as an event place, keyed with the replica's id salt at its
/// folded cursor.
pub fn vault_place(record: &VaultRecord, replica: &Replica) -> Result<Place, CloudError> {
    let pk_id = replica.id_salt()?.ok_or_else(|| {
        CloudError::Local("replica has no id_salt: run `lean-ctx cloud sync`".into())
    })?;
    Ok(Place {
        kind: "vault".into(),
        id: place_id(&record.vault_id),
        pk_id,
        causal_base: replica.cursor()?.unwrap_or(0),
    })
}

#[cfg(test)]
mod tests {
    use ed25519_dalek::SigningKey;

    use crate::events::build::Signer;

    /// The 1.x enrolment's `inst_<hex>` id signs as its UUID: the C0 2.0
    /// event schema only takes a UUID or 16 hex digits as `hlc.node`.
    #[test]
    fn a_workspace_signer_names_the_instance_as_its_uuid() {
        let key = SigningKey::from_bytes(&[0x49; 32]);
        for spelling in [
            "inst_6f2b9c4e1a3d4e5f8a7b0c1d2e3f4a5b",
            "6f2b9c4e1a3d4e5f8a7b0c1d2e3f4a5b",
            "6F2B9C4E-1A3D-4E5F-8A7B-0C1D2E3F4A5B",
        ] {
            let signer = Signer::workspace(&key, spelling, "wkr_vk4oelpv5lzjmev44hun");
            assert_eq!(
                signer.hlc_node, "6f2b9c4e-1a3d-4e5f-8a7b-0c1d2e3f4a5b",
                "{spelling}"
            );
        }
    }
}