use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::CloudError;
use crate::receipts::ReceiptKey;
use crate::state::CloudStore;
use crate::transport::CloudClient;
pub const FEATURE_SESSION_REF: &str = "session_ref";
pub const EVENT_REF_PREFIX: &str = "hs_";
pub(crate) const CAPABILITIES_FILE: &str = "capabilities.json";
const MAX_FEATURES: usize = 64;
const MAX_FEATURE_LEN: usize = 64;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct Capabilities {
server: String,
features: Vec<String>,
}
fn features_of(answer: &serde_json::Value) -> Vec<String> {
if answer["schema"] != "leanctx.cloud.capabilities/v1" {
return Vec::new();
}
answer["features"]
.as_array()
.into_iter()
.flatten()
.filter_map(serde_json::Value::as_str)
.filter(|f| !f.is_empty() && f.len() <= MAX_FEATURE_LEN)
.take(MAX_FEATURES)
.map(str::to_string)
.collect()
}
pub fn record(store: &CloudStore, server: &str, features: Vec<String>) -> Result<(), CloudError> {
store.write_config(
CAPABILITIES_FILE,
&Capabilities {
server: server.to_string(),
features,
},
)
}
pub fn refresh(store: &CloudStore, server: &str, client: &CloudClient) -> Result<(), CloudError> {
record(store, server, announced_by(client.get("/capabilities"))?)
}
fn announced_by(answer: Result<serde_json::Value, CloudError>) -> Result<Vec<String>, CloudError> {
match answer {
Ok(answer) => Ok(features_of(&answer)),
Err(CloudError::NotFound | CloudError::Server { status: 404, .. }) => Ok(Vec::new()),
Err(error) => Err(error),
}
}
pub fn announced(store: &CloudStore, feature: &str) -> bool {
let Ok(Some(instance)) = store.instance() else {
return false;
};
store
.read_config::<Capabilities>(CAPABILITIES_FILE)
.ok()
.flatten()
.is_some_and(|c| c.server == instance.url && c.features.iter().any(|f| f == feature))
}
pub struct SessionRefs(ReceiptKey);
impl SessionRefs {
pub fn for_store(store: &CloudStore) -> Option<Self> {
if !announced(store, FEATURE_SESSION_REF) {
return None;
}
ReceiptKey::load_or_create(store.data_dir()).map(Self)
}
#[cfg(test)]
pub(crate) fn from_key(key: ReceiptKey) -> Self {
Self(key)
}
pub fn event_ref(&self, id: &str) -> Option<String> {
let id = id.trim();
lean_ctx::core::session::is_engine_session_id(id)
.then(|| format!("{EVENT_REF_PREFIX}{}", &self.0.mac_hex("session", id)[..32]))
}
pub fn episode_ref(&self, id: &str) -> String {
keyed_ses_ref(&self.0, id)
}
}
const BODY_REF_LABEL: &str = "session.episode";
fn keyed_ses_ref(key: &ReceiptKey, id: &str) -> String {
format!("ses_{}", &key.mac_hex(BODY_REF_LABEL, id.trim())[..24])
}
const LEGACY_REF_LABEL: &str = "session.legacy-ref";
pub const NO_SESSION_KEY: &str = "the local receipts key (receipts.hmac-key) cannot be read or \
created, so nothing that names a session is uploaded; check the permissions of the Cloud \
data directory";
fn local_key(cloud_data: &Path) -> Result<ReceiptKey, CloudError> {
ReceiptKey::load_or_create(cloud_data).ok_or_else(|| CloudError::Local(NO_SESSION_KEY.into()))
}
pub fn uploaded_ses_ref(cloud_data: &Path, id: &str) -> Result<String, CloudError> {
local_key(cloud_data).map(|key| keyed_ses_ref(&key, id))
}
pub fn rekeyed_legacy_ref(cloud_data: &Path, stored: &str) -> Result<String, CloudError> {
local_key(cloud_data).map(|key| {
format!(
"ses_{}",
&key.mac_hex(LEGACY_REF_LABEL, stored.trim())[..24]
)
})
}
#[cfg(test)]
pub(crate) fn make_keyless(cloud_data: &Path) {
std::fs::create_dir_all(cloud_data.join("receipts.hmac-key")).unwrap();
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
const ID: &str = "20261116-091500u123456p4242s0";
fn refs() -> SessionRefs {
SessionRefs::from_key(ReceiptKey::from_bytes([7; 32]))
}
#[test]
fn an_event_ref_is_keyed_and_names_only_generated_sessions() {
let r = refs().event_ref(ID).unwrap();
assert!(r.starts_with(EVENT_REF_PREFIX) && r.len() == 35, "{r}");
assert!(crate::events::build::valid_session_ref(&r));
assert_eq!(refs().event_ref(ID), Some(r.clone()));
let other = SessionRefs::from_key(ReceiptKey::from_bytes([8; 32]));
assert_ne!(other.event_ref(ID), Some(r));
for label in ["import", "cloud-link", "mcp-4242-0001", "", "ses_1"] {
assert_eq!(refs().event_ref(label), None, "{label}");
}
}
#[test]
fn nothing_carries_a_session_unless_the_enrolled_server_announced_it() {
let dir = tempfile::tempdir().unwrap();
let store = CloudStore::at(dir.path().join("config"), dir.path().join("cloud"));
assert!(SessionRefs::for_store(&store).is_none(), "not enrolled");
let instance: crate::state::InstanceRecord = serde_json::from_value(json!({
"instance_id": "inst_1", "org_id": "org_acme", "org_name": "Platform",
"label": "laptop", "url": "https://cloud.example", "org_salt_id": "salt",
"enrolled_at": "2026-11-16T12:00:00Z"
}))
.unwrap();
store
.write_config(crate::state::INSTANCE_FILE, &instance)
.unwrap();
assert!(SessionRefs::for_store(&store).is_none(), "never asked");
record(&store, "https://cloud.example", vec!["device_setup".into()]).unwrap();
assert!(SessionRefs::for_store(&store).is_none(), "an older Cloud");
record(
&store,
"https://other.example",
vec![FEATURE_SESSION_REF.into()],
)
.unwrap();
assert!(
SessionRefs::for_store(&store).is_none(),
"another server's answer"
);
record(
&store,
"https://cloud.example",
vec![FEATURE_SESSION_REF.into()],
)
.unwrap();
let refs = SessionRefs::for_store(&store).unwrap();
assert!(refs.event_ref(ID).is_some());
}
#[test]
fn only_a_capabilities_answer_announces_features() {
let ok = json!({"schema": "leanctx.cloud.capabilities/v1",
"features": ["device_setup", "session_ref", 3]});
assert_eq!(features_of(&ok), ["device_setup", "session_ref"]);
assert!(features_of(&json!({"features": ["session_ref"]})).is_empty());
let long = json!({"schema": "leanctx.cloud.capabilities/v1",
"features": ["x".repeat(65)]});
assert!(features_of(&long).is_empty());
let fixture: serde_json::Value = serde_json::from_str(include_str!(
"../../lean-ctx-protocol/tests/fixtures/cloud-v2/routes/capabilities.response.json"
))
.unwrap();
assert!(
features_of(&fixture)
.iter()
.any(|f| f == FEATURE_SESSION_REF)
);
}
#[test]
fn an_episode_ref_keeps_the_contract_shape_but_is_keyed_apart() {
let r = refs().episode_ref(ID);
let unsalted = crate::knowledge::draft::session_ref(ID);
assert!(r.starts_with("ses_") && r.len() == unsalted.len(), "{r}");
assert!(r[4..].chars().all(|c| c.is_ascii_hexdigit()));
assert_ne!(r, unsalted);
assert_eq!(refs().episode_ref(ID), r);
let event = refs().event_ref(ID).unwrap();
assert!(!event.contains(&r[4..]) && !r.contains(&event[3..27]));
let other = SessionRefs::from_key(ReceiptKey::from_bytes([8; 32]));
assert_ne!(other.episode_ref(ID), r);
}
#[test]
fn an_uploaded_body_ref_is_keyed_under_the_local_key_without_a_capability() {
let dir = tempfile::tempdir().unwrap();
let r = uploaded_ses_ref(dir.path(), ID).unwrap();
let unsalted = crate::knowledge::draft::session_ref(ID);
assert!(r.starts_with("ses_") && r.len() == unsalted.len(), "{r}");
assert!(r[4..].chars().all(|c| c.is_ascii_hexdigit()));
assert_ne!(r, unsalted, "never the unsalted digest");
assert!(!r.contains(ID));
assert_eq!(uploaded_ses_ref(dir.path(), ID).unwrap(), r);
let local = ReceiptKey::load_or_create(dir.path()).unwrap();
assert_eq!(SessionRefs::from_key(local).episode_ref(ID), r);
let other = tempfile::tempdir().unwrap();
assert_ne!(uploaded_ses_ref(other.path(), ID).unwrap(), r);
}
#[test]
fn without_a_readable_key_no_ref_is_made_rather_than_the_unsalted_one() {
let dir = tempfile::tempdir().unwrap();
make_keyless(dir.path());
assert!(ReceiptKey::load_or_create(dir.path()).is_none());
for id in [ID, "cli-import", "emspace-import"] {
let error = uploaded_ses_ref(dir.path(), id).unwrap_err();
assert!(error.to_string().contains("receipts.hmac-key"), "{error}");
let stored = crate::knowledge::draft::session_ref(id);
assert!(rekeyed_legacy_ref(dir.path(), &stored).is_err());
}
}
#[test]
fn a_queued_legacy_ref_is_keyed_apart_from_a_fresh_one() {
let dir = tempfile::tempdir().unwrap();
let stored = crate::knowledge::draft::session_ref(ID);
let rekeyed = rekeyed_legacy_ref(dir.path(), &stored).unwrap();
assert!(rekeyed.starts_with("ses_") && rekeyed.len() == stored.len());
assert!(rekeyed[4..].chars().all(|c| c.is_ascii_hexdigit()));
assert_ne!(rekeyed, stored, "never the unsalted digest");
assert_ne!(rekeyed, uploaded_ses_ref(dir.path(), ID).unwrap());
assert_eq!(rekeyed_legacy_ref(dir.path(), &stored).unwrap(), rekeyed);
let other = tempfile::tempdir().unwrap();
assert_ne!(rekeyed_legacy_ref(other.path(), &stored).unwrap(), rekeyed);
}
#[test]
fn a_404_from_a_proxy_clears_the_capability_like_a_missing_route() {
for gone in [
CloudError::NotFound,
CloudError::Server {
status: 404,
code: "unknown".into(),
},
] {
assert_eq!(announced_by(Err(gone)).unwrap(), Vec::<String>::new());
}
let busy = CloudError::Server {
status: 503,
code: "unknown".into(),
};
assert!(announced_by(Err(busy)).is_err(), "keeps the last answer");
let ok = json!({"schema": "leanctx.cloud.capabilities/v1", "features": ["session_ref"]});
assert_eq!(announced_by(Ok(ok)).unwrap(), [FEATURE_SESSION_REF]);
}
#[test]
fn the_event_ref_is_the_receipt_hmac_of_the_same_input_cut_to_fit() {
let key = ReceiptKey::from_bytes([7; 32]);
let full = key.mac_hex("session", ID);
assert_eq!(
refs().event_ref(ID).unwrap()[EVENT_REF_PREFIX.len()..],
full[..32]
);
}
}