use std::collections::BTreeSet;
use std::path::Path;
use lean_ctx_protocol::cloud::effective_policy::EffectivePolicyV1;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};
use super::v2::MessageType;
use crate::CloudError;
use crate::effective_floor;
use crate::knowledge::screening::Screen;
use crate::state::{lock_state, write_private};
pub const KINDS_MEMBER: &str = "agent_exchange";
pub const AUDIT_FILE: &str = "exchange-audit.jsonl";
pub const AUDIT_SCHEMA: &str = "leanctx.exchange-audit/v1";
pub const AUDIT_MAX: usize = 2000;
pub fn is_exchange(kind: MessageType) -> bool {
kind != MessageType::Progress
}
pub fn kind_name(kind: MessageType) -> String {
serde_json::to_value(kind)
.ok()
.and_then(|v| v.as_str().map(str::to_owned))
.unwrap_or_default()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Refusal {
pub rule: &'static str,
pub why: String,
}
impl Refusal {
pub fn new(rule: &'static str, why: impl Into<String>) -> Self {
Self {
rule,
why: why.into(),
}
}
}
impl std::fmt::Display for Refusal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} (rule: {})", self.why, self.rule)
}
}
pub fn account_refusal(org_kind: Option<&str>) -> Option<Refusal> {
(org_kind == Some("personal")).then(|| {
Refusal::new(
"account",
"knowledge and tasks are exchanged with a team: this device is connected to a personal account, which keeps its own memory",
)
})
}
pub(crate) fn allowed_kinds(
policy: &EffectivePolicyV1,
) -> Result<Option<BTreeSet<MessageType>>, ()> {
let sharing = serde_json::to_value(&policy.sharing).map_err(|_| ())?;
let Some(value) = sharing.get(KINDS_MEMBER) else {
return Ok(None);
};
let names = value.as_array().ok_or(())?;
let mut kinds = BTreeSet::new();
let mut last: Option<&str> = None;
for name in names {
let name = name.as_str().ok_or(())?;
if last.is_some_and(|prev| prev >= name) {
return Err(());
}
last = Some(name);
let kind = MessageType::parse(name)
.filter(|k| is_exchange(*k))
.ok_or(())?;
kinds.insert(kind);
}
Ok(Some(kinds))
}
fn vault_listed(allowed: &[String], vault_id: &str) -> bool {
let compact: String = vault_id
.chars()
.filter(|c| *c != '-')
.collect::<String>()
.to_ascii_lowercase();
allowed.iter().any(|v| v == vault_id || *v == compact)
}
pub fn policy_refusal(
policy: &EffectivePolicyV1,
vault_id: &str,
kind: MessageType,
) -> Option<Refusal> {
if let Some(allowed) = &policy.sharing.allowed_vaults
&& !vault_listed(allowed, vault_id)
{
return Some(Refusal::new(
"policy",
"the team policy does not allow sharing with this vault (sharing.allowed_vaults)",
));
}
match allowed_kinds(policy) {
Ok(None) => None,
Ok(Some(kinds)) if kinds.contains(&kind) => None,
Ok(Some(_)) => Some(Refusal::new(
"policy",
format!(
"the team policy does not allow exchanging a {} (sharing.{KINDS_MEMBER})",
kind_name(kind)
),
)),
Err(()) => Some(Refusal::new(
"policy",
format!(
"the team policy's sharing.{KINDS_MEMBER} is not a sorted list of message types; nothing is exchanged until it is repaired"
),
)),
}
}
pub fn floor_refusal(
cloud_data: &Path,
vault_id: &str,
kind: MessageType,
now: lean_ctx_time::UtcDateTime,
) -> Option<Refusal> {
if let Err(why) = crate::effective_policy::sensitive_gate(cloud_data, now) {
return Some(Refusal::new("policy", why));
}
let floor = effective_floor::read(&effective_floor::floor_dir_in(cloud_data)).and_then(
|(floor, required)| effective_floor::check(floor.as_deref(), required.as_deref()),
);
match floor {
Ok(None) => None,
Ok(Some(floor)) => policy_refusal(floor.policy.for_vault(Some(vault_id)), vault_id, kind),
Err(why) => Some(Refusal::new(
"policy",
format!("{why}; nothing is exchanged until the team policy is repaired"),
)),
}
}
pub fn screen_fields(screen: &mut Screen, fields: &[&str]) -> Result<(), Refusal> {
for field in fields.iter().filter(|f| !f.is_empty()) {
screen
.check(field)
.map_err(|blocked| Refusal::new("content", format!("it contains a {blocked}")))?;
}
Ok(())
}
fn strings_of<'a>(value: &'a Value, out: &mut Vec<&'a str>) {
match value {
Value::String(s) => out.push(s),
Value::Array(items) => items.iter().for_each(|v| strings_of(v, out)),
Value::Object(map) => map.values().for_each(|v| strings_of(v, out)),
_ => {}
}
}
pub fn inbound_refusal(
cloud_data: &Path,
screen: &mut Screen,
message: &Value,
vault_id: Option<&str>,
now: lean_ctx_time::UtcDateTime,
) -> Option<Refusal> {
let kind = MessageType::parse(message["type"].as_str()?)?;
if !is_exchange(kind) {
return None;
}
let body = &message["body"];
if message["sender"]["kind"].as_str() == Some("worker")
&& let Err(invalid) = super::person_rules::check_agent_body(kind, body)
{
return Some(Refusal::new("person_rules", invalid.to_string()));
}
let mut fields = Vec::new();
strings_of(body, &mut fields);
strings_of(&message["refs"], &mut fields);
if let Err(refusal) = screen_fields(screen, &fields) {
return Some(refusal);
}
vault_id.and_then(|vault| floor_refusal(cloud_data, vault, kind, now))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Delivery {
Pass,
Allowed(Box<Entry>),
Refused {
address: String,
seq: u64,
entry: Box<Entry>,
},
}
#[cfg_attr(not(feature = "http-server"), allow(dead_code))]
pub(crate) fn delivered(
cloud_data: &Path,
map: &crate::vault::VaultMap,
state: &super::relay_v2::StateV2,
message: &Value,
local_workers: &BTreeSet<String>,
now: lean_ctx_time::UtcDateTime,
) -> Delivery {
let Some((address, seq, record)) = super::relay_v2::new_delivery(map, state, message) else {
return Delivery::Pass;
};
let Some(kind) = message["type"]
.as_str()
.and_then(MessageType::parse)
.filter(|k| is_exchange(*k))
else {
return Delivery::Pass;
};
let sender = &message["sender"];
let peer = match sender["kind"].as_str() {
Some("worker") => sender["worker_id"].as_str().map(str::to_owned),
Some("person") => sender["handle"].as_str().map(|h| format!("person:{h}")),
_ => None,
};
if peer.as_ref().is_some_and(|p| local_workers.contains(p)) {
return Delivery::Pass;
}
let mut entry = Entry::now(now, "in", "delivered", kind, &address);
entry.msg_id = message["id"].as_str().map(str::to_owned);
entry.peer = peer;
entry.digest = Some(digest(&message["body"]));
let refusal = inbound_refusal(
cloud_data,
&mut Screen::for_installation(),
message,
record.map(|r| r.vault_id.as_str()),
now,
);
match refusal {
None => Delivery::Allowed(Box::new(entry)),
Some(refusal) => {
entry.outcome = "dropped".into();
Delivery::Refused {
address,
seq,
entry: Box::new(entry.refused_by(&refusal)),
}
}
}
}
pub fn digest(body: &Value) -> String {
let canonical = lean_ctx_protocol::cloud::canonical_json(body)
.unwrap_or_else(|_| body.to_string().into_bytes());
format!("sha256:{}", hex::encode(Sha256::digest(&canonical)))
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Entry {
pub at: String,
pub direction: String,
pub outcome: String,
pub kind: String,
pub address: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub msg_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub agent: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub peer: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rule: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub digest: Option<String>,
}
pub const REASON_MAX: usize = 300;
impl Entry {
pub fn now(
now: lean_ctx_time::UtcDateTime,
direction: &str,
outcome: &str,
kind: MessageType,
address: &str,
) -> Self {
Self {
at: now.to_rfc3339_opts(lean_ctx_time::SecondsFormat::Secs, true),
direction: direction.into(),
outcome: outcome.into(),
kind: kind_name(kind),
address: address.into(),
msg_id: None,
agent: None,
peer: None,
rule: None,
reason: None,
digest: None,
}
}
pub fn refused_by(mut self, refusal: &Refusal) -> Self {
self.rule = Some(refusal.rule.into());
self.reason = Some(refusal.why.chars().take(REASON_MAX).collect());
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct Header {
schema: String,
}
pub const COMPACT_BYTES: u64 = 4 << 20;
fn local(path: &Path, e: impl std::fmt::Display) -> CloudError {
CloudError::Local(format!("{}: {e}", path.display()))
}
fn ledger_bytes<'a>(entries: impl IntoIterator<Item = &'a Entry>) -> Result<Vec<u8>, CloudError> {
let encode = |e: serde_json::Error| CloudError::Local(e.to_string());
let mut bytes = serde_json::to_vec(&Header {
schema: AUDIT_SCHEMA.into(),
})
.map_err(encode)?;
bytes.push(b'\n');
for entry in entries {
bytes.extend(serde_json::to_vec(entry).map_err(encode)?);
bytes.push(b'\n');
}
Ok(bytes)
}
fn intact(path: &Path) -> Result<bool, CloudError> {
use std::io::{BufRead, Read, Seek, SeekFrom};
let mut file = std::fs::File::open(path).map_err(|e| local(path, e))?;
let mut last = [0u8; 1];
if file.seek(SeekFrom::End(-1)).is_err() || file.read_exact(&mut last).is_err() {
return Ok(false);
}
file.seek(SeekFrom::Start(0)).map_err(|e| local(path, e))?;
let mut first = String::new();
std::io::BufReader::new(file.take(4096))
.read_line(&mut first)
.map_err(|e| local(path, e))?;
Ok(last[0] == b'\n'
&& serde_json::from_str::<Header>(first.trim_end()).is_ok_and(|h| h.schema == AUDIT_SCHEMA))
}
fn append_line(path: &Path, line: &[u8]) -> Result<u64, CloudError> {
use std::io::Write;
let mut options = std::fs::OpenOptions::new();
options.append(true);
let mut file = options.open(path).map_err(|e| local(path, e))?;
file.write_all(line).map_err(|e| local(path, e))?;
file.sync_data().map_err(|e| local(path, e))?;
Ok(file.metadata().map_err(|e| local(path, e))?.len())
}
pub fn record(data_dir: &Path, entry: &Entry) -> Result<(), CloudError> {
record_bounded(data_dir, entry, COMPACT_BYTES)
}
pub(crate) fn record_bounded(
data_dir: &Path,
entry: &Entry,
compact_bytes: u64,
) -> Result<(), CloudError> {
let dir = data_dir.join("bus");
let _lock = lock_state(&dir, AUDIT_FILE)?;
let path = dir.join(AUDIT_FILE);
let existing = match std::fs::symlink_metadata(&path) {
Ok(meta) if meta.file_type().is_file() => true,
Ok(_) => return Err(local(&path, "not a regular file; nothing is recorded")),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => false,
Err(e) => return Err(local(&path, e)),
};
if existing && !intact(&path)? {
set_aside(&path)?;
} else if existing {
let mut line = serde_json::to_vec(entry).map_err(|e| CloudError::Local(e.to_string()))?;
line.push(b'\n');
if append_line(&path, &line)? < compact_bytes {
return Ok(());
}
let bytes = std::fs::read(&path).map_err(|e| local(&path, e))?;
match parse_entries(&bytes) {
Some(kept) => return write_private(&dir, AUDIT_FILE, &ledger_bytes(newest(&kept))?),
None => set_aside(&path)?,
}
}
write_private(&dir, AUDIT_FILE, &ledger_bytes([entry])?)
}
fn set_aside(path: &Path) -> Result<(), CloudError> {
let moved = crate::state::quarantine(path)?;
tracing::warn!(
"cloud exchange audit: {} is damaged; moved to {}",
path.display(),
moved.display()
);
Ok(())
}
fn newest(entries: &[Entry]) -> &[Entry] {
&entries[entries.len().saturating_sub(AUDIT_MAX)..]
}
fn read_entries(path: &Path) -> Result<Vec<Entry>, CloudError> {
let bytes = std::fs::read(path).map_err(|e| local(path, e))?;
parse_entries(&bytes).ok_or_else(|| CloudError::Local(format!("{} is damaged", path.display())))
}
fn parse_entries(bytes: &[u8]) -> Option<Vec<Entry>> {
let body = std::str::from_utf8(bytes).ok()?.strip_suffix('\n')?;
let mut lines = body.split('\n');
let header: Header = serde_json::from_str(lines.next()?).ok()?;
if header.schema != AUDIT_SCHEMA {
return None;
}
lines
.map(|l| serde_json::from_str::<Entry>(l).ok())
.collect()
}
pub fn entries(data_dir: &Path) -> Result<Vec<Entry>, CloudError> {
let path = data_dir.join("bus").join(AUDIT_FILE);
if !path.exists() {
return Ok(Vec::new());
}
let mut all = read_entries(&path)?;
let excess = all.len().saturating_sub(AUDIT_MAX);
all.drain(..excess);
Ok(all)
}
#[cfg_attr(not(feature = "http-server"), allow(dead_code))]
pub(crate) fn admit(
cloud_data: &Path,
map: &crate::vault::VaultMap,
state: &mut super::relay_v2::StateV2,
message: &Value,
local_workers: &BTreeSet<String>,
now: lean_ctx_time::UtcDateTime,
) -> Result<bool, CloudError> {
match delivered(cloud_data, map, state, message, local_workers, now) {
Delivery::Pass => Ok(true),
Delivery::Allowed(entry) => record(cloud_data, &entry).map(|()| true),
Delivery::Refused {
address,
seq,
entry,
} => {
record(cloud_data, &entry)?;
super::relay_v2::skip_delivery(state, &address, seq);
Ok(false)
}
}
}
#[cfg(test)]
#[path = "exchange_tests.rs"]
mod tests;