use base64::Engine as _;
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64URL;
use ed25519_dalek::Signer as _;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use super::super::CloudError;
use super::super::instance_key::InstanceKeys;
use super::super::transport::CloudClient;
use super::card::WorkerCard;
use super::{WorkerAliasKey, alias_signing_key, check_name, key_id};
pub const WORKER_DOMAIN: &str = "leanctx.cloud.worker.v1\n";
pub const INSTANCE_CARD_DOMAIN: &str = "leanctx.cloud.instance_card.v1\n";
pub struct Registration<'a> {
pub instance_id: &'a str,
pub host_app: &'a str,
pub role: &'a str,
pub name: &'a str,
pub card: &'a WorkerCard,
pub alias_place: Option<&'a str>,
}
fn sign(keys: &InstanceKeys, domain: &str, mut body: Value) -> Result<Value, CloudError> {
let signer = keys.signing_key();
body["key_id"] = key_id(signer).into();
let input = lean_ctx_protocol::cloud::signing_input(domain, &body)
.map_err(|e| CloudError::Local(e.to_string()))?;
body["signature"] = hex::encode(signer.sign(&input).to_bytes()).into();
Ok(body)
}
pub fn register_body(keys: &InstanceKeys, reg: &Registration<'_>) -> Result<Value, CloudError> {
let name = check_name(reg.name)
.map_err(|refusal| CloudError::Config(format!("invalid_name: {}", refusal.message())))?;
let card = serde_json::to_value(reg.card).map_err(|e| CloudError::Local(e.to_string()))?;
let mut body = json!({
"host_app": reg.host_app,
"role": reg.role,
"name": name,
"card": card,
});
match reg.alias_place {
None => {
body["worker_id"] =
super::workspace_worker_id(reg.instance_id, reg.host_app, reg.role).into();
}
Some(place) => {
let alias = WorkerAliasKey::of(keys);
body["worker_id"] = alias.worker_id(reg.host_app, reg.role, place).into();
body["place"] = place.into();
body["alias_pubkey"] = B64URL
.encode(alias_signing_key(keys, place).verifying_key().as_bytes())
.into();
body["hlc_node"] = alias.hlc_node(place).into();
}
}
sign(keys, WORKER_DOMAIN, body)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConsentChannel {
OsPresence,
TtyOnly,
}
pub fn instance_card_body(
keys: &InstanceKeys,
instance_version: &str,
consent_channel: ConsentChannel,
clear_presence_key: bool,
) -> Result<Value, CloudError> {
let mut body = json!({
"instance_version": instance_version,
"consent_channel": consent_channel,
});
if clear_presence_key {
body["presence_key"] = Value::Null;
}
sign(keys, INSTANCE_CARD_DOMAIN, body)
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct RegisteredWorker {
pub id: String,
pub name: String,
pub place: Option<String>,
pub status: String,
pub registered_at: u64,
}
#[derive(Deserialize)]
struct WorkerEnvelope {
worker: RegisteredWorker,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct PresenceKey {
pub alg: String,
pub key_id: String,
pub hardware: bool,
pub bound_at: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Account {
pub user_id: String,
#[serde(default)]
pub handle: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct CardState {
pub instance_version: String,
pub consent_channel: ConsentChannel,
pub updated_at: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct InstanceCard {
pub card: CardState,
pub account: Account,
pub presence_key: Option<PresenceKey>,
}
const MAX_ANSWER: usize = 64 * 1024;
fn worded(code: &str, reason: &str) -> Option<String> {
Some(match (code, reason) {
(_, "confusable") => "This name mixes alphabets. Use one alphabet.".into(),
(_, "reserved") => {
"Names may not contain cloud, thinkery, leanctx or moderator. Pick another name.".into()
}
("worker_not_owned", _) => {
"worker_not_owned: LeanCTX Cloud has bound this worker id to another instance.".into()
}
(_, "worker_cap_reached") => {
"worker_cap_reached: this instance already has 32 workers on LeanCTX Cloud.".into()
}
(_, "presence_key_locked") => "The presence key can only be cleared here; bind a new one with `lean-ctx cloud connect --bind-presence-key`.".into(),
_ => return None,
})
}
fn call<T: for<'de> Deserialize<'de>>(
client: &CloudClient,
method: &str,
path: &str,
body: &Value,
what: &str,
) -> Result<T, CloudError> {
let (status, answer) = client.send_answer(method, path, Some(body), MAX_ANSWER)?;
if !(200..300).contains(&status) {
let code = answer["error"]["code"].as_str().unwrap_or_default();
let reason = answer["error"]["reason"].as_str().unwrap_or_default();
return Err(worded(code, reason).map_or_else(
|| CloudError::from_response(status, &answer),
CloudError::Config,
));
}
parse(answer, what)
}
fn parse<T: for<'de> Deserialize<'de>>(value: Value, what: &str) -> Result<T, CloudError> {
serde_json::from_value(value).map_err(|e| CloudError::Protocol(format!("{what}: {e}")))
}
pub fn register(
client: &CloudClient,
keys: &InstanceKeys,
reg: &Registration<'_>,
) -> Result<RegisteredWorker, CloudError> {
let body = register_body(keys, reg)?;
Ok(call::<WorkerEnvelope>(client, "POST", "/workers", &body, "worker registration")?.worker)
}
pub fn announce_instance(session: &crate::auth::Session) -> Result<InstanceCard, CloudError> {
let keys = InstanceKeys::load(&session.store)?;
let channel = if crate::consent::broker::bound_presence_key(&session.store).is_some() {
ConsentChannel::OsPresence
} else {
ConsentChannel::TtyOnly
};
let minor = std::cell::Cell::new(None);
let exchanged = session.with_client(|client| {
let (status, answer) =
card_exchange(client, &keys, &session.instance.instance_id, channel, false)?;
minor.set(client.answered_minor());
if status == 401 {
return Err(CloudError::from_response(status, &answer));
}
Ok((status, answer))
});
let speaks = match &exchanged {
Ok((status, _)) if (200..300).contains(status) => Some(true),
Ok((404, _)) | Err(CloudError::NotFound) => Some(false),
_ => None,
};
if let Some(v2) = speaks {
crate::bus::relay_v2::record_protocol_minor(&session.store, v2, minor.get())?;
}
let _ = session.with_client(|client| {
crate::session_ref::refresh(&session.store, &session.instance.url, client)
});
let result = exchanged.and_then(|(status, answer)| card_from(status, answer));
if let Ok(card) = &result {
session
.store
.write_config(crate::consent::broker::ACCOUNT_FILE, &card.account)?;
}
result
}
pub fn retire(client: &CloudClient, worker_id: &str) -> Result<(), CloudError> {
if !crate::bus::address::is_worker_id(worker_id) {
return Err(CloudError::Config(format!(
"{worker_id} is not a worker id"
)));
}
let (status, answer) =
client.send_answer("DELETE", &format!("/workers/{worker_id}"), None, MAX_ANSWER)?;
let code = answer["error"]["code"].as_str().unwrap_or_default();
if (200..300).contains(&status)
|| (status == 404 && code == "not_found")
|| code == "worker_not_owned"
{
return Ok(());
}
Err(CloudError::from_response(status, &answer))
}
pub fn retire_evicted(session: &crate::auth::Session) -> Result<(), CloudError> {
let instance_id = &session.instance.instance_id;
let mut first_error = None;
for worker_id in super::attach::retired(&session.store, instance_id)? {
match session.with_client(|client| retire(client, &worker_id)) {
Ok(()) => super::attach::retirement_done(&session.store, instance_id, &worker_id)?,
Err(error) => {
first_error.get_or_insert(error);
}
}
}
first_error.map_or(Ok(()), Err)
}
pub const IDLE_RETIRE: std::time::Duration = std::time::Duration::from_hours(24);
pub const SESSION_IDLE_RETIRE: std::time::Duration = std::time::Duration::from_secs(3600);
const SESSION_END_GRACE: std::time::Duration = std::time::Duration::from_secs(600);
pub fn sweep(session: &crate::auth::Session) -> Result<Vec<String>, CloudError> {
let instance_id = &session.instance.instance_id;
let mut gone = super::attach::give_up_idle(&session.store, instance_id, IDLE_RETIRE, false)?;
gone.extend(super::attach::give_up_idle(
&session.store,
instance_id,
SESSION_IDLE_RETIRE,
true,
)?);
retire_evicted(session)?;
Ok(gone)
}
pub fn on_session_end(store: &crate::state::CloudStore) {
let Ok(Some(instance)) = store.instance() else {
return;
};
match super::attach::give_up_idle(store, &instance.instance_id, SESSION_END_GRACE, true) {
Ok(gone) if !gone.is_empty() => {
tracing::debug!("cloud: workers of ended sessions given up: {gone:?}");
}
Ok(_) => {}
Err(error) => tracing::debug!("cloud: workers of ended sessions not given up: {error}"),
}
}
pub fn ensure_registered(
session: &crate::auth::Session,
attached: &super::attach::LocalWorker,
card: &WorkerCard,
) -> Result<super::attach::LocalWorker, CloudError> {
if attached.registered_at.is_some() {
return Ok(attached.clone());
}
if let Err(error) = retire_evicted(session) {
session
.store
.record_error(&format!("worker retirement: {error}"));
}
let keys = InstanceKeys::load(&session.store)?;
let instance = &session.instance;
let name = attached.name.clone().unwrap_or_else(|| {
super::default_name(
&instance.label,
&super::attach::host_display(&attached.host_app),
&attached.role,
)
});
let reg = Registration {
instance_id: &instance.instance_id,
host_app: &attached.host_app,
role: &attached.role,
name: &name,
card,
alias_place: None,
};
let worker = session.with_client(|client| register(client, &keys, ®))?;
if worker.id != attached.worker_id {
return Err(CloudError::Protocol(format!(
"LeanCTX Cloud registered {} for {}",
worker.id, attached.worker_id
)));
}
super::attach::update_book(&session.store, &instance.instance_id, |workers| {
let entry = workers.iter_mut().find(|w| w.worker_id == worker.id)?;
entry.registered_at = Some(worker.registered_at);
entry.name = Some(worker.name.clone());
Some(entry.clone())
})?
.ok_or_else(|| CloudError::Local(format!("{} left the worker book", worker.id)))
}
pub fn patch(
client: &CloudClient,
worker_id: &str,
name: Option<&str>,
card: Option<&WorkerCard>,
) -> Result<RegisteredWorker, CloudError> {
if !crate::bus::address::is_worker_id(worker_id) {
return Err(CloudError::Config(format!(
"{worker_id} is not a worker id"
)));
}
let mut body = json!({});
if let Some(name) = name {
body["name"] = check_name(name)
.map_err(|r| CloudError::Config(format!("invalid_name: {}", r.message())))?
.into();
}
if let Some(card) = card {
body["card"] = serde_json::to_value(card).map_err(|e| CloudError::Local(e.to_string()))?;
}
if body.as_object().is_none_or(serde_json::Map::is_empty) {
return Err(CloudError::Config("nothing to change".into()));
}
let path = format!("/workers/{worker_id}");
Ok(call::<WorkerEnvelope>(client, "PATCH", &path, &body, "worker update")?.worker)
}
pub fn send_instance_card(
client: &CloudClient,
keys: &InstanceKeys,
instance_id: &str,
consent_channel: ConsentChannel,
clear_presence_key: bool,
) -> Result<InstanceCard, CloudError> {
let (status, answer) = card_exchange(
client,
keys,
instance_id,
consent_channel,
clear_presence_key,
)?;
card_from(status, answer)
}
fn card_exchange(
client: &CloudClient,
keys: &InstanceKeys,
instance_id: &str,
consent_channel: ConsentChannel,
clear_presence_key: bool,
) -> Result<(u16, Value), CloudError> {
let body = instance_card_body(
keys,
env!("CARGO_PKG_VERSION"),
consent_channel,
clear_presence_key,
)?;
let path = format!("/instances/{instance_id}/card");
client.send_answer("PATCH", &path, Some(&body), MAX_ANSWER)
}
fn card_from(status: u16, answer: Value) -> Result<InstanceCard, CloudError> {
if !(200..300).contains(&status) {
let code = answer["error"]["code"].as_str().unwrap_or_default();
let reason = answer["error"]["reason"].as_str().unwrap_or_default();
return Err(worded(code, reason).map_or_else(
|| CloudError::from_response(status, &answer),
CloudError::Config,
));
}
parse(answer, "instance card")
}
#[cfg(test)]
mod tests {
use super::super::card::{Availability, Budget};
use super::*;
const FIXTURES: &str = concat!(
concat!(env!("CARGO_MANIFEST_DIR"), "/../.."),
"/crates/lean-ctx-protocol/tests/fixtures/cloud-v2/"
);
fn fixture(name: &str) -> Value {
let text = std::fs::read_to_string(format!("{FIXTURES}{name}")).unwrap();
serde_json::from_str(&text).unwrap()
}
fn instance_a() -> InstanceKeys {
InstanceKeys::from_seeds([0x49; 32], [0x01; 32])
}
#[test]
fn workspace_registration_matches_the_fixture_byte_for_byte() {
let card = WorkerCard::new(
"claude-code",
Some("claude-opus-5-5"),
["ctx_read", "ctx_search", "ctx_shell"].map(String::from),
Budget {
tasks_per_day: Some(20),
usd_cents_per_day: None,
},
Availability::Online,
);
let body = register_body(
&instance_a(),
&Registration {
instance_id: "6f2b9c4e-1a3d-4e5f-8a7b-0c1d2e3f4a5b",
host_app: "claude-code",
role: "backend",
name: "yves-mac · Claude Code · backend",
card: &card,
alias_place: None,
},
)
.unwrap();
assert_eq!(body, fixture("routes/worker_register.request.json"));
}
#[test]
fn alias_registration_matches_the_fixture_byte_for_byte() {
let card = WorkerCard::new(
"codex",
None,
Vec::new(),
Budget::default(),
Availability::Idle,
);
let body = register_body(
&instance_a(),
&Registration {
instance_id: "6f2b9c4e-1a3d-4e5f-8a7b-0c1d2e3f4a5b",
host_app: "codex",
role: "general",
name: "Codex · general",
card: &card,
alias_place: Some("vault:3f1c2b6a-8d4e-4a7b-9c10-5e2f6a7b8c01"),
},
)
.unwrap();
let want = fixture("routes/worker_register.alias.request.json");
assert_eq!(body, want);
assert!(!body.to_string().contains("6f2b9c4e"));
}
#[test]
fn instance_cards_match_the_fixtures_and_can_only_clear() {
let keys = instance_a();
assert_eq!(
instance_card_body(&keys, "3.12.0", ConsentChannel::OsPresence, false).unwrap(),
fixture("routes/instance_card.request.json")
);
assert_eq!(
instance_card_body(&keys, "3.12.0", ConsentChannel::TtyOnly, true).unwrap(),
fixture("routes/instance_card.clear.request.json")
);
let card: InstanceCard =
parse(fixture("routes/instance_card.response.json"), "card").unwrap();
assert_eq!(card.presence_key.unwrap().key_id, "ef1bd9c792ccc768");
assert_eq!(card.account.handle.as_deref(), Some("yves"));
let mut nameless = fixture("routes/instance_card.response.json");
nameless["account"]["handle"] = Value::Null;
let card = card_from(200, nameless).unwrap();
assert_eq!(card.account.handle, None);
assert!(card_from(404, json!({"error": {"code": "not_found"}})).is_err());
}
#[test]
fn a_mixed_alphabet_name_is_refused_before_it_leaves() {
let card = WorkerCard::new(
"codex",
None,
Vec::new(),
Budget::default(),
Availability::Idle,
);
let err = register_body(
&instance_a(),
&Registration {
instance_id: "6f2b9c4e-1a3d-4e5f-8a7b-0c1d2e3f4a5b",
host_app: "codex",
role: "general",
name: "аdmin",
card: &card,
alias_place: None,
},
)
.unwrap_err();
assert!(err.to_string().contains("invalid_name"), "{err}");
}
#[test]
fn refusals_reach_the_person_in_their_words() {
let locked = fixture("routes/instance_card.error.presence_key_locked.json");
let text = worded(
locked["error"]["code"].as_str().unwrap(),
locked["error"]["reason"].as_str().unwrap(),
)
.unwrap();
assert!(text.contains("--bind-presence-key"), "{text}");
assert_eq!(
worded("invalid_request", "confusable").unwrap(),
"This name mixes alphabets. Use one alphabet."
);
assert!(
worded("worker_not_owned", "")
.unwrap()
.starts_with("worker_not_owned")
);
assert_eq!(worded("invalid_request", "other"), None);
}
#[test]
fn responses_parse() {
let w: WorkerEnvelope =
parse(fixture("routes/worker_register.response.json"), "w").unwrap();
assert_eq!(w.worker.id, "wkr_vk4oelpv5lzjmev44hun");
let p: WorkerEnvelope = parse(fixture("routes/worker_patch.response.json"), "p").unwrap();
assert!(p.worker.name.contains("payments"));
let r: WorkerEnvelope = parse(
fixture("routes/worker_register.reactivated.response.json"),
"r",
)
.unwrap();
assert!(r.worker.id.starts_with("wkr_"));
}
fn retiring_cloud(
status: u16,
body: String,
) -> (String, std::sync::Arc<std::sync::Mutex<Vec<String>>>) {
use std::io::{BufRead as _, BufReader, Write as _};
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let origin = format!("http://127.0.0.1:{}", listener.local_addr().unwrap().port());
let seen = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let recorded = std::sync::Arc::clone(&seen);
std::thread::spawn(move || {
for stream in listener.incoming().flatten() {
let mut writer = stream.try_clone().unwrap();
let mut reader = BufReader::new(stream);
let mut line = String::new();
if reader.read_line(&mut line).unwrap_or(0) == 0 {
continue;
}
loop {
let mut header = String::new();
reader.read_line(&mut header).unwrap();
if header.trim_end().is_empty() {
break;
}
}
recorded.lock().unwrap().push(line.trim_end().to_string());
let _ = write!(
writer,
"HTTP/1.1 {status} X\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
body.len()
);
}
});
(origin, seen)
}
#[test]
fn retiring_a_worker_is_done_unless_cloud_could_not_answer() {
let route = fixture("routes/worker_retire.request.json");
let id = route["path"]
.as_str()
.unwrap()
.strip_prefix("/v1/workers/")
.unwrap()
.to_string();
assert_eq!(route["method"], "DELETE");
assert_eq!(route["response"]["status"], 204);
for (status, body) in [
(204, String::new()),
(
404,
fixture("routes/worker_retire.error.not_found.json").to_string(),
),
(
409,
fixture("routes/worker_retire.error.not_owned.json").to_string(),
),
] {
let (origin, seen) = retiring_cloud(status, body);
let client = CloudClient::new(&origin).unwrap();
retire(&client, &id).unwrap_or_else(|e| panic!("{status}: {e}"));
assert_eq!(
seen.lock().unwrap().as_slice(),
[format!("DELETE /v1/workers/{id} HTTP/1.1")]
);
}
let (origin, _) = retiring_cloud(503, r#"{"error":{"code":"unavailable"}}"#.into());
assert!(retire(&CloudClient::new(&origin).unwrap(), &id).is_err());
let (origin, _) = retiring_cloud(404, String::new());
assert!(retire(&CloudClient::new(&origin).unwrap(), &id).is_err());
assert!(retire(&CloudClient::new(&origin).unwrap(), "../admin").is_err());
}
}