use std::cell::Cell;
use std::sync::Mutex;
use std::time::{Duration, Instant};
use base64::Engine as _;
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64URL;
use lean_ctx_cloud::bus::relay_v2::PERSON_PUBLISH_DOMAIN;
use lean_ctx_cloud::consent::chat_key::ChatKeyCheck;
use lean_ctx_cloud::{CloudError, CloudStore};
use serde_json::{Map, Value, json};
use super::chat_part::{self, Call};
use super::{Answer, Code, Failure, Via};
pub(crate) const PREPARE: &str = "chat.prepare";
pub(crate) const PREPARE_KINDS: &[&str] = &["post", "dm", "bus"];
pub(crate) const PERSON_POST_KINDS: &[&str] = &["chat.post", "chat.dm", "bus.post", PREPARE];
pub(crate) const MAX_PREPARED: usize = 64;
pub(crate) const TTL: Duration = Duration::from_secs(120);
pub(crate) const FRAME_FLAG: &str = "--frame-b64";
pub(crate) const SIGNATURE_FLAG: &str = "--presence-signature";
pub(crate) const KEY_ID_FLAG: &str = "--key-id";
pub(crate) const FROM_TERMINAL: &str = "Team chat posts are sent from the LeanCTX Code app.";
pub(crate) const FROM_OLD_APP: &str = "Update LeanCTX Code to send team messages.";
thread_local! {
static FROM_APP: Cell<bool> = const { Cell::new(false) };
}
pub(crate) fn with_app<T>(from_app: bool, f: impl FnOnce() -> T) -> T {
let before = FROM_APP.with(|c| c.replace(from_app));
let out = f();
FROM_APP.with(|c| c.set(before));
out
}
pub(crate) fn signature_required() -> Failure {
let text = if FROM_APP.with(Cell::get) {
FROM_OLD_APP
} else {
FROM_TERMINAL
};
Failure::new(Code::SignatureRequired, text)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Kind {
Post,
Dm,
Bus,
}
impl Kind {
fn of_prepare(raw: &str) -> Option<Self> {
match raw {
_ if raw == PREPARE_KINDS[0] => Some(Self::Post),
_ if raw == PREPARE_KINDS[1] => Some(Self::Dm),
_ if raw == PREPARE_KINDS[2] => Some(Self::Bus),
_ => None,
}
}
pub(crate) fn sent_by(route: &str) -> &'static [Self] {
match route {
"chat.post" => &[Self::Post, Self::Dm],
"chat.dm" => &[Self::Dm],
"bus.post" => &[Self::Bus],
_ => &[],
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Send {
Rest { path: String },
Bus { args: Vec<String> },
}
#[derive(Debug, Clone)]
struct Prepared {
frame_b64: String,
frame: Map<String, Value>,
signed: Map<String, Value>,
kind: Kind,
expires: Instant,
used: bool,
send: Send,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Ready {
pub frame: Map<String, Value>,
pub signature: String,
pub send: Send,
}
#[derive(Debug, Default)]
pub(crate) struct Store {
frames: Vec<Prepared>,
}
fn canonical(frame: &Map<String, Value>) -> Result<Vec<u8>, Failure> {
lean_ctx_protocol::cloud::signing_input("", &Value::Object(frame.clone()))
.map_err(|e| Failure::new(Code::Internal, e.to_string()))
}
impl Store {
pub(crate) const fn new() -> Self {
Self { frames: Vec::new() }
}
pub(crate) fn insert(
&mut self,
kind: Kind,
frame: Map<String, Value>,
signed: Map<String, Value>,
send: Send,
now: Instant,
) -> Result<String, Failure> {
let frame_b64 = B64URL.encode(canonical(&frame)?);
self.frames.retain(|p| now < p.expires);
if self
.frames
.iter()
.any(|p| p.frame_b64 == frame_b64 && p.used)
{
return Err(Failure::new(Code::FrameUnknown, ALREADY_SENT));
}
self.frames.retain(|p| p.frame_b64 != frame_b64);
while self.frames.len() >= MAX_PREPARED {
self.frames.remove(0);
}
self.frames.push(Prepared {
frame_b64: frame_b64.clone(),
frame,
signed,
kind,
expires: now + TTL,
used: false,
send,
});
Ok(frame_b64)
}
#[cfg(test)]
pub(crate) fn take(
&mut self,
kind: Kind,
frame_b64: &str,
signature: &str,
key_id: &str,
check: Option<&ChatKeyCheck>,
now: Instant,
) -> Result<Ready, Failure> {
self.take_any(&[kind], frame_b64, signature, key_id, check, now)
}
pub(crate) fn take_any(
&mut self,
kinds: &[Kind],
frame_b64: &str,
signature: &str,
key_id: &str,
check: Option<&ChatKeyCheck>,
now: Instant,
) -> Result<Ready, Failure> {
let unknown = |why: &str| Failure::new(Code::FrameUnknown, why.to_owned());
let Some(prepared) = self
.frames
.iter_mut()
.find(|p| p.frame_b64 == frame_b64 && kinds.contains(&p.kind))
else {
return Err(unknown(
"this frame was not prepared here (prepare it again with chat.prepare)",
));
};
if prepared.used {
return Err(unknown(ALREADY_SENT));
}
if now >= prepared.expires {
return Err(Failure::new(
Code::FrameExpired,
"the prepared frame expired; prepare it again",
));
}
let Some(check) = check else {
return Err(chat_key_missing());
};
if key_id != check.key.key_id {
return Err(Failure::new(
Code::SignatureInvalid,
"key_id is not the chat key bound to this machine",
));
}
if !check.admits(
PERSON_PUBLISH_DOMAIN,
&Value::Object(prepared.signed.clone()),
signature,
) {
return Err(Failure::new(
Code::SignatureInvalid,
"the signature does not verify with the bound chat key",
));
}
prepared.used = true;
Ok(Ready {
frame: prepared.frame.clone(),
signature: signature.to_owned(),
send: prepared.send.clone(),
})
}
}
impl Store {
pub(crate) fn release(&mut self, frame_b64: &str, now: Instant) {
if let Some(prepared) = self
.frames
.iter_mut()
.find(|p| p.frame_b64 == frame_b64 && now < p.expires)
{
prepared.used = false;
}
}
}
const ALREADY_SENT: &str = "this post was already sent; check the chat before sending it again";
pub(crate) fn reached_the_cloud(outcome: &Result<(u16, Value), Failure>) -> bool {
match outcome {
Ok((status, _)) => *status < 500,
Err(failure) => failure.code == Code::Forbidden,
}
}
pub(crate) fn release_signed(args: &[String]) {
if let Ok(Some(frame_b64)) = super::flag(args, FRAME_FLAG) {
store().release(frame_b64, Instant::now());
}
}
static STORE: Mutex<Store> = Mutex::new(Store::new());
fn store() -> std::sync::MutexGuard<'static, Store> {
STORE
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
fn chat_key_missing() -> Failure {
Failure::new(
Code::ChatKeyMissing,
"no chat key is bound to this machine: connect it again from LeanCTX Code",
)
}
fn connected() -> Result<(CloudStore, String, Option<ChatKeyCheck>), Failure> {
let store = CloudStore::open()?;
let instance = store.instance()?.ok_or(CloudError::NotConnected)?;
let instance_id = signed_instance_id(&instance.instance_id);
let check = ChatKeyCheck::load(&store, &instance_id);
Ok((store, instance_id, check))
}
pub(crate) fn signed_instance_id(stored: &str) -> String {
lean_ctx_cloud::workers::canonical_instance_id(stored)
}
pub(crate) fn offered() -> bool {
matches!(connected(), Ok((_, _, Some(_))))
}
pub(crate) fn rest_signed_object_post(
channel_id: &str,
body: &Map<String, Value>,
) -> Map<String, Value> {
let mut object = body.clone();
object.insert("t".into(), "chat.message".into());
object.insert("channel_id".into(), channel_id.to_ascii_lowercase().into());
object
}
pub(crate) fn rest_signed_object_dm(body: &Map<String, Value>) -> Map<String, Value> {
let mut object = body.clone();
object.insert("t".into(), "chat.dm".into());
object
}
fn client_msg_id() -> String {
uuid::Uuid::new_v4().hyphenated().to_string()
}
fn bus_frame(store: &CloudStore, args: &[String]) -> Result<Map<String, Value>, Failure> {
use lean_ctx::core::a2a::message::PrivacyLevel;
use lean_ctx::core::agents::ScratchpadEntry;
use lean_ctx_cloud::bus::address::Address;
let to = super::flags(args, "--to")?;
let [to] = to.as_slice() else {
return Err(Failure::invalid(
"a signed bus post names one vault in --to",
));
};
let Ok(Address::Vault { workspace, vault }) = Address::parse(Some(to)) else {
return Err(Failure::invalid(
"a signed bus post goes to a vault (#team/vault)",
));
};
let map = lean_ctx_cloud::vault::VaultMap::load(store.data_dir());
let record = map
.vaults
.values()
.find(|r| r.ns == workspace && r.name == vault)
.ok_or_else(|| Failure::new(Code::NoVault, "this machine does not follow that vault"))?;
let message = super::flag(args, "--message")?
.filter(|m| !m.trim().is_empty())
.ok_or_else(|| Failure::invalid("message is required"))?;
let category = super::flag(args, "--category")?.unwrap_or_default();
let field = raw_text_field(category, message)?;
let mut metadata = std::collections::HashMap::new();
if let Some(thread) = super::flag(args, "--thread")? {
metadata.insert(super::bus::THREAD_KEY.to_owned(), thread.to_owned());
}
let entry = ScratchpadEntry {
id: uuid::Uuid::new_v4().simple().to_string(),
from_agent: super::bus::PERSON.to_owned(),
to_agent: Some((*to).to_owned()),
task_id: super::flag(args, "--task-id")?.map(str::to_owned),
category: category.to_owned(),
priority: Default::default(),
privacy: PrivacyLevel::Team,
message: message.to_owned(),
metadata,
project_root: None,
timestamp: lean_ctx_time::UtcDateTime::now(),
read_by: Vec::new(),
expires_at: None,
};
let mut frame = lean_ctx_cloud::bus::relay_v2::person_frame(
&entry,
&lean_ctx_cloud::bus::relay_v2::address_of(record),
)
.ok_or_else(|| {
Failure::new(
Code::Withheld,
"this kind of message is not sent to the vault (answers, findings, offers, handoffs)",
)
})?;
with_raw_text(&mut frame, field, message)?;
with_app_fields(&mut frame, args)?;
Ok(frame)
}
const STEP_MAX: usize = 300;
fn raw_text_field(category: &str, message: &str) -> Result<&'static str, Failure> {
let (field, max) = match category {
"question" => ("text", lean_ctx_cloud::bus::v2::TEXT_MAX),
"progress" => ("step", STEP_MAX),
_ => {
return Err(Failure::invalid(
"a signed bus post is a question or a progress note (category)",
));
}
};
if message.chars().count() > max {
return Err(Failure::invalid(format!(
"message is at most {max} characters for a {category}"
)));
}
Ok(field)
}
fn with_raw_text(
frame: &mut Map<String, Value>,
field: &str,
message: &str,
) -> Result<(), Failure> {
frame
.get_mut("body")
.and_then(Value::as_object_mut)
.ok_or_else(|| Failure::new(Code::Internal, "a bus frame without a body"))?
.insert(field.into(), message.into());
Ok(())
}
const APP_FIELD_FLAGS: [&str; 4] = ["--client-msg-id", "--thread-id", "--reply-to", "--deadline"];
fn with_app_fields(frame: &mut Map<String, Value>, args: &[String]) -> Result<(), Failure> {
if let Some(id) = super::flag(args, APP_FIELD_FLAGS[0])? {
if !is_ulid(id) {
return Err(Failure::invalid(
"client_msg_id is a ULID (26 characters, Crockford base32)",
));
}
frame.insert("client_msg_id".into(), id.into());
}
for (name, flag) in [
("thread_id", APP_FIELD_FLAGS[1]),
("reply_to", APP_FIELD_FLAGS[2]),
] {
if let Some(id) = super::flag(args, flag)? {
if !is_msg_id(id) {
return Err(Failure::invalid(format!(
"{name} is msg_ and 16 lowercase hex digits"
)));
}
frame.insert(name.into(), id.into());
}
}
if let Some(deadline) = super::flag(args, APP_FIELD_FLAGS[3])? {
let ms: u64 = deadline
.parse()
.map_err(|_| Failure::invalid("deadline is unix milliseconds"))?;
if frame.get("type").and_then(Value::as_str) != Some("question") {
return Err(Failure::invalid("only a question has a deadline"));
}
frame
.get_mut("body")
.and_then(Value::as_object_mut)
.ok_or_else(|| Failure::new(Code::Internal, "a bus frame without a body"))?
.insert("deadline".into(), ms.into());
}
Ok(())
}
fn is_ulid(id: &str) -> bool {
id.len() == 26
&& id.as_bytes()[0] <= b'7'
&& id
.bytes()
.all(|b| b.is_ascii_digit() || (b.is_ascii_uppercase() && !b"ILOU".contains(&b)))
}
fn is_msg_id(id: &str) -> bool {
id.strip_prefix("msg_").is_some_and(|r| {
r.len() == 16
&& r.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
})
}
fn bus_args(args: &[String]) -> Vec<String> {
let start = args.iter().position(|a| a == "--json").unwrap_or(0);
let mut out = Vec::new();
let mut rest = args[start..].iter();
while let Some(arg) = rest.next() {
if arg == "--kind" || APP_FIELD_FLAGS.contains(&arg.as_str()) {
rest.next();
continue;
}
out.push(arg.clone());
}
out
}
pub(crate) fn prepare(args: &[String]) -> Result<Value, Failure> {
let kind = super::flag(args, "--kind")?
.and_then(Kind::of_prepare)
.ok_or_else(|| Failure::invalid("kind is post, dm or bus"))?;
let (cloud, instance_id, check) = connected()?;
let check = check.ok_or_else(chat_key_missing)?;
let (frame, signed, send) = match kind {
Kind::Post => {
let call = chat_part::call_of("chat.post", args)?;
let channel = super::flag(args, "--channel")?.unwrap_or_default();
let body = body_with_id(&call, args, true)?;
let signed = rest_signed_object_post(channel, &body);
(body, signed, Send::Rest { path: call.path })
}
Kind::Dm => {
let call = chat_part::call_of("chat.dm", args)?;
let body = body_with_id(&call, args, false)?;
let signed = rest_signed_object_dm(&body);
(body, signed, Send::Rest { path: call.path })
}
Kind::Bus => {
let frame = bus_frame(&cloud, args)?;
let send = Send::Bus {
args: bus_args(args),
};
(frame.clone(), frame, send)
}
};
let signing_input = lean_ctx_cloud::consent::person_sign::signing_input(
PERSON_PUBLISH_DOMAIN,
&Value::Object(signed.clone()),
&instance_id,
)
.map_err(|e| Failure::new(Code::Internal, e))?;
let frame_b64 = store().insert(kind, frame, signed, send, Instant::now())?;
let now = lean_ctx_time::UtcDateTime::now().timestamp();
let ttl = i64::try_from(TTL.as_secs()).unwrap_or(120);
let expires_at = lean_ctx_time::UtcDateTime::from_timestamp(now + ttl, 0)
.map(|at| at.to_rfc3339_opts(lean_ctx_time::SecondsFormat::Secs, true));
Ok(json!({
"frame_b64": frame_b64,
"signing_input_b64": B64URL.encode(signing_input),
"instance_id": instance_id,
"key_id_expected": check.key.key_id,
"expires_at": expires_at,
}))
}
pub(crate) const CLIENT_MSG_ID_FLAG: &str = "--client-msg-id";
pub(crate) const ORCHESTRATOR_FLAG: &str = "--orchestrator-value";
fn body_with_id(call: &Call, args: &[String], post: bool) -> Result<Map<String, Value>, Failure> {
let mut body = match &call.body {
Some(Value::Object(body)) => body.clone(),
_ => Map::new(),
};
if let Some(raw) = super::flag(args, ORCHESTRATOR_FLAG)? {
if !post {
return Err(Failure::invalid("a direct message has no orchestrator"));
}
let on = match raw {
"true" => true,
"false" => false,
_ => return Err(Failure::invalid("orchestrator is true or false")),
};
body.insert("orchestrator".into(), on.into());
}
let id = match super::flag(args, CLIENT_MSG_ID_FLAG)? {
Some(id) if is_uuid_v4(id) => id.to_owned(),
Some(_) => {
return Err(Failure::invalid(
"client_msg_id of a post or direct message is a lowercase UUID v4",
));
}
None => client_msg_id(),
};
body.insert("client_msg_id".into(), id.into());
Ok(body)
}
fn is_uuid_v4(id: &str) -> bool {
let b = id.as_bytes();
b.len() == 36
&& b.iter().enumerate().all(|(i, c)| match i {
8 | 13 | 18 | 23 => *c == b'-',
_ => c.is_ascii_digit() || (b'a'..=b'f').contains(c),
})
&& b[14] == b'4'
&& matches!(b[19], b'8' | b'9' | b'a' | b'b')
}
pub(crate) fn prepare_answer(args: &[String]) -> Answer {
super::answer(PREPARE, || prepare(args))
}
pub(crate) fn take_signed(kinds: &[Kind], args: &[String]) -> Result<Ready, Failure> {
let need = |flag: &str, name: &str| {
super::flag(args, flag)?
.filter(|v| !v.is_empty())
.map(str::to_owned)
.ok_or_else(|| Failure::invalid(format!("{name} is required with frame_b64")))
};
let frame_b64 = need(FRAME_FLAG, "frame_b64")?;
let signature = need(SIGNATURE_FLAG, "presence_signature")?;
let key_id = need(KEY_ID_FLAG, "key_id")?;
let (_, _, check) = connected()?;
store().take_any(
kinds,
&frame_b64,
&signature,
&key_id,
check.as_ref(),
Instant::now(),
)
}
pub(crate) fn is_signed(args: &[String]) -> bool {
args.iter().any(|a| a == FRAME_FLAG)
}
pub(crate) fn rest_body(ready: &Ready) -> Value {
let mut body = ready.frame.clone();
body.insert(
lean_ctx_cloud::consent::person_sign::SIGNATURE_FIELD.into(),
ready.signature.clone().into(),
);
Value::Object(body)
}
fn names(body: &Value, word: &str) -> bool {
["code", "reason", "field"]
.iter()
.any(|k| body["error"][*k].as_str() == Some(word))
}
pub(crate) fn signed_outcome(status: u16, body: Value) -> Result<Value, Failure> {
if status == 409 && names(&body, "client_msg_id_reused") {
return Err(Failure::new(
Code::FrameUnknown,
"LeanCTX Cloud already has another message under this frame's id; prepare it again",
));
}
if status == 400 && names(&body, "client_msg_id") {
return Err(Failure::new(
Code::Internal,
"LeanCTX Cloud refused the prepared message id",
));
}
chat_part::outcome(status, body)
}
pub(crate) fn rest_answer(kind: &str, args: &[String], via: Via) -> Answer {
let kinds = Kind::sent_by(kind);
if kinds.is_empty() {
return Answer::failure(&Failure::invalid(format!("unknown kind {kind}")));
}
let ready = match take_signed(kinds, args) {
Ok(ready) => ready,
Err(failure) => return Answer::failure(&failure),
};
let Send::Rest { path } = &ready.send else {
return Answer::failure(&Failure::new(Code::FrameUnknown, "not a chat frame"));
};
let body = rest_body(&ready);
let call = Call {
method: "POST",
path: path.clone(),
body: Some(body.clone()),
};
let reached = std::cell::Cell::new(false);
let answer = super::answer_released_write(chat_part::TOOL, "", kind, via, &body, || {
let outcome = chat_part::run_raw(&call);
reached.set(reached_the_cloud(&outcome));
let (status, answer) = outcome?;
signed_outcome(status, answer)
});
if !reached.get() {
release_signed(args);
}
answer
}
#[cfg(test)]
mod tests {
use super::*;
use ed25519_dalek::{Signer as _, SigningKey};
use lean_ctx_cloud::consent::chat_key::BoundChatKey;
use sha2::Digest as _;
const INSTANCE: &str = "3f1c2b6a-8d4e-4a7b-9c10-5e2f6a7b8c09";
fn key() -> SigningKey {
let seed: [u8; 32] = sha2::Sha256::digest(b"leanctx test chat key: rc4-32").into();
SigningKey::from_bytes(&seed)
}
fn check(key: &SigningKey) -> ChatKeyCheck {
let public = key.verifying_key().to_bytes();
ChatKeyCheck {
key: BoundChatKey {
alg: "ed25519".into(),
key_id: lean_ctx_cloud::consent::chat_key::key_id_of(&public),
public_key: B64URL.encode(public),
},
instance_id: INSTANCE.into(),
}
}
fn sign(key: &SigningKey, frame: &Map<String, Value>) -> String {
let input = lean_ctx_cloud::consent::person_sign::signing_input(
PERSON_PUBLISH_DOMAIN,
&Value::Object(frame.clone()),
INSTANCE,
)
.unwrap();
B64URL.encode(key.sign(&input).to_bytes())
}
const STORED_1X: &str = "inst_3F1C2B6A8D4E4A7B9C105E2F6A7B8C09";
#[test]
fn signed_instance_id_is_the_dashed_uuid_for_a_1x_enrolment() {
assert_eq!(signed_instance_id(STORED_1X), INSTANCE);
assert_eq!(
signed_instance_id("inst_3f1c2b6a8d4e4a7b9c105e2f6a7b8c09"),
INSTANCE
);
assert_eq!(signed_instance_id(INSTANCE), INSTANCE);
}
#[test]
fn a_1x_enrolment_signs_and_checks_the_dashed_uuid() {
let frame = rest_signed_object_post("CH_1", &post_frame());
let instance_id = signed_instance_id(STORED_1X);
let input = lean_ctx_cloud::consent::person_sign::signing_input(
PERSON_PUBLISH_DOMAIN,
&Value::Object(frame.clone()),
&instance_id,
)
.unwrap();
let text = String::from_utf8(input).unwrap();
assert!(
text.contains(&format!("\"instance_id\":\"{INSTANCE}\"")),
"{text}"
);
assert!(!text.contains("inst_"), "{text}");
let key = key();
let mut check = check(&key);
check.instance_id = instance_id;
assert!(check.admits(
PERSON_PUBLISH_DOMAIN,
&Value::Object(frame.clone()),
&sign(&key, &frame)
));
}
fn post_frame() -> Map<String, Value> {
json!({"body": "hello team", "client_msg_id": "6f1c0a52-3c1e-4f43-9a52-0f5d2f8e1b77"})
.as_object()
.unwrap()
.clone()
}
fn dm_frame() -> Map<String, Value> {
json!({"members": ["0c1d2e3f-4a5b-4c6d-8e7f-9a0b1c2d3e4f"], "client_msg_id": "0b3f5c4e-8d7a-4c21-b5e9-2a6d1f0c9e88"})
.as_object()
.unwrap()
.clone()
}
fn signed_of(kind: Kind, frame: &Map<String, Value>) -> Map<String, Value> {
match kind {
Kind::Post => rest_signed_object_post("CH_1", frame),
Kind::Dm => rest_signed_object_dm(frame),
Kind::Bus => frame.clone(),
}
}
#[test]
fn a_prepared_bus_frame_carries_only_the_app_fields() {
use lean_ctx::core::a2a::message::PrivacyLevel;
use lean_ctx::core::agents::ScratchpadEntry;
let entry = |category: &str, message: &str| ScratchpadEntry {
id: "e1".into(),
from_agent: super::super::bus::PERSON.to_owned(),
to_agent: Some("#acme/payments".into()),
task_id: None,
category: category.into(),
priority: Default::default(),
privacy: PrivacyLevel::Team,
message: message.into(),
metadata: std::collections::HashMap::new(),
project_root: None,
timestamp: lean_ctx_time::UtcDateTime::now(),
read_by: Vec::new(),
expires_at: None,
};
let frame_of = |category: &str, message: &str, flags: &[&str]| {
let mut frame = lean_ctx_cloud::bus::relay_v2::person_frame(
&entry(category, message),
"#acme/payments",
)
.unwrap();
let args: Vec<String> = flags.iter().map(|f| (*f).to_owned()).collect();
with_app_fields(&mut frame, &args).map(|()| frame)
};
let ulid = "01JT0000000000000000000000";
let question = frame_of(
"question",
"Who owns the E14 rollout?",
&[
"--client-msg-id",
ulid,
"--thread-id",
"msg_0123456789abcdef",
"--reply-to",
"msg_fedcba9876543210",
"--deadline",
"1764000000000",
],
)
.unwrap();
eprintln!("BUS_PREPARED_FRAME {}", Value::Object(question.clone()));
let mut keys: Vec<&str> = question.keys().map(String::as_str).collect();
keys.sort_unstable();
assert_eq!(
keys,
[
"body",
"client_msg_id",
"reply_to",
"schema",
"sender",
"t",
"thread_id",
"to",
"type"
]
);
assert_eq!(question["client_msg_id"], ulid, "kept as given");
assert_eq!(question["to"], "#acme/payments");
assert_eq!(question["type"], "question");
assert_eq!(
question["body"],
json!({"text": "Who owns the E14 rollout?", "deadline": 1_764_000_000_000_u64})
);
assert_eq!(question["sender"], json!({"kind": "person"}));
let progress = frame_of("progress", "deploy done", &[]).unwrap();
assert_eq!(progress["type"], "progress");
assert_eq!(progress["body"], json!({"step": "deploy done"}));
for bad in [
&["--client-msg-id", "01jt0000000000000000000000"][..],
&["--client-msg-id", "8ZZZZZZZZZZZZZZZZZZZZZZZZZ"],
&["--thread-id", "msg_0123"],
&["--reply-to", "m_0123456789abcdef"],
&["--deadline", "soon"],
] {
assert!(frame_of("question", "q?", bad).is_err(), "{bad:?}");
}
assert!(
frame_of("progress", "p", &["--deadline", "1"]).is_err(),
"only a question has a deadline"
);
assert_eq!(
bus_args(&[
"bus".into(),
"--json".into(),
"--kind".into(),
"bus".into(),
"--client-msg-id".into(),
ulid.into(),
"--deadline".into(),
"1".into(),
"--to".into(),
"#acme/payments".into(),
]),
["--json", "--to", "#acme/payments"]
);
}
fn bus_frame_fixture() -> Map<String, Value> {
json!({
"t": "place.publish",
"to": "#team/main",
"client_msg_id": "01JT0000000000000000000000",
"sender": {"kind": "person"},
"body": {"step": "deploy done"},
})
.as_object()
.unwrap()
.clone()
}
#[test]
fn chat_sign_happy_path_for_post_dm_and_bus() {
let key = key();
let check = check(&key);
let now = Instant::now();
for (kind, frame, send) in [
(
Kind::Post,
post_frame(),
Send::Rest {
path: "/chat/channels/ch_1/messages".into(),
},
),
(
Kind::Dm,
dm_frame(),
Send::Rest {
path: "/chat/dms".into(),
},
),
(
Kind::Bus,
bus_frame_fixture(),
Send::Bus {
args: vec!["--json".into()],
},
),
] {
let mut store = Store::new();
let signed = signed_of(kind, &frame);
let b64 = store
.insert(kind, frame.clone(), signed.clone(), send.clone(), now)
.unwrap();
assert_eq!(B64URL.decode(&b64).unwrap(), canonical(&frame).unwrap());
let sig = sign(&key, &signed);
let input = lean_ctx_cloud::consent::person_sign::signing_input(
PERSON_PUBLISH_DOMAIN,
&Value::Object(signed),
INSTANCE,
)
.unwrap();
eprintln!(
"CHAT_SIGN_BYTES {kind:?} {}",
String::from_utf8(input).unwrap().escape_debug()
);
if kind != Kind::Bus {
let bare = sign(&key, &frame);
let refused = store
.take(kind, &b64, &bare, &check.key.key_id, Some(&check), now)
.unwrap_err();
assert_eq!(refused.code, Code::SignatureInvalid);
}
let ready = store
.take(kind, &b64, &sig, &check.key.key_id, Some(&check), now)
.unwrap();
assert_eq!(ready.frame, frame, "forwarded unchanged");
assert_eq!(ready.send, send);
let body = rest_body(&ready);
assert_eq!(body["presence_signature"], json!(sig));
let again = store
.take(kind, &b64, &sig, &check.key.key_id, Some(&check), now)
.unwrap_err();
assert_eq!(again.code, Code::FrameUnknown);
}
let post = rest_signed_object_post("CH_1", &post_frame());
assert_eq!(post["t"], json!("chat.message"));
assert_eq!(post["channel_id"], json!("ch_1"));
let dm = rest_signed_object_dm(&dm_frame());
assert_eq!(dm["t"], json!("chat.dm"));
assert!(!post_frame().contains_key("t"), "the route is never sent");
}
#[test]
fn chat_sign_refuses_what_does_not_check_out() {
let key = key();
let check = check(&key);
let now = Instant::now();
let mut store = Store::new();
let frame = post_frame();
let signed = rest_signed_object_post("ch_1", &frame);
let send = Send::Rest { path: "/x".into() };
let b64 = store
.insert(Kind::Post, frame.clone(), signed.clone(), send, now)
.unwrap();
let sig = sign(&key, &signed);
let take = |store: &mut Store, kind, b64: &str, sig: &str, id: &str, check, at| {
store.take(kind, b64, sig, id, check, at).unwrap_err().code
};
let id = check.key.key_id.clone();
assert_eq!(
take(
&mut store,
Kind::Post,
&b64,
&sig,
"ab".repeat(8).as_str(),
Some(&check),
now
),
Code::SignatureInvalid
);
let other = sign(&SigningKey::from_bytes(&[7; 32]), &signed);
assert_eq!(
take(&mut store, Kind::Post, &b64, &other, &id, Some(&check), now),
Code::SignatureInvalid
);
assert_eq!(
take(&mut store, Kind::Post, &b64, "%%", &id, Some(&check), now),
Code::SignatureInvalid
);
assert_eq!(
take(&mut store, Kind::Post, &b64, &sig, &id, None, now),
Code::ChatKeyMissing
);
assert_eq!(
take(&mut store, Kind::Post, "AAAA", &sig, &id, Some(&check), now),
Code::FrameUnknown
);
assert_eq!(
take(&mut store, Kind::Dm, &b64, &sig, &id, Some(&check), now),
Code::FrameUnknown,
"a frame is only good for the kind it was prepared for"
);
assert_eq!(
take(
&mut store,
Kind::Post,
&b64,
&sig,
&id,
Some(&check),
now + TTL
),
Code::FrameExpired
);
assert!(
store
.take(Kind::Post, &b64, &sig, &id, Some(&check), now)
.is_ok()
);
}
#[test]
fn chat_sign_keeps_the_app_client_msg_id_and_orchestrator() {
const MSG_ID: &str = "8a6f0c3e-2b1d-4e5f-9a7b-3c4d5e6f7a8b";
const CHANNEL: &str = "6b2d9c4e-1f3a-4b5c-8d7e-9f0a1b2c3d4e";
let args = |extra: &[&str]| -> Vec<String> {
["--json", "--channel", CHANNEL, "--body", "hi"]
.iter()
.chain(extra)
.map(|a| (*a).to_owned())
.collect()
};
let body_of = |extra: &[&str]| {
let args = args(extra);
let call = chat_part::call_of("chat.post", &args).unwrap();
body_with_id(&call, &args, true)
};
for (orchestrator, want) in [("false", false), ("true", true)] {
let body =
body_of(&[CLIENT_MSG_ID_FLAG, MSG_ID, ORCHESTRATOR_FLAG, orchestrator]).unwrap();
let signed = rest_signed_object_post(CHANNEL, &body);
let expected = json!({
"t": "chat.message",
"channel_id": CHANNEL,
"body": "hi",
"client_msg_id": MSG_ID,
"orchestrator": want,
});
assert_eq!(Value::Object(signed.clone()), expected);
let input = lean_ctx_cloud::consent::person_sign::signing_input(
PERSON_PUBLISH_DOMAIN,
&Value::Object(signed),
INSTANCE,
)
.unwrap();
assert_eq!(
String::from_utf8(input).unwrap(),
format!(
"leanctx.cloud.person_publish.v1\n{{\"body\":\"hi\",\"channel_id\":\"{CHANNEL}\",\"client_msg_id\":\"{MSG_ID}\",\"instance_id\":\"{INSTANCE}\",\"orchestrator\":{want},\"t\":\"chat.message\"}}"
)
);
}
let plain = body_of(&[CLIENT_MSG_ID_FLAG, MSG_ID]).unwrap();
assert!(!plain.contains_key("orchestrator"));
assert_eq!(plain["client_msg_id"], MSG_ID);
let made = body_of(&[]).unwrap();
assert!(is_uuid_v4(made["client_msg_id"].as_str().unwrap()));
for bad in [
"8A6F0C3E-2B1D-4E5F-9A7B-3C4D5E6F7A8B",
"8a6f0c3e-2b1d-1e5f-9a7b-3c4d5e6f7a8b",
"8a6f0c3e-2b1d-4e5f-7a7b-3c4d5e6f7a8b",
"8a6f0c3e2b1d4e5f9a7b3c4d5e6f7a8b",
"01JT0000000000000000000000",
"",
] {
let refused = body_of(&[CLIENT_MSG_ID_FLAG, bad]).unwrap_err();
assert_eq!(refused.code, Code::InvalidArgument, "{bad:?}");
}
assert!(body_of(&[ORCHESTRATOR_FLAG, "yes"]).is_err());
let dm_args: Vec<String> = ["--json", "--member", CHANNEL, ORCHESTRATOR_FLAG, "false"]
.iter()
.map(|a| (*a).to_owned())
.collect();
let call = chat_part::call_of("chat.dm", &dm_args).unwrap();
assert!(body_with_id(&call, &dm_args, false).is_err());
}
#[test]
fn chat_sign_matches_the_shared_p37_vectors() {
use ed25519_dalek::Verifier as _;
let key = key();
assert_eq!(check(&key).key.key_id, "b87ae058e7c0c071");
let verify = |signed: &Map<String, Value>, input: &str, signature: &str| {
let bytes = lean_ctx_cloud::consent::person_sign::signing_input(
PERSON_PUBLISH_DOMAIN,
&Value::Object(signed.clone()),
INSTANCE,
)
.unwrap();
assert_eq!(String::from_utf8(bytes.clone()).unwrap(), input);
assert_eq!(sign(&key, signed), signature, "deterministic Ed25519");
let sig =
ed25519_dalek::Signature::from_slice(&B64URL.decode(signature).unwrap()).unwrap();
key.verifying_key().verify(&bytes, &sig).unwrap();
};
let args: Vec<String> = [
"--json",
"--member",
"0c1d2e3f-4a5b-4c6d-8e7f-9a0b1c2d3e4f",
CLIENT_MSG_ID_FLAG,
"0b3f5c4e-8d7a-4c21-b5e9-2a6d1f0c9e88",
]
.iter()
.map(|a| (*a).to_owned())
.collect();
let call = chat_part::call_of("chat.dm", &args).unwrap();
let body = body_with_id(&call, &args, false).unwrap();
verify(
&rest_signed_object_dm(&body),
"leanctx.cloud.person_publish.v1\n{\"client_msg_id\":\"0b3f5c4e-8d7a-4c21-b5e9-2a6d1f0c9e88\",\"instance_id\":\"3f1c2b6a-8d4e-4a7b-9c10-5e2f6a7b8c09\",\"members\":[\"0c1d2e3f-4a5b-4c6d-8e7f-9a0b1c2d3e4f\"],\"t\":\"chat.dm\"}",
"boynUSaEEuL59eNmYwxMPoOUTu3Y-KK9UOgPZ4xshFEa9zLYtOdt_6X_EzmWpmrEMbDEODszAyJIqsIykxcVCQ",
);
use lean_ctx::core::a2a::message::PrivacyLevel;
use lean_ctx::core::agents::ScratchpadEntry;
let entry = ScratchpadEntry {
id: "e1".into(),
from_agent: super::super::bus::PERSON.to_owned(),
to_agent: Some("#team/main".into()),
task_id: None,
category: "progress".into(),
priority: Default::default(),
privacy: PrivacyLevel::Team,
message: "deploy done".into(),
metadata: std::collections::HashMap::new(),
project_root: None,
timestamp: lean_ctx_time::UtcDateTime::now(),
read_by: Vec::new(),
expires_at: None,
};
let mut frame = lean_ctx_cloud::bus::relay_v2::person_frame(&entry, "#team/main").unwrap();
let field = raw_text_field("progress", "deploy done").unwrap();
with_raw_text(&mut frame, field, "deploy done").unwrap();
let ulid: Vec<String> = vec![
CLIENT_MSG_ID_FLAG.into(),
"01JT0000000000000000000000".into(),
];
with_app_fields(&mut frame, &ulid).unwrap();
verify(
&frame,
"leanctx.cloud.person_publish.v1\n{\"body\":{\"step\":\"deploy done\"},\"client_msg_id\":\"01JT0000000000000000000000\",\"instance_id\":\"3f1c2b6a-8d4e-4a7b-9c10-5e2f6a7b8c09\",\"schema\":\"leanctx.cloud.bus/v2\",\"sender\":{\"kind\":\"person\"},\"t\":\"place.publish\",\"to\":\"#team/main\",\"type\":\"progress\"}",
"AqzpM-19emMnPpA2N6aZUL53U6T032zglNA37LKuNQDJsGr2_ymgO4JyJpR82TiwCNzUuLRrXQYtiY67DsDMDg",
);
}
#[test]
fn chat_sign_bus_text_is_raw_and_never_cut() {
use lean_ctx::core::a2a::message::PrivacyLevel;
use lean_ctx::core::agents::ScratchpadEntry;
let long = format!(" {} ", "ä".repeat(lean_ctx_cloud::bus::v2::TEXT_MAX - 4));
let entry = ScratchpadEntry {
id: "e1".into(),
from_agent: super::super::bus::PERSON.to_owned(),
to_agent: Some("#acme/payments".into()),
task_id: None,
category: "question".into(),
priority: Default::default(),
privacy: PrivacyLevel::Team,
message: long.clone(),
metadata: std::collections::HashMap::new(),
project_root: None,
timestamp: lean_ctx_time::UtcDateTime::now(),
read_by: Vec::new(),
expires_at: None,
};
let mut frame =
lean_ctx_cloud::bus::relay_v2::person_frame(&entry, "#acme/payments").unwrap();
let field = raw_text_field("question", &long).unwrap();
with_raw_text(&mut frame, field, &long).unwrap();
assert_eq!(frame["body"], json!({ "text": long }));
let too_long = "x".repeat(lean_ctx_cloud::bus::v2::TEXT_MAX + 1);
assert!(
raw_text_field("question", &too_long).is_err(),
"refused, not cut"
);
assert_eq!(
raw_text_field("progress", &"x".repeat(STEP_MAX)).unwrap(),
"step"
);
assert!(raw_text_field("progress", &"x".repeat(STEP_MAX + 1)).is_err());
for category in ["", "direct", "answer", "finding"] {
assert!(raw_text_field(category, "hi").is_err(), "{category:?}");
}
}
#[test]
fn chat_sign_takes_a_dm_on_chat_post() {
let key = key();
let check = check(&key);
let now = Instant::now();
let mut store = Store::new();
let frame = dm_frame();
let signed = rest_signed_object_dm(&frame);
let send = Send::Rest {
path: "/chat/dms".into(),
};
let b64 = store
.insert(Kind::Dm, frame.clone(), signed.clone(), send.clone(), now)
.unwrap();
let sig = sign(&key, &signed);
let ready = store
.take_any(
Kind::sent_by("chat.post"),
&b64,
&sig,
&check.key.key_id,
Some(&check),
now,
)
.unwrap();
assert_eq!(ready.send, send, "the stored frame decides the route");
let post = post_frame();
let post_signed = rest_signed_object_post("ch_1", &post);
let b64 = store
.insert(Kind::Post, post, post_signed.clone(), send, now)
.unwrap();
let refused = store
.take_any(
Kind::sent_by("chat.dm"),
&b64,
&sign(&key, &post_signed),
&check.key.key_id,
Some(&check),
now,
)
.unwrap_err();
assert_eq!(refused.code, Code::FrameUnknown);
assert!(Kind::sent_by("chat.read").is_empty());
}
#[test]
fn chat_sign_a_sent_frame_stays_used_until_it_expires() {
let key = key();
let check = check(&key);
let now = Instant::now();
let mut store = Store::new();
let frame = post_frame();
let signed = rest_signed_object_post("ch_1", &frame);
let send = Send::Rest { path: "/x".into() };
let insert = |store: &mut Store, at| {
store.insert(Kind::Post, frame.clone(), signed.clone(), send.clone(), at)
};
let b64 = insert(&mut store, now).unwrap();
assert_eq!(insert(&mut store, now).unwrap(), b64, "unsent: renewed");
let sig = sign(&key, &signed);
store
.take(Kind::Post, &b64, &sig, &check.key.key_id, Some(&check), now)
.unwrap();
assert_eq!(
insert(&mut store, now).unwrap_err().code,
Code::FrameUnknown
);
assert_eq!(
store
.take(Kind::Post, &b64, &sig, &check.key.key_id, Some(&check), now)
.unwrap_err()
.code,
Code::FrameUnknown
);
assert!(insert(&mut store, now + TTL).is_ok());
}
#[test]
fn chat_sign_releases_a_frame_whose_send_never_reached_the_cloud() {
let key = key();
let check = check(&key);
let now = Instant::now();
let frame = post_frame();
let signed = signed_of(Kind::Post, &frame);
let signature = sign(&key, &signed);
let send = || Send::Rest {
path: "/v1/chat/channels/c/messages".into(),
};
let take = |store: &mut Store, b64: &str| {
store.take(
Kind::Post,
b64,
&signature,
&check.key.key_id,
Some(&check),
now,
)
};
let not_reached = [
Err(Failure::new(Code::Unavailable, "connection refused")),
Err(Failure::new(Code::Internal, "timed out")),
Ok((502, json!({}))),
Ok((503, json!({}))),
];
for outcome in ¬_reached {
assert!(!reached_the_cloud(outcome), "{outcome:?}");
let mut store = Store::new();
let b64 = store
.insert(Kind::Post, frame.clone(), signed.clone(), send(), now)
.unwrap();
assert!(take(&mut store, &b64).is_ok());
store.release(&b64, now);
let again = take(&mut store, &b64).expect("released frame is sendable again");
assert_eq!(again.frame, frame);
}
let reached = [
Ok((200, json!({}))),
Ok((201, json!({}))),
Ok((400, json!({}))),
Ok((409, json!({}))),
Err(Failure::new(Code::Forbidden, chat_part::RECONNECT)),
];
for outcome in &reached {
assert!(reached_the_cloud(outcome), "{outcome:?}");
let mut store = Store::new();
let b64 = store
.insert(Kind::Post, frame.clone(), signed.clone(), send(), now)
.unwrap();
assert!(take(&mut store, &b64).is_ok());
let refused = take(&mut store, &b64).unwrap_err();
assert_eq!(refused.code, Code::FrameUnknown);
assert_eq!(refused.message, ALREADY_SENT);
let again = store
.insert(Kind::Post, frame.clone(), signed.clone(), send(), now)
.unwrap_err();
assert_eq!(again.message, ALREADY_SENT);
}
let mut store = Store::new();
let b64 = store
.insert(Kind::Post, frame.clone(), signed.clone(), send(), now)
.unwrap();
assert!(take(&mut store, &b64).is_ok());
store.release(&b64, now + TTL);
assert_eq!(take(&mut store, &b64).unwrap_err().code, Code::FrameUnknown);
}
#[test]
fn chat_sign_store_keeps_at_most_64_frames() {
let mut store = Store::new();
let now = Instant::now();
let mut first = None;
for i in 0..=MAX_PREPARED {
let mut frame = post_frame();
frame.insert("client_msg_id".into(), format!("id-{i}").into());
let signed = frame.clone();
let b64 = store
.insert(
Kind::Post,
frame,
signed,
Send::Rest { path: "/x".into() },
now,
)
.unwrap();
first.get_or_insert(b64);
}
assert_eq!(store.frames.len(), MAX_PREPARED);
assert!(
store
.frames
.iter()
.all(|p| Some(&p.frame_b64) != first.as_ref())
);
}
#[test]
fn chat_sign_refusal_names_the_app_or_the_terminal() {
let terminal = signature_required();
assert_eq!(terminal.code, Code::SignatureRequired);
assert_eq!(terminal.message, FROM_TERMINAL);
let app = with_app(true, signature_required);
assert_eq!(app.message, FROM_OLD_APP);
assert_eq!(signature_required().message, FROM_TERMINAL);
assert_eq!(Code::SignatureRequired.as_str(), "signature_required");
}
#[test]
fn chat_sign_maps_the_cloud_replay_guard() {
let original = json!({"id": "m1", "client_msg_id": "6f1c0a52-3c1e-4f43-9a52-0f5d2f8e1b77"});
assert_eq!(signed_outcome(200, original.clone()).unwrap(), original);
let reused = json!({"error": {"code": "invalid_state", "reason": "client_msg_id_reused"}});
assert_eq!(
signed_outcome(409, reused).unwrap_err().code,
Code::FrameUnknown
);
let bad_id = json!({"error": {"code": "invalid_field", "field": "client_msg_id"}});
assert_eq!(
signed_outcome(400, bad_id).unwrap_err().code,
Code::Internal
);
let unproven = json!({"error": {"code": "forbidden_right", "reason": "person_unproven"}});
let refused = signed_outcome(403, unproven).unwrap_err();
assert_eq!(refused.code, Code::SignatureRequired);
assert_eq!(refused.message, FROM_TERMINAL);
let app = with_app(true, || {
chat_part::outcome(
403,
json!({"error": {"code": "forbidden_right", "reason": "person_unproven"}}),
)
});
assert_eq!(app.unwrap_err().message, FROM_OLD_APP);
}
#[test]
fn chat_sign_bus_args_drop_the_kind() {
let args: Vec<String> = ["prepare", "--json", "--kind", "bus", "--to", "#t/v"]
.into_iter()
.map(str::to_owned)
.collect();
assert_eq!(bus_args(&args), vec!["--json", "--to", "#t/v"]);
}
}