use std::fmt;
use lean_ctx_protocol::cloud::lease::CloudPlan;
use lean_ctx_protocol::cloud::{
CloudErrorBody, CloudErrorCode, CloudErrorDetailV1, CloudUploadKind,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CloudError {
Offline(String),
NotConnected,
Unauthorized,
PlanRequired(Box<PlanNeed>),
InstanceLimit(Box<InstanceCap>),
QuotaExceeded(Box<Quota>),
StorageFull(Box<StorageFull>),
UploadRateLimited {
retry_after: Option<u64>,
per_hour: Option<u64>,
},
PolicyDenied(PolicyDenial),
LeaseLapsed,
Revoked(Revocation),
Forbidden,
Vetoed,
UploadDisabled(CloudUploadKind),
FeatureDisabled {
feature: Option<String>,
},
NotFound,
Conflict {
expected: Option<u64>,
actual: Option<u64>,
},
ScreeningBlocked,
UpgradeRequired {
min: Option<String>,
},
RateLimited {
retry_after: Option<u64>,
},
DeviceFlow(String),
TrialUnsupported {
origin: String,
},
Protocol(String),
Config(String),
Local(String),
Refused(String),
Task {
refusal: TaskRefusal,
number: Option<u64>,
detail: Option<String>,
},
Server {
status: u16,
code: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Quota {
pub status: u16,
pub resource: String,
pub limit: Option<u64>,
pub used: Option<u64>,
pub reset_at: Option<String>,
pub retry_after: Option<u64>,
pub cooldown_days: Option<u32>,
pub next_step: Option<NextStep>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct InstanceCap {
pub resource: Option<String>,
pub limit: Option<u64>,
pub used: Option<u64>,
pub seats: Option<u64>,
pub per_seat: Option<u64>,
pub next_step: Option<NextStep>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct StorageFull {
pub limit: Option<u64>,
pub used: Option<u64>,
pub next_step: Option<NextStep>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NextStep {
Subscribe,
TopUp,
UpgradeTeam,
AddSeats,
Contact,
Wait,
}
impl NextStep {
#[must_use]
pub fn of_wire(value: Option<&str>) -> Option<Self> {
Some(match value? {
"subscribe" => Self::Subscribe,
"top_up" => Self::TopUp,
"upgrade_team" => Self::UpgradeTeam,
"add_seats" => Self::AddSeats,
"contact" => Self::Contact,
"wait" => Self::Wait,
_ => return None,
})
}
#[must_use]
pub fn hint(self) -> Option<&'static str> {
Some(match self {
Self::Subscribe => "choose a plan in the Cloud",
Self::TopUp => "a top-up in the Cloud adds more",
Self::UpgradeTeam => "Team gives every seat its own allowance",
Self::AddSeats => "every seat you add in the Cloud brings its own allowance",
Self::Contact => "contact us to extend your Enterprise contract",
Self::Wait => return None,
})
}
}
fn write_hint(f: &mut fmt::Formatter<'_>, next: Option<NextStep>) -> fmt::Result {
match next.and_then(NextStep::hint) {
Some(hint) => write!(f, "; {hint}"),
None => Ok(()),
}
}
fn bytes_text(bytes: u64) -> String {
const UNITS: [(&str, u64); 4] = [
("TiB", 1 << 40),
("GiB", 1 << 30),
("MiB", 1 << 20),
("KiB", 1 << 10),
];
for (unit, size) in UNITS {
if bytes >= size {
let tenths = u128::from(bytes) * 10 / u128::from(size);
return if tenths % 10 == 0 {
format!("{} {unit}", tenths / 10)
} else {
format!("{}.{} {unit}", tenths / 10, tenths % 10)
};
}
}
format!("{bytes} B")
}
#[must_use]
pub fn amount_text(resource: &str, value: u64) -> String {
if resource.ends_with("_bytes") {
bytes_text(value)
} else if resource.ends_with("_usd_micros") {
format!("${}.{:02}", value / 1_000_000, value % 1_000_000 / 10_000)
} else {
value.to_string()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct PlanNeed {
pub feature: Option<String>,
pub plan_needed: Option<String>,
pub limit: Option<u64>,
pub used: Option<u64>,
pub message: Option<String>,
pub plan: Option<CloudPlan>,
pub reason: Option<String>,
pub next_step: Option<NextStep>,
pub plans_url: Option<String>,
pub trial_reason: Option<String>,
}
pub const LOCKED_MARK: &str = "this workspace is locked (read and export only)";
pub const PLAN_LIMITS: &str = "Personal up to 3 hosts registered, up to 2 connected at once; Team and Enterprise up to 3 hosts registered, up to 2 connected at once per seat";
pub const HOSTS_REGISTERED: &str = "hosts registered";
pub const HOSTS_CONNECTED: &str = "hosts connected at once";
pub const FREE_REGISTERED: &str = "to free one, revoke a host you no longer use on the Hosts page";
pub const FREE_CONNECTED: &str =
"to free one, disconnect a host (`lean-ctx cloud disconnect` on that machine)";
impl PlanNeed {
#[must_use]
pub fn locked(&self) -> Option<&str> {
self.reason
.as_deref()
.filter(|r| matches!(*r, "trial_ended" | "plan_ended"))
}
#[must_use]
pub fn trial_used(&self) -> bool {
self.reason.as_deref() == Some(TRIAL_USED)
}
fn plan_word(&self) -> &'static str {
let team = self.plan_needed.as_deref() == Some("team")
|| matches!(self.plan, Some(CloudPlan::Team | CloudPlan::Enterprise));
if team { "Team" } else { "Personal" }
}
}
#[must_use]
pub fn locked_line(reason: &str, plan_word: &str) -> String {
let what = if reason == "trial_ended" {
format!("Your {plan_word} trial has ended")
} else {
"Your plan has ended".to_string()
};
format!("{what}: {LOCKED_MARK}. Choose a plan in the Cloud to sync again.")
}
fn plans_from(needed: &str) -> Option<&'static str> {
Some(match needed {
"personal" => "Personal, Team and Enterprise",
"team" => "Team and Enterprise",
"enterprise" => "Enterprise",
_ => return None,
})
}
fn write_plan_need(f: &mut fmt::Formatter<'_>, need: &PlanNeed) -> fmt::Result {
if need.trial_used() {
let plan = need.plan_word();
match need.trial_reason.as_deref() {
Some("trial_ended") => write!(
f,
"the {plan} trial of this address has ended; no second trial starts. Choose a plan"
)?,
Some("no_plan") => write!(
f,
"this address has no {plan} trial left and no plan; no second trial starts. Choose a plan"
)?,
_ => write!(
f,
"this address already had its {plan} trial; no second trial starts. Choose a plan"
)?,
}
return match need.plans_url.as_deref().filter(|u| u.starts_with("http")) {
Some(url) => write!(f, " at {url}, then run `lean-ctx cloud connect`"),
None => f.write_str(" in the Cloud, then run `lean-ctx cloud connect`"),
};
}
if let Some(reason) = need.locked() {
return f.write_str(&locked_line(reason, need.plan_word()));
}
let Some(feature) = need.feature.as_deref() else {
return write!(
f,
"your plan does not include the Cloud on this machine ({PLAN_LIMITS})"
);
};
let plans = need.plan_needed.as_deref().and_then(plans_from);
let on = need
.plan
.filter(|p| *p != CloudPlan::Unknown)
.map(|p| match p {
CloudPlan::NoPlan => "; this workspace has no plan".to_string(),
p => format!("; this workspace is on {}", p.display_name()),
})
.unwrap_or_default();
match feature {
"repos" | "vaults" | "private_vaults" => {
f.write_str("this workspace has used all its vaults")?;
if let (Some(used), Some(limit)) = (need.used, need.limit) {
write!(f, " ({used} of {limit})")?;
}
if let Some(plans) = plans {
write!(f, "; more are included in {plans}")?;
}
write!(
f,
"{on}; remove or publish a vault in the Cloud, or upgrade the plan"
)
}
other => {
match (&need.message, plans) {
(Some(message), _) => f.write_str(message.trim_end_matches('.'))?,
(None, Some(plans)) => {
write!(f, "{} is included in {plans}", other.replace('_', " "))?;
}
(None, None) => write!(
f,
"this workspace's plan does not include {}",
other.replace('_', " ")
)?,
}
f.write_str(&on)
}
}
}
pub const MONTHLY_RESOURCES: &[&str] =
&["transfer_bytes", "bus_messages", "model_credit_usd_micros"];
impl Quota {
#[must_use]
pub fn waits(&self) -> bool {
self.status == 429 || MONTHLY_RESOURCES.contains(&self.resource.as_str())
}
#[must_use]
pub fn cooling_slot(&self) -> bool {
self.resource == "registered_instances"
&& self.cooldown_days.is_some_and(|d| d > 0)
&& self.reset_at.is_some()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct PolicyDenial {
pub member: Option<String>,
pub level: Option<String>,
pub action: Option<String>,
}
fn wire_token(value: Option<&str>, max: usize) -> Option<String> {
value
.filter(|s| {
(1..=max).contains(&s.len())
&& s.bytes().all(|b| {
b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'_' | b'.' | b'-')
})
})
.map(str::to_string)
}
pub(crate) fn wire_time(value: Option<&str>) -> Option<String> {
let parsed = lean_ctx_time::OffsetDateTime::parse_from_rfc3339(value?).ok()?;
Some(
parsed
.to_utc()
.to_rfc3339_opts(lean_ctx_time::SecondsFormat::Secs, true),
)
}
pub const TRIAL_USED: &str = "trial_used";
pub const NOT_READY: &str = "not_ready";
pub const VAULT_CREATE_FORBIDDEN: &str = "vault_create_forbidden";
fn web_url(value: Option<&str>) -> Option<String> {
value
.filter(|s| {
(s.starts_with("https://")
|| s.starts_with("http://")
|| (s.starts_with('/') && !s.starts_with("//")))
&& s.len() <= 512
&& !s.chars().any(hidden_in_url)
})
.map(str::to_string)
}
fn hidden_in_url(c: char) -> bool {
use unicode_general_category::{GeneralCategory, get_general_category};
c.is_control()
|| c.is_whitespace()
|| get_general_category(c) == GeneralCategory::Format
|| lean_ctx_protocol::cloud::invisible(c)
}
fn same_origin(url: &str, base: &str) -> bool {
let Some(start) = url.find("://").map(|i| i + 3) else {
return false;
};
let end = url[start..]
.find(['/', '?', '#'])
.map_or(url.len(), |i| start + i);
url[..end].eq_ignore_ascii_case(base)
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Revocation {
pub workspace: Option<String>,
pub reason: Option<String>,
pub revoked_at: Option<u64>,
pub instance_id: Option<String>,
}
impl Revocation {
fn of_statement(statement: &serde_json::Value) -> Self {
let token = |v: &serde_json::Value, max: usize| {
v.as_str()
.filter(|s| {
(1..=max).contains(&s.len())
&& s.bytes().all(|b| {
b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_' || b == b'-'
})
})
.map(str::to_string)
};
Self {
workspace: None,
reason: token(&statement["reason"], 32),
revoked_at: statement["revoked_at"].as_u64(),
instance_id: token(&statement["instance_id"], 36),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PermitCheck {
Presence,
Role,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TaskRefusal {
Claimed,
NotOpen,
StaleClaim,
Closed,
SelfVerify,
NotSubmitted,
NotPermitted(PermitCheck),
OptinScope,
OptinChanged,
OptinInactive,
DailyBudget { retry_after: Option<u64> },
StaleBase,
PersonUnproven,
SignatureExpired,
}
impl TaskRefusal {
fn from_reason(
code: CloudErrorCode,
reason: Option<&str>,
retry_after: Option<u64>,
check: Option<&str>,
) -> Option<Self> {
Some(match (code, reason?) {
(CloudErrorCode::InvalidState, "task_claimed") => Self::Claimed,
(CloudErrorCode::InvalidState, "task_not_open") => Self::NotOpen,
(CloudErrorCode::InvalidState, "stale_claim") => Self::StaleClaim,
(CloudErrorCode::InvalidState, "task_closed") => Self::Closed,
(CloudErrorCode::ForbiddenRight, "self_verify") => Self::SelfVerify,
(CloudErrorCode::InvalidState, "not_submitted") => Self::NotSubmitted,
(CloudErrorCode::ForbiddenRight, "not_permitted") => Self::NotPermitted(match check {
Some("presence") => PermitCheck::Presence,
Some("role") => PermitCheck::Role,
_ => PermitCheck::Unknown,
}),
(CloudErrorCode::ForbiddenRight, "optin_scope") => Self::OptinScope,
(CloudErrorCode::ForbiddenRight, "person_unproven") => Self::PersonUnproven,
(CloudErrorCode::ForbiddenRight, "signature_expired") => Self::SignatureExpired,
(CloudErrorCode::InvalidState, "optin_changed") => Self::OptinChanged,
(CloudErrorCode::InvalidState, "optin_inactive") => Self::OptinInactive,
(CloudErrorCode::RateLimited, "daily_budget") => Self::DailyBudget { retry_after },
(CloudErrorCode::InvalidState | CloudErrorCode::RevisionConflict, "stale_base") => {
Self::StaleBase
}
_ => return None,
})
}
#[must_use]
pub fn ends_claim(self) -> bool {
matches!(self, Self::StaleClaim | Self::Closed | Self::OptinInactive)
}
}
impl fmt::Display for TaskRefusal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Claimed => f.write_str("another agent holds the task"),
Self::NotOpen => f.write_str("the task is not open"),
Self::StaleClaim => f.write_str("this claim is no longer the task's claim"),
Self::Closed => f.write_str("the task is closed"),
Self::SelfVerify => f.write_str(
"the agent that did the work does not check its own result; another agent verifies it",
),
Self::NotSubmitted => f.write_str("the task has no result waiting for a check"),
Self::NotPermitted(PermitCheck::Presence) => f.write_str(
"this agent is not on the bus: its session (or the relay) must be running and announce it with the task's role",
),
Self::NotPermitted(PermitCheck::Role) => {
f.write_str("this agent is on the bus in another role than the task needs")
}
Self::NotPermitted(PermitCheck::Unknown) => f.write_str(
"the task's role or the owner's worker consent does not allow this agent",
),
Self::OptinScope => f.write_str("this instance's opt-in does not cover this vault"),
Self::OptinChanged => f.write_str("the opt-in changed; it is read again"),
Self::OptinInactive => f.write_str(
"this instance is not opted in (or the owner stopped worker mode on the web)",
),
Self::StaleBase => f.write_str(
"the task's priority or labels changed meanwhile; read it again and retry",
),
Self::PersonUnproven => f.write_str(
"the app's signature on this task did not check out; propose it again in the app",
),
Self::SignatureExpired => f.write_str(
"the app's signature on this task is too old; propose it again in the app",
),
Self::DailyBudget { retry_after } => match retry_after {
Some(s) => write!(f, "today's task budget is used up (resets in {s} s)"),
None => f.write_str("today's task budget is used up"),
},
}
}
}
fn bus_role(role: Option<&String>) -> Option<&str> {
role.map(String::as_str).filter(|r| {
(1..=64).contains(&r.len())
&& r.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
})
}
pub(crate) fn safe_message(message: &str) -> Option<String> {
let text: String = message
.chars()
.map(|c| if c.is_control() { ' ' } else { c })
.take(300)
.collect();
let text = text.trim();
(!text.is_empty()).then(|| text.to_string())
}
fn permit_line(refusal: TaskRefusal, detail: &CloudErrorDetailV1) -> Option<String> {
let TaskRefusal::NotPermitted(check) = refusal else {
return None;
};
let mut line =
safe_message(&detail.message).unwrap_or_else(|| match permit_roles(refusal, detail) {
Some(roles) => format!("{refusal} ({roles})"),
None => refusal.to_string(),
});
match check {
PermitCheck::Presence => line.push_str(
" [check: presence] Run `lean-ctx cloud bus relay --for 30`: it says who this machine announced, or why an agent is missing. The Cloud keeps an agent present only while a relay stays connected (the daemon's), so claim while it runs.",
),
PermitCheck::Role => line.push_str(" [check: role]"),
PermitCheck::Unknown => {}
}
Some(line)
}
fn permit_roles(refusal: TaskRefusal, detail: &CloudErrorDetailV1) -> Option<String> {
let role = bus_role(detail.role.as_ref())?;
match refusal {
TaskRefusal::NotPermitted(PermitCheck::Presence) => {
Some(format!("the task needs role {role}"))
}
TaskRefusal::NotPermitted(PermitCheck::Role) => {
Some(match bus_role(detail.agent_role.as_ref()) {
Some(agent_role) => {
format!("the agent works as {agent_role}, the task needs {role}")
}
None => format!("the task needs role {role}"),
})
}
_ => None,
}
}
fn lenient_u64(value: &serde_json::Value) -> Option<u64> {
value
.as_u64()
.or_else(|| value.as_str().and_then(|s| s.trim().parse().ok()))
}
fn lenient_detail(error: &serde_json::Value) -> Option<CloudErrorDetailV1> {
let object = error.as_object()?;
let code = object.get("code").filter(|c| c.is_string())?.clone();
let message = object
.get("message")
.filter(|m| m.is_string())
.cloned()
.unwrap_or_else(|| serde_json::Value::String(String::new()));
let base = || {
let mut kept = serde_json::Map::new();
kept.insert("code".into(), code.clone());
kept.insert("message".into(), message.clone());
kept
};
let decodes = |kept: &serde_json::Map<String, serde_json::Value>| {
serde_json::from_value::<CloudErrorDetailV1>(serde_json::Value::Object(kept.clone()))
.is_ok()
};
let mut kept = base();
for (name, value) in object {
if matches!(name.as_str(), "code" | "message") {
continue;
}
let candidates = [
Some(value.clone()),
lenient_u64(value).map(serde_json::Value::from),
];
for candidate in candidates.into_iter().flatten() {
let mut probe = base();
probe.insert(name.clone(), candidate.clone());
if decodes(&probe) {
kept.insert(name.clone(), candidate);
break;
}
}
}
serde_json::from_value(serde_json::Value::Object(kept)).ok()
}
fn task_key_refusal(status: u16, body: &serde_json::Value) -> Option<String> {
let error = &body["error"];
let code = error["code"].as_str()?;
let reason = error["reason"].as_str();
match (status, code) {
(400, "invalid_field") if error["field"].as_str() == Some("task_propose_key") => {
Some(TASK_KEY_INVALID.into())
}
(409, "invalid_state") => reason
.filter(|r| matches!(*r, TASK_KEY_BOUND | TASK_KEY_CLEARED))
.map(str::to_owned),
_ => None,
}
}
fn optin_key_refusal(status: u16, body: &serde_json::Value) -> Option<String> {
let error = &body["error"];
let code = error["code"].as_str()?;
match (status, code) {
(400, "invalid_field")
if matches!(
error["field"].as_str(),
Some("optin_key" | "optin_key.bind_signature")
) =>
{
Some(OPTIN_KEY_INVALID.into())
}
(409, _) => [code, error["reason"].as_str().unwrap_or_default()]
.into_iter()
.find(|name| matches!(*name, OPTIN_KEY_BOUND | OPTIN_KEY_CLEARED))
.map(str::to_owned),
_ => None,
}
}
pub const OPTIN_KEY_INVALID: &str = "optin_key_invalid";
pub const OPTIN_KEY_BOUND: &str = "optin_key_bound";
pub const OPTIN_KEY_CLEARED: &str = "optin_key_cleared";
pub const APP_KEY_NOT_ANNOUNCED: &str = "app_key_not_announced";
pub const APP_KEY_BIND_INVALID: &str = "app_key_bind_invalid";
pub const APP_KEYS_INVALID: &str = "app_keys_invalid";
pub const TASK_KEY_INVALID: &str = "task_propose_key_invalid";
pub const TASK_KEY_BOUND: &str = "task_propose_key_bound";
pub const TASK_KEY_CLEARED: &str = "task_propose_key_cleared";
pub const RETRY_AFTER_CAP_S: u64 = 32 * 86_400;
impl CloudError {
pub(crate) fn from_response(status: u16, body: &serde_json::Value) -> Self {
let value_reason = body["error"]["reason"].clone();
let revocation = body["error"]["revocation"].clone();
let trial_used = body["error"]["code"].as_str() == Some(TRIAL_USED);
let plans_url = web_url(body["error"]["plans_url"].as_str());
let decoded = serde_json::from_value::<CloudErrorBody>(body.clone())
.map(|body| body.error)
.ok()
.or_else(|| lenient_detail(&body["error"]));
if status == 400
&& body["error"]["code"].as_str() == Some("invalid_field")
&& let Some(reason @ (APP_KEY_NOT_ANNOUNCED | APP_KEY_BIND_INVALID)) =
body["error"]["reason"].as_str()
{
return Self::Server {
status,
code: reason.into(),
};
}
if status == 400
&& body["error"]["code"].as_str() == Some("invalid_field")
&& body["error"]["field"].as_str() == Some("app_keys")
{
return Self::Server {
status,
code: APP_KEYS_INVALID.into(),
};
}
if let Some(code) = task_key_refusal(status, body) {
return Self::Server { status, code };
}
if let Some(code) = optin_key_refusal(status, body) {
return Self::Server { status, code };
}
if matches!(status, 400 | 409)
&& let Some(name) = [&body["error"]["reason"], &body["error"]["code"]]
.iter()
.filter_map(|v| v.as_str())
.find(|name| crate::auth::CHAT_KEY_REPAIR_REASONS.contains(name))
{
return Self::Server {
status,
code: name.to_owned(),
};
}
if status == 409 && body["error"]["code"].as_str() == Some(NOT_READY) {
return Self::Server {
status,
code: NOT_READY.into(),
};
}
if status == 403 && body["error"]["code"].as_str() == Some(VAULT_CREATE_FORBIDDEN) {
return Self::Forbidden;
}
let Some(mut detail) = decoded else {
return Self::of_status(status, lenient_u64(&body["error"]["retry_after_s"]));
};
detail.retry_after_s = detail.retry_after_s.map(|s| s.min(RETRY_AFTER_CAP_S));
if let Some(refusal) = TaskRefusal::from_reason(
detail.code,
detail.reason.as_deref(),
detail.retry_after_s,
detail.check.as_deref(),
) {
return Self::Task {
refusal,
number: detail.number,
detail: permit_line(refusal, &detail),
};
}
let next_step = NextStep::of_wire(detail.next_step.as_deref());
if trial_used {
return Self::PlanRequired(Box::new(PlanNeed {
plan_needed: wire_token(detail.plan_needed.as_deref(), 32),
message: safe_message(&detail.message),
reason: Some(TRIAL_USED.into()),
next_step,
plans_url,
trial_reason: wire_token(detail.reason.as_deref(), 32),
..PlanNeed::default()
}));
}
match detail.code {
CloudErrorCode::Unauthorized => Self::Unauthorized,
CloudErrorCode::LeaseExpired => Self::LeaseLapsed,
CloudErrorCode::InstanceRevoked => Self::Revoked(Revocation::of_statement(&revocation)),
CloudErrorCode::PlanRequired => Self::PlanRequired(Box::new(PlanNeed {
feature: wire_token(detail.feature.as_deref(), 64),
plan_needed: wire_token(detail.plan_needed.as_deref(), 32),
limit: detail.limit,
used: detail.used,
message: safe_message(&detail.message),
plan: None,
reason: wire_token(detail.reason.as_deref(), 32),
next_step,
plans_url: None,
trial_reason: None,
})),
CloudErrorCode::InstanceLimit => Self::InstanceLimit(Box::new(InstanceCap {
resource: wire_token(detail.resource.as_deref(), 64),
limit: detail.limit,
used: detail.used,
seats: detail.seats,
per_seat: detail.per_seat,
next_step,
})),
CloudErrorCode::QuotaExceeded => Self::QuotaExceeded(Box::new(Quota {
status,
resource: wire_token(detail.resource.as_deref(), 64)
.unwrap_or_else(|| "unknown".into()),
limit: detail.limit,
used: detail.used,
reset_at: wire_time(detail.reset_at.as_deref()),
retry_after: detail.retry_after_s,
cooldown_days: detail.cooldown_days,
next_step,
})),
CloudErrorCode::StorageQuotaExceeded => Self::StorageFull(Box::new(StorageFull {
limit: detail.limit_bytes.or(detail.limit),
used: detail.used_bytes.or(detail.used),
next_step,
})),
CloudErrorCode::UploadRateLimited => Self::UploadRateLimited {
retry_after: detail.retry_after_s,
per_hour: detail.limit_bytes_per_hour,
},
CloudErrorCode::PolicyDenied => Self::PolicyDenied(PolicyDenial {
member: wire_token(detail.member.as_deref().or(detail.field.as_deref()), 80),
level: wire_token(
detail.level.as_deref().or(detail
.reason
.as_deref()
.filter(|r| matches!(*r, "org" | "workspace" | "vault" | "instance"))),
16,
),
action: wire_token(detail.action.as_deref(), 64),
}),
CloudErrorCode::ForbiddenRight if value_reason.as_str() == Some("vetoed") => {
Self::Vetoed
}
CloudErrorCode::ForbiddenRight => Self::Forbidden,
CloudErrorCode::UploadDisabled => {
Self::UploadDisabled(detail.kind.unwrap_or(CloudUploadKind::Unknown))
}
CloudErrorCode::NotFound => Self::NotFound,
CloudErrorCode::FeatureDisabled => Self::FeatureDisabled {
feature: wire_token(detail.feature.as_deref(), 64),
},
CloudErrorCode::RevisionConflict => Self::Conflict {
expected: detail.expected,
actual: detail.actual,
},
CloudErrorCode::ScreeningRequired => Self::ScreeningBlocked,
CloudErrorCode::UpgradeRequired => Self::UpgradeRequired {
min: detail.min_instance,
},
CloudErrorCode::RateLimited => Self::RateLimited {
retry_after: detail.retry_after_s,
},
CloudErrorCode::ExpiredToken => Self::DeviceFlow(CODE_EXPIRED.into()),
CloudErrorCode::AccessDenied => Self::DeviceFlow(CODE_DENIED.into()),
_ if status == 429 => Self::RateLimited {
retry_after: detail.retry_after_s,
},
CloudErrorCode::Unknown => Self::of_status(status, detail.retry_after_s),
other => Self::Server {
status,
code: serde_json::to_value(other)
.ok()
.and_then(|v| v.as_str().map(str::to_string))
.unwrap_or_else(|| "unknown".into()),
},
}
}
#[must_use]
pub fn with_origin(mut self, origin: &str) -> Self {
if let Self::PlanRequired(need) = &mut self
&& let Some(url) = need.plans_url.take()
{
let base = origin.trim_end_matches('/');
need.plans_url = if !(base.starts_with("https://") || base.starts_with("http://")) {
None
} else if url.starts_with('/') {
Some(format!("{base}{url}"))
} else if same_origin(&url, base) {
Some(url)
} else {
None
};
}
self
}
#[must_use]
pub fn for_trial(self) -> Self {
match self {
Self::DeviceFlow(why) if why == CODE_EXPIRED => {
Self::DeviceFlow(TRIAL_CODE_EXPIRED.into())
}
other => other,
}
}
fn of_status(status: u16, retry_after: Option<u64>) -> Self {
match status {
429 => Self::RateLimited {
retry_after: retry_after.map(|s| s.min(RETRY_AFTER_CAP_S)),
},
413 => Self::Server {
status,
code: "payload_too_large".into(),
},
_ => Self::Server {
status,
code: "unknown".into(),
},
}
}
#[must_use]
pub fn with_plan(mut self, plan: CloudPlan) -> Self {
if let Self::PlanRequired(need) = &mut self
&& (need.feature.is_some() || need.locked().is_some())
&& plan != CloudPlan::Unknown
{
need.plan.get_or_insert(plan);
}
self
}
pub fn is_transient(&self) -> bool {
match self {
Self::QuotaExceeded(q) => q.waits(),
other => matches!(
other,
Self::Offline(_)
| Self::RateLimited { .. }
| Self::UploadRateLimited { .. }
| Self::Server {
status: 408 | 500..,
..
}
),
}
}
pub fn retry_after_at(&self, now: lean_ctx_time::UtcDateTime) -> Option<u64> {
match self {
Self::RateLimited { retry_after } | Self::UploadRateLimited { retry_after, .. } => {
*retry_after
}
Self::QuotaExceeded(q) if q.waits() => q.retry_after.or_else(|| {
let reset =
lean_ctx_time::OffsetDateTime::parse_from_rfc3339(q.reset_at.as_deref()?)
.ok()?;
u64::try_from((reset.to_utc() - now).num_seconds().max(1)).ok()
}),
_ => None,
}
}
}
impl fmt::Display for CloudError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Offline(why) => write!(f, "The Cloud is not reachable ({why})"),
Self::NotConnected => f.write_str("not connected to the Cloud (run `lean-ctx cloud connect`)"),
Self::Unauthorized => f.write_str(
"The Cloud does not accept this instance any more (run `lean-ctx cloud connect` again)",
),
Self::PlanRequired(need) => {
write_plan_need(f, need)?;
if need.locked().is_none() && !need.trial_used() {
write_hint(f, need.next_step)?;
}
Ok(())
}
Self::InstanceLimit(cap) => write_instance_cap(f, cap),
Self::QuotaExceeded(q) => write_quota(f, q),
Self::StorageFull(s) => {
f.write_str("this workspace's Cloud storage is full")?;
if let (Some(used), Some(limit)) = (s.used, s.limit) {
write!(f, " ({} of {})", bytes_text(used), bytes_text(limit))?;
}
f.write_str(
"; nothing is charged for more; uploads stop until storage frees (remove vaults or files in the Cloud)",
)?;
write_hint(f, s.next_step)
}
Self::UploadRateLimited {
retry_after,
per_hour,
} => {
f.write_str("The Cloud's upload allowance for this hour is used up")?;
if let Some(per_hour) = per_hour {
write!(f, " ({} per hour)", bytes_text(*per_hour))?;
}
match retry_after {
Some(s) => write!(f, "; the rest goes out in {s} s")?,
None => f.write_str("; the rest goes out later")?,
}
f.write_str(" (a large first sync can take a few hours)")
}
Self::PolicyDenied(d) => {
f.write_str("Cloud policy refuses this")?;
if let Some(member) = &d.member {
write!(f, " ({member}")?;
if let Some(level) = &d.level {
write!(f, ", set at the {level} level")?;
}
f.write_str(")")?;
}
match &d.action {
Some(action) => write!(
f,
"; a person does this on the Cloud website ({})",
action.replace('_', " ")
),
None => f.write_str(
"; it is not retried until an admin changes the policy in the Cloud",
),
}
}
Self::LeaseLapsed => {
f.write_str("the Cloud lease has lapsed (run `lean-ctx cloud connect` again)")
}
Self::Revoked(r) => {
f.write_str("this instance was revoked by ")?;
match &r.workspace {
Some(workspace) => f.write_str(workspace)?,
None => f.write_str("its workspace")?,
}
if let Some(reason) = &r.reason {
write!(f, " ({})", reason.replace('_', " "))?;
}
f.write_str("; reconnect with `lean-ctx cloud connect` after an admin allows it")
}
Self::Forbidden => f.write_str("The Cloud does not allow this for your account"),
Self::Vetoed => f.write_str("a workspace veto bars it until the veto is lifted"),
Self::UploadDisabled(kind) => match kind {
CloudUploadKind::Metrics => {
f.write_str("the workspace has switched metrics upload off")
}
CloudUploadKind::Receipts => {
f.write_str("the workspace has switched receipts upload off")
}
CloudUploadKind::Unknown => {
f.write_str("the workspace has switched this upload off")
}
},
Self::NotFound => f.write_str("The Cloud does not know that item"),
Self::FeatureDisabled { feature } => match feature.as_deref() {
Some("instance_metrics") | None => f.write_str(
"this Cloud server has switched off instance metrics; nothing was sent",
),
Some(other) => write!(
f,
"this Cloud server has switched off {}; nothing was sent",
other.replace('_', " ")
),
},
Self::Conflict { expected, actual } => match (expected, actual) {
(Some(e), Some(a)) => {
write!(f, "knowledge moved on (expected revision {e}, now {a})")
}
_ => f.write_str("knowledge moved on"),
},
Self::ScreeningBlocked => f.write_str("Cloud screening refused the content"),
Self::UpgradeRequired { min } => match min {
Some(min) => write!(f, "The Cloud needs lean-ctx {min} or newer (run `lean-ctx update`)"),
None => f.write_str("The Cloud needs a newer lean-ctx (run `lean-ctx update`)"),
},
Self::RateLimited { retry_after } => match retry_after {
Some(s) => write!(f, "The Cloud asks to try again in {s} s"),
None => f.write_str("The Cloud asks to try again later"),
},
Self::DeviceFlow(why) => write!(f, "connection not approved: {why}"),
Self::TrialUnsupported { origin } => write!(
f,
"LeanCTX does not offer the one-step trial here yet. Start the trial on {origin}, or sign in with `lean-ctx cloud connect`"
),
Self::Protocol(why) => write!(f, "unexpected answer from Cloud: {why}"),
Self::Config(why) => write!(f, "Cloud configuration: {why}"),
Self::Local(why) => write!(f, "local Cloud state: {why}"),
Self::Refused(why) => f.write_str(why),
Self::Task {
refusal,
number,
detail,
} => {
if let Some(number) = number {
write!(f, "task #{number}: ")?;
}
match detail {
Some(detail) if matches!(refusal, TaskRefusal::NotPermitted(_)) => {
f.write_str(detail)
}
Some(detail) => write!(f, "{refusal} ({detail})"),
None => write!(f, "{refusal}"),
}
}
Self::Server { status, code } => write!(f, "The Cloud answered {status} ({code})"),
}
}
}
impl std::error::Error for CloudError {}
pub const CODE_EXPIRED: &str =
"the code expired before it was approved; run `lean-ctx cloud connect` again for a new one";
pub const TRIAL_CODE_EXPIRED: &str = "the code expired before it was approved. If the browser said this address already had its trial, choose a plan in the Cloud, then run `lean-ctx cloud connect`; otherwise run `lean-ctx cloud connect --trial` again for a new code";
pub const CODE_DENIED: &str =
"you chose \"Not mine\" in the browser; nothing changed on this machine";
impl CloudError {
#[must_use]
pub fn event_code(&self) -> String {
match self {
Self::PlanRequired(need) if need.trial_used() => TRIAL_USED.into(),
Self::PlanRequired(_) => "plan_required".into(),
Self::TrialUnsupported { .. } => "trial_unsupported".into(),
Self::DeviceFlow(why) if why == CODE_EXPIRED || why == TRIAL_CODE_EXPIRED => {
"expired".into()
}
Self::DeviceFlow(why) if why == CODE_DENIED => "denied".into(),
Self::DeviceFlow(why) if why == crate::auth::TASK_KEY_REFUSED => {
TASK_KEY_INVALID.into()
}
Self::DeviceFlow(why) if why == crate::auth::TASK_KEY_ALREADY_BOUND => {
TASK_KEY_BOUND.into()
}
Self::DeviceFlow(why) if why == crate::auth::TASK_KEY_WAS_REMOVED => {
TASK_KEY_CLEARED.into()
}
Self::DeviceFlow(why) if why == crate::auth::OPTIN_KEY_REFUSED => {
OPTIN_KEY_INVALID.into()
}
Self::DeviceFlow(why) if why == crate::auth::OPTIN_KEY_ALREADY_BOUND => {
OPTIN_KEY_BOUND.into()
}
Self::DeviceFlow(why) if why == crate::auth::OPTIN_KEY_WAS_REMOVED => {
OPTIN_KEY_CLEARED.into()
}
Self::DeviceFlow(why) if why == crate::auth::OPTIN_KEY_TOKEN_ENROLLMENT => {
"token_enrollment".into()
}
Self::DeviceFlow(why) if why == crate::auth::APP_KEY_PAIRING_FAILED => {
APP_KEY_NOT_ANNOUNCED.into()
}
Self::DeviceFlow(_) => "not_approved".into(),
Self::InstanceLimit(_) => "instance_limit".into(),
Self::QuotaExceeded(_) => "quota_exceeded".into(),
Self::Offline(_) => "offline".into(),
Self::Unauthorized => "unauthorized".into(),
Self::Revoked(_) => "instance_revoked".into(),
Self::RateLimited { .. } => "rate_limited".into(),
Self::UpgradeRequired { .. } => "upgrade_required".into(),
Self::Config(_) => "config".into(),
Self::Protocol(_) => "protocol".into(),
Self::Local(_) => "local".into(),
Self::Server { code, .. } if wire_token(Some(code), 64).is_some() => code.clone(),
_ => "error".into(),
}
}
}
pub const RECOVER_HINT: &str = "for a lost or broken device, recover its slot now on the Cloud website (Hosts › Recover the slot now, with your second factor; once per 30 days)";
fn host_cap_head(resource: &str) -> Option<&'static str> {
match resource {
"registered_instances" => Some("all hosts the plan includes are registered"),
"connected_instances" => Some("all hosts that may be connected at once are connected"),
_ => None,
}
}
fn write_instance_cap(f: &mut fmt::Formatter<'_>, cap: &InstanceCap) -> fmt::Result {
let connected = cap.resource.as_deref() == Some("connected_instances");
let resource = if connected {
"connected_instances"
} else {
"registered_instances"
};
f.write_str(host_cap_head(resource).unwrap_or_default())?;
if let (Some(used), Some(limit)) = (cap.used, cap.limit) {
write!(f, " ({used} of {limit}")?;
if let (Some(seats), Some(per_seat)) = (cap.seats, cap.per_seat) {
write!(f, ": {seats} seats, {per_seat} per seat")?;
}
f.write_str(")")?;
}
if connected {
write!(f, "; {FREE_CONNECTED}, or upgrade the plan")?;
} else {
write!(f, "; {FREE_REGISTERED}, or upgrade the plan")?;
}
write_hint(f, cap.next_step)
}
#[must_use]
pub fn quota_label(resource: &str) -> &str {
match resource {
"transfer_bytes" => "monthly transfer",
"bus_messages" => "monthly bus messages",
"registered_instances" => HOSTS_REGISTERED,
"private_vaults" => "private vaults",
"connected_instances" => HOSTS_CONNECTED,
"storage_bytes" => "storage",
"model_credit_usd_micros" => "model credit",
other => other,
}
}
fn write_quota(f: &mut fmt::Formatter<'_>, q: &Quota) -> fmt::Result {
let what = quota_label(&q.resource);
if let Some(head) = host_cap_head(&q.resource) {
f.write_str(head)?;
if let (Some(used), Some(limit)) = (q.used, q.limit) {
write!(f, " ({used} of {limit})")?;
}
} else {
write!(f, "this workspace has used all its {what}")?;
if let (Some(used), Some(limit)) = (q.used, q.limit) {
write!(
f,
" ({} of {})",
amount_text(&q.resource, used),
amount_text(&q.resource, limit)
)?;
}
}
f.write_str("; nothing is charged for more")?;
match q.resource.as_str() {
"registered_instances" if q.cooling_slot() => write!(
f,
"; a revoked host stays registered until {} ({}-day cool-down), or {RECOVER_HINT}",
q.reset_at.as_deref().unwrap_or("?"),
q.cooldown_days.unwrap_or(0)
)?,
"registered_instances" => write!(f, "; {FREE_REGISTERED}, or upgrade the plan")?,
"connected_instances" => write!(f, "; {FREE_CONNECTED}")?,
"private_vaults" => {
f.write_str("; remove or publish a vault in the Cloud, or upgrade the plan")?;
}
"model_credit_usd_micros" => {}
_ if q.waits() => match &q.reset_at {
Some(at) => write!(f, "; uploads wait in the local outbox until {at}")?,
None => f.write_str("; uploads wait in the local outbox until it frees")?,
},
_ => {}
}
write_hint(f, q.next_step)
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn trial_used_refusal_names_the_used_trial_and_the_plans_page() {
let body = json!({"error": {
"code": "trial_used",
"message": "This address already had a trial.",
"plan_needed": "team",
"next_step": "subscribe",
"plans_url": "https://cloud.leanctx.com/app/plans"
}});
let error = CloudError::from_response(402, &body);
let CloudError::PlanRequired(need) = &error else {
panic!("{error:?}");
};
assert!(need.trial_used());
assert_eq!(need.plan_needed.as_deref(), Some("team"));
assert_eq!(
need.plans_url.as_deref(),
Some("https://cloud.leanctx.com/app/plans")
);
assert_eq!(error.event_code(), "trial_used");
assert!(!error.is_transient());
assert_eq!(
error.to_string(),
"this address already had its Team trial; no second trial starts. Choose a plan at https://cloud.leanctx.com/app/plans, then run `lean-ctx cloud connect`"
);
let ended = CloudError::from_response(
402,
&json!({"error": {"code": "trial_used", "reason": "trial_ended", "message": "",
"plan_needed": "personal", "plans_url": "https://cloud.leanctx.com/app/plans"}}),
);
assert_eq!(ended.event_code(), "trial_used");
assert_eq!(
ended.to_string(),
"the Personal trial of this address has ended; no second trial starts. Choose a plan at https://cloud.leanctx.com/app/plans, then run `lean-ctx cloud connect`"
);
let no_plan = CloudError::from_response(
402,
&json!({"error": {"code": "trial_used", "reason": "no_plan", "message": "",
"plan_needed": "team"}}),
);
assert_eq!(no_plan.event_code(), "trial_used");
assert!(no_plan.to_string().starts_with(
"this address has no Team trial left and no plan; no second trial starts."
));
let path = format!(
"{}/crates/lean-ctx-protocol/tests/fixtures/cloud/error.trial_used.json",
concat!(env!("CARGO_MANIFEST_DIR"), "/../..")
);
let fixture: serde_json::Value =
serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
let refused = CloudError::from_response(402, &fixture);
assert_eq!(refused.event_code(), "trial_used");
assert_eq!(
refused.to_string(),
"this address already had its Personal trial; no second trial starts. Choose a plan in the Cloud, then run `lean-ctx cloud connect`"
);
assert_eq!(
refused
.with_origin("https://cloud.leanctx.com/")
.to_string(),
"this address already had its Personal trial; no second trial starts. Choose a plan at https://cloud.leanctx.com/o/bob/billing/?plan=personal, then run `lean-ctx cloud connect`"
);
let hostile = CloudError::from_response(
402,
&json!({"error": {"code": "trial_used", "message": "", "plan_needed": "personal",
"plans_url": "javascript:alert(1)"}}),
);
assert_eq!(
hostile.to_string(),
"this address already had its Personal trial; no second trial starts. Choose a plan in the Cloud, then run `lean-ctx cloud connect`"
);
let plain = CloudError::from_response(
402,
&json!({"error": {"code": "plan_required", "message": "", "feature": "audit_export", "plan_needed": "team"}}),
);
assert_eq!(plain.event_code(), "plan_required");
}
fn trial_used_with(plans_url: &str) -> CloudError {
CloudError::from_response(
402,
&json!({"error": {"code": "trial_used", "message": "", "plan_needed": "personal",
"plans_url": plans_url}}),
)
}
const NO_PAGE: &str = "this address already had its Personal trial; no second trial starts. Choose a plan in the Cloud, then run `lean-ctx cloud connect`";
#[test]
fn trial_plans_url_on_another_origin_is_not_shown() {
let origin = "https://cloud.leanctx.com";
assert_eq!(
trial_used_with("https://Cloud.LeanCTX.com/app/plans?plan=personal")
.with_origin(origin)
.to_string(),
"this address already had its Personal trial; no second trial starts. Choose a plan at https://Cloud.LeanCTX.com/app/plans?plan=personal, then run `lean-ctx cloud connect`"
);
for foreign in [
"http://cloud.leanctx.com/app/plans",
"https://evil.example/app/plans",
"https://cloud.leanctx.com.evil.example/app/plans",
"https://cloud.leanctx.com@evil.example/app/plans",
"https://cloud.leanctx.com:8443/app/plans",
"https://cloud.leanctx.com",
] {
let shown = trial_used_with(foreign).with_origin(origin).to_string();
let expected = if foreign == "https://cloud.leanctx.com" {
"this address already had its Personal trial; no second trial starts. Choose a plan at https://cloud.leanctx.com, then run `lean-ctx cloud connect`"
} else {
NO_PAGE
};
assert_eq!(shown, expected, "{foreign}");
}
assert_eq!(
trial_used_with("https://cloud.leanctx.com/app/plans")
.with_origin("file:///tmp")
.to_string(),
NO_PAGE
);
}
#[test]
fn trial_plans_url_with_format_characters_is_not_shown() {
for c in [
'\u{202E}',
'\u{202A}',
'\u{2066}',
'\u{2069}',
'\u{200B}',
'\u{200D}',
'\u{200E}',
'\u{061C}',
'\u{00AD}',
'\u{FEFF}',
'\u{E0041}',
'\u{3164}',
] {
let url = format!("/o/bob/billing/{c}?plan=personal");
assert_eq!(
trial_used_with(&url)
.with_origin("https://cloud.leanctx.com")
.to_string(),
NO_PAGE,
"U+{:04X}",
c as u32
);
}
assert_eq!(
trial_used_with("/o/bob/billing/?plan=personal")
.with_origin("https://cloud.leanctx.com")
.to_string(),
"this address already had its Personal trial; no second trial starts. Choose a plan at https://cloud.leanctx.com/o/bob/billing/?plan=personal, then run `lean-ctx cloud connect`"
);
}
#[test]
fn trial_code_expiry_points_to_the_plans_page() {
let expired = CloudError::DeviceFlow(CODE_EXPIRED.into()).for_trial();
assert_eq!(expired, CloudError::DeviceFlow(TRIAL_CODE_EXPIRED.into()));
assert_eq!(expired.event_code(), "expired");
assert!(expired.to_string().contains("choose a plan in the Cloud"));
assert!(
!expired
.to_string()
.contains("run `lean-ctx cloud connect` again")
);
let denied = CloudError::DeviceFlow(CODE_DENIED.into());
assert_eq!(denied.clone().for_trial(), denied);
assert_eq!(
CloudError::Offline("reset".into()).for_trial(),
CloudError::Offline("reset".into())
);
}
#[test]
fn trial_event_codes_name_the_device_flow_outcomes() {
assert_eq!(
CloudError::DeviceFlow(CODE_EXPIRED.into()).event_code(),
"expired"
);
assert_eq!(
CloudError::from_response(
400,
&json!({"error": {"code": "access_denied", "message": ""}})
)
.event_code(),
"denied"
);
assert_eq!(
CloudError::Server {
status: 400,
code: "invalid_request".into()
}
.event_code(),
"invalid_request"
);
assert_eq!(
CloudError::Server {
status: 500,
code: "Bad Code\n".into()
}
.event_code(),
"error"
);
assert_eq!(CloudError::Offline("reset".into()).event_code(), "offline");
assert_eq!(
CloudError::DeviceFlow(crate::auth::APP_KEY_PAIRING_FAILED.into()).event_code(),
APP_KEY_NOT_ANNOUNCED
);
assert_eq!(APP_KEY_NOT_ANNOUNCED, "app_key_not_announced");
for reason in [APP_KEY_NOT_ANNOUNCED, APP_KEY_BIND_INVALID] {
let body = serde_json::json!({"error": {"code": "invalid_field",
"field": "app_keys_bind_signature", "reason": reason, "message": "x"}});
assert!(matches!(
CloudError::from_response(400, &body),
CloudError::Server { status: 400, code } if code == reason
));
}
}
#[test]
fn invalid_field_keeps_its_code() {
assert_eq!(
CloudError::from_response(
400,
&json!({"error": {"code": "invalid_field", "message": "x",
"field": "chat_key.bind_signature"}}),
),
CloudError::Server {
status: 400,
code: "invalid_field".into()
}
);
}
#[test]
fn a_detail_with_a_mistyped_member_keeps_its_code_and_retry() {
let limited = CloudError::from_response(
429,
&json!({"error": {"code": "rate_limited", "message": "slow down",
"retry_after_s": 12, "number": "seventeen"}}),
);
assert_eq!(
limited,
CloudError::RateLimited {
retry_after: Some(12)
}
);
let claimed = CloudError::from_response(
409,
&json!({"error": {"code": "invalid_state", "message": "x",
"reason": "task_claimed", "number": "17", "limit": {"odd": true}}}),
);
assert!(
matches!(
claimed,
CloudError::Task {
refusal: TaskRefusal::Claimed,
number: Some(17),
..
}
),
"{claimed:?}"
);
let quota = CloudError::from_response(
429,
&json!({"error": {"code": "quota_exceeded", "message": "x",
"resource": "bus_messages", "retry_after_s": "60", "used": -1}}),
);
let CloudError::QuotaExceeded(quota) = quota else {
panic!("{quota:?}");
};
assert_eq!(
(quota.resource.as_str(), quota.retry_after, quota.used),
("bus_messages", Some(60), None)
);
assert_eq!(
CloudError::from_response(400, &json!({"error": {"code": 7}})),
CloudError::Server {
status: 400,
code: "unknown".into()
}
);
}
#[test]
fn edge_answers_are_classified_by_status() {
let busy = CloudError::from_response(
429,
&json!({"error": {"code": "server_busy", "message": "x", "retry_after_s": 5}}),
);
assert_eq!(
busy,
CloudError::RateLimited {
retry_after: Some(5)
}
);
let header_only = CloudError::from_response(429, &json!({"error": {"retry_after_s": 9}}));
assert_eq!(
header_only,
CloudError::RateLimited {
retry_after: Some(9)
}
);
assert_eq!(
CloudError::from_response(429, &serde_json::Value::Null),
CloudError::RateLimited { retry_after: None }
);
for status in [408, 500, 502, 503, 504] {
let down = CloudError::from_response(status, &serde_json::Value::Null);
assert!(down.is_transient(), "{status}: {down:?}");
}
let too_large = CloudError::from_response(413, &serde_json::Value::Null);
assert_eq!(
too_large,
CloudError::Server {
status: 413,
code: "payload_too_large".into()
}
);
assert!(!too_large.is_transient());
}
#[test]
fn instance_limit_carries_the_seat_numbers() {
let error = CloudError::from_response(
409,
&json!({"error": {"code": "instance_limit", "message": "x", "limit": 2, "used": 2}}),
);
assert_eq!(
error,
CloudError::InstanceLimit(Box::new(InstanceCap {
limit: Some(2),
used: Some(2),
..InstanceCap::default()
}))
);
assert_eq!(
error.to_string(),
"all hosts the plan includes are registered (2 of 2); to free one, revoke a host you no longer use on the Hosts page, or upgrade the plan"
);
}
#[test]
fn a_connected_limit_says_disconnect_and_names_the_seats() {
let error = CloudError::from_response(
409,
&json!({"error": {"code": "instance_limit", "message": "x", "resource": "connected_instances",
"limit": 4, "used": 4, "plan": "team", "next_step": "add_seats",
"seats": 2, "per_seat": 2}}),
);
assert_eq!(
error,
CloudError::InstanceLimit(Box::new(InstanceCap {
resource: Some("connected_instances".into()),
limit: Some(4),
used: Some(4),
seats: Some(2),
per_seat: Some(2),
next_step: Some(NextStep::AddSeats),
}))
);
assert_eq!(
error.to_string(),
"all hosts that may be connected at once are connected (4 of 4: 2 seats, 2 per seat); to free one, disconnect a host (`lean-ctx cloud disconnect` on that machine), or upgrade the plan; every seat you add in the Cloud brings its own allowance"
);
assert!(!error.is_transient());
let personal = CloudError::from_response(
409,
&json!({"error": {"code": "instance_limit", "message": "x", "resource": "connected_instances",
"limit": 2, "used": 2, "plan": "personal", "next_step": "upgrade_team"}}),
);
assert!(
personal
.to_string()
.ends_with("; Team gives every seat its own allowance"),
"{personal}"
);
}
#[test]
fn storage_full_decodes_with_bytes_and_is_not_retried() {
let error = CloudError::from_response(
403,
&json!({"error": {"code": "storage_quota_exceeded", "message": "x", "resource": "storage_bytes",
"limit": 1_073_741_824_u64, "used": 1_073_741_824_u64,
"limit_bytes": 1_073_741_824_u64, "used_bytes": 1_073_741_824_u64,
"plan": "personal", "next_step": "upgrade_team"}}),
);
assert_eq!(
error,
CloudError::StorageFull(Box::new(StorageFull {
limit: Some(1 << 30),
used: Some(1 << 30),
next_step: Some(NextStep::UpgradeTeam),
}))
);
assert!(!error.is_transient(), "waiting does not free storage");
assert_eq!(
error.retry_after_at(lean_ctx_time::UtcDateTime::now()),
None
);
assert_eq!(
error.to_string(),
"this workspace's Cloud storage is full (1 GiB of 1 GiB); nothing is charged for more; uploads stop until storage frees (remove vaults or files in the Cloud); Team gives every seat its own allowance"
);
assert!(!error.to_string().contains("unknown"));
let bare = CloudError::from_response(
403,
&json!({"error": {"code": "storage_quota_exceeded", "message": "x", "limit": 1536, "used": 2048,
"next_step": "something_new"}}),
);
assert_eq!(
bare,
CloudError::StorageFull(Box::new(StorageFull {
limit: Some(1536),
used: Some(2048),
next_step: None,
}))
);
assert!(bare.to_string().contains("(2 KiB of 1.5 KiB)"), "{bare}");
}
#[test]
fn upload_rate_limited_is_transient_and_honours_retry_after() {
let error = CloudError::from_response(
429,
&json!({"error": {"code": "upload_rate_limited", "message": "x",
"limit_bytes_per_hour": 268_435_456_u64, "plan": "personal",
"next_step": "wait", "retry_after_s": 1200}}),
);
assert_eq!(
error,
CloudError::UploadRateLimited {
retry_after: Some(1200),
per_hour: Some(256 << 20),
}
);
assert!(error.is_transient());
assert_eq!(
error.retry_after_at(lean_ctx_time::UtcDateTime::now()),
Some(1200)
);
assert_eq!(
error.to_string(),
"The Cloud's upload allowance for this hour is used up (256 MiB per hour); the rest goes out in 1200 s (a large first sync can take a few hours)"
);
let huge = CloudError::from_response(
429,
&json!({"error": {"code": "upload_rate_limited", "message": "x", "retry_after_s": u64::MAX}}),
);
assert_eq!(
huge.retry_after_at(lean_ctx_time::UtcDateTime::now()),
Some(RETRY_AFTER_CAP_S)
);
}
#[test]
fn quota_hints_and_new_resource_labels() {
let quota = |resource: &str, limit: u64, next: &str| {
CloudError::from_response(
409,
&json!({"error": {"code": "quota_exceeded", "message": "x", "resource": resource,
"limit": limit, "used": limit, "plan": "personal", "next_step": next}}),
)
.to_string()
};
assert_eq!(
quota("storage_bytes", 1 << 30, "upgrade_team"),
"this workspace has used all its storage (1 GiB of 1 GiB); nothing is charged for more; Team gives every seat its own allowance"
);
assert_eq!(
quota("model_credit_usd_micros", 8_000_000, "top_up"),
"this workspace has used all its model credit ($8.00 of $8.00); nothing is charged for more; a top-up in the Cloud adds more"
);
assert!(
quota("registered_instances", 3, "contact")
.ends_with("; contact us to extend your Enterprise contract")
);
assert_eq!(
quota("registered_instances", 3, "upgrade_team"),
"all hosts the plan includes are registered (3 of 3); nothing is charged for more; \
to free one, revoke a host you no longer use on the Hosts page, or upgrade the plan; \
Team gives every seat its own allowance"
);
assert_eq!(
quota("connected_instances", 2, "wait"),
"all hosts that may be connected at once are connected (2 of 2); \
nothing is charged for more; to free one, disconnect a host (`lean-ctx cloud disconnect` on that machine)"
);
for resource in ["registered_instances", "connected_instances"] {
let line = quota(resource, 2, "upgrade_team");
for word in ["instance", "slot", "workspace"] {
assert!(!line.contains(word), "{word} in {line}");
}
}
let monthly = CloudError::from_response(
429,
&json!({"error": {"code": "quota_exceeded", "message": "x", "resource": "bus_messages",
"limit": 200_000, "used": 200_000, "next_step": "wait",
"reset_at": "2026-12-01T00:00:00Z", "retry_after_s": 60}}),
);
assert_eq!(
monthly.to_string(),
"this workspace has used all its monthly bus messages (200000 of 200000); nothing is charged for more; uploads wait in the local outbox until 2026-12-01T00:00:00Z"
);
let feature = CloudError::from_response(
402,
&json!({"error": {"code": "plan_required", "message": "", "feature": "audit_export",
"plan_needed": "team", "next_step": "upgrade_team"}}),
);
assert_eq!(
feature.to_string(),
"audit export is included in Team and Enterprise; Team gives every seat its own allowance"
);
let locked = CloudError::from_response(
402,
&json!({"error": {"code": "plan_required", "message": "x", "feature": "read_only",
"reason": "trial_ended", "plan": "locked", "next_step": "subscribe",
"plan_needed": "personal"}}),
);
assert_eq!(
locked.to_string(),
"Your Personal trial has ended: this workspace is locked (read and export only). Choose a plan in the Cloud to sync again."
);
}
#[test]
fn bytes_are_worded_in_binary_units() {
assert_eq!(bytes_text(0), "0 B");
assert_eq!(bytes_text(1023), "1023 B");
assert_eq!(bytes_text(256 << 20), "256 MiB");
assert_eq!(bytes_text(40 << 30), "40 GiB");
assert_eq!(bytes_text(3 << 29), "1.5 GiB");
assert_eq!(bytes_text(u64::MAX), "16777215.9 TiB");
}
#[test]
fn a_huge_retry_after_is_capped_on_the_way_in() {
for huge in [u64::MAX, i64::MAX.unsigned_abs(), RETRY_AFTER_CAP_S + 1] {
let error = CloudError::from_response(
429,
&json!({"error": {"code": "rate_limited", "message": "x", "retry_after_s": huge}}),
);
assert_eq!(
error,
CloudError::RateLimited {
retry_after: Some(RETRY_AFTER_CAP_S)
}
);
}
let small = CloudError::from_response(
429,
&json!({"error": {"code": "rate_limited", "message": "x", "retry_after_s": 90}}),
);
assert_eq!(
small,
CloudError::RateLimited {
retry_after: Some(90)
}
);
}
#[test]
fn upload_disabled_names_the_kind_and_is_not_retried() {
let error = CloudError::from_response(
403,
&json!({"error": {"code": "upload_disabled", "message": "x", "kind": "metrics"}}),
);
assert_eq!(error, CloudError::UploadDisabled(CloudUploadKind::Metrics));
assert!(!error.is_transient());
assert!(error.to_string().contains("metrics upload off"));
}
#[test]
fn task_reasons_become_task_refusals_and_others_stay() {
let refusal = |status, code: &str, reason: &str| {
CloudError::from_response(
status,
&json!({"error": {"code": code, "message": "x", "reason": reason, "number": 7, "retry_after_s": 60}}),
)
};
assert_eq!(
refusal(409, "invalid_state", "stale_claim"),
CloudError::Task {
refusal: TaskRefusal::StaleClaim,
number: Some(7),
detail: None
}
);
assert_eq!(
refusal(403, "forbidden_right", "optin_scope"),
CloudError::Task {
refusal: TaskRefusal::OptinScope,
number: Some(7),
detail: None
}
);
let permit_said = |check: &str, agent_role: Option<&str>, message: &str| {
let mut e = json!({"error": {"code": "forbidden_right", "message": message,
"reason": "not_permitted", "number": 7, "check": check,
"role": "backend"}});
if let Some(r) = agent_role {
e["error"]["agent_role"] = r.into();
}
CloudError::from_response(403, &e).to_string()
};
let permit = |check: &str, agent_role: Option<&str>| permit_said(check, agent_role, "");
assert_eq!(
permit("presence", None),
"task #7: this agent is not on the bus: its session (or the relay) must be running and announce it with the task's role (the task needs role backend) [check: presence] Run `lean-ctx cloud bus relay --for 30`: it says who this machine announced, or why an agent is missing. The Cloud keeps an agent present only while a relay stays connected (the daemon's), so claim while it runs."
);
assert_eq!(
permit("role", Some("web")),
"task #7: this agent is on the bus in another role than the task needs (the agent works as web, the task needs backend) [check: role]"
);
assert!(
permit("role", Some("web\u{1b}[31m"))
.ends_with("(the task needs role backend) [check: role]"),
"a role that is not a bus identifier is not printed"
);
assert_eq!(
permit_said(
"presence",
None,
"Agent coder-1 is not on the bus: this task needs it present with role backend. Start its session (or the relay) and claim again."
),
"task #7: Agent coder-1 is not on the bus: this task needs it present with role backend. Start its session (or the relay) and claim again. [check: presence] Run `lean-ctx cloud bus relay --for 30`: it says who this machine announced, or why an agent is missing. The Cloud keeps an agent present only while a relay stays connected (the daemon's), so claim while it runs."
);
assert_eq!(
permit_said(
"role",
Some("web"),
"Agent coder-1 works as role web;\u{1b}[31m this task needs role backend."
),
"task #7: Agent coder-1 works as role web; [31m this task needs role backend. [check: role]",
"control characters of the service's message never reach the terminal"
);
assert_eq!(
permit_said("", None, "Agent coder-1 may not take this task."),
"task #7: Agent coder-1 may not take this task."
);
let inactive = refusal(409, "invalid_state", "optin_inactive");
assert!(matches!(inactive, CloudError::Task { refusal, .. } if refusal.ends_claim()));
assert!(matches!(
refusal(409, "invalid_state", "optin_changed"),
CloudError::Task {
refusal: TaskRefusal::OptinChanged,
..
}
));
let budget = refusal(429, "rate_limited", "daily_budget");
assert_eq!(
budget,
CloudError::Task {
refusal: TaskRefusal::DailyBudget {
retry_after: Some(60)
},
number: Some(7),
detail: None
}
);
assert!(!budget.is_transient(), "a used-up budget is not retried");
assert!(
budget
.to_string()
.starts_with("task #7: today's task budget")
);
for (fixture, expected) in [
("error.optin_scope.json", TaskRefusal::OptinScope),
("error.optin_changed.json", TaskRefusal::OptinChanged),
("error.optin_inactive.json", TaskRefusal::OptinInactive),
(
"error.daily_budget.json",
TaskRefusal::DailyBudget {
retry_after: Some(43200),
},
),
("error.task_claimed.json", TaskRefusal::Claimed),
(
"error.task_not_permitted.json",
TaskRefusal::NotPermitted(PermitCheck::Unknown),
),
(
"error.task_not_permitted_role.json",
TaskRefusal::NotPermitted(PermitCheck::Role),
),
] {
let path = format!(
"{}/crates/lean-ctx-protocol/tests/fixtures/cloud/{fixture}",
concat!(env!("CARGO_MANIFEST_DIR"), "/../..")
);
let body: serde_json::Value =
serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
let code: CloudErrorCode =
serde_json::from_value(body["error"]["code"].clone()).unwrap();
assert!(
matches!(
CloudError::from_response(code.http_status(), &body),
CloudError::Task { refusal, .. } if refusal == expected
),
"{fixture}"
);
}
for (fixture, expected) in [
(
"task_verify.error.self_verify.json",
TaskRefusal::SelfVerify,
),
(
"task_verify.error.not_submitted.json",
TaskRefusal::NotSubmitted,
),
] {
let path = format!(
"{}/crates/lean-ctx-protocol/tests/fixtures/cloud-v2/routes/{fixture}",
concat!(env!("CARGO_MANIFEST_DIR"), "/../..")
);
let body: serde_json::Value =
serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
let code: CloudErrorCode =
serde_json::from_value(body["error"]["code"].clone()).unwrap();
assert!(
matches!(
CloudError::from_response(code.http_status(), &body),
CloudError::Task { refusal, .. } if refusal == expected
),
"{fixture}"
);
}
assert_eq!(
refusal(403, "forbidden_right", "vetoed"),
CloudError::Vetoed
);
assert_eq!(
refusal(409, "invalid_state", "code_graph_disabled"),
CloudError::Server {
status: 409,
code: "invalid_state".into()
},
"a reason of another section keeps its old meaning"
);
}
fn plan_required_example(file: &str, feature: &str) -> (u16, serde_json::Value) {
let path = format!(
"{}/crates/lean-ctx-protocol/tests/fixtures/cloud-v2/web/{file}",
concat!(env!("CARGO_MANIFEST_DIR"), "/../..")
);
let fixture: serde_json::Value =
serde_json::from_slice(&std::fs::read(&path).expect(&path)).expect(&path);
let example = fixture["errors"]
.as_array()
.expect("errors")
.iter()
.find(|e| {
e["body"]["error"]["code"] == "plan_required"
&& e["body"]["error"]["feature"] == feature
})
.unwrap_or_else(|| panic!("{file}: no plan_required {feature}"));
(
u16::try_from(example["status"].as_u64().unwrap()).unwrap(),
example["body"].clone(),
)
}
#[test]
fn plan_required_keeps_the_feature_and_words_it() {
let (status, body) = plan_required_example("vault-transfer.json", "repos");
assert_eq!(status, 402);
let error = CloudError::from_response(status, &body);
assert!(
matches!(&error, CloudError::PlanRequired(need) if need.feature.as_deref() == Some("repos"))
);
assert_eq!(
error.clone().with_plan(CloudPlan::Unknown),
error,
"an unknown plan is not named"
);
assert!(!error.is_transient());
let repos = error.with_plan(CloudPlan::NoPlan);
assert!(!repos.to_string().contains("engine"));
assert_eq!(
repos.to_string(),
"this workspace has used all its vaults (3 of 3); more are included in Personal, Team and Enterprise; this workspace has no plan; remove or publish a vault in the Cloud, or upgrade the plan"
);
let (status, body) = plan_required_example("organisation-create.json", "company");
let org = CloudError::from_response(status, &body).with_plan(CloudPlan::Team);
assert_eq!(
org.to_string(),
"A company comes with Enterprise, on request: send a request and we set it up with you; this workspace is on Team"
);
let unnamed = CloudError::from_response(
402,
&json!({"error": {"code": "plan_required", "message": "", "feature": "audit_export", "plan_needed": "team"}}),
);
assert_eq!(
unnamed.to_string(),
"audit export is included in Team and Enterprise"
);
let hostile = CloudError::from_response(
402,
&json!({"error": {"code": "plan_required", "message": "x\u{1b}[2J", "feature": "Comp\u{1b}"}}),
);
assert_eq!(
hostile,
CloudError::PlanRequired(Box::new(PlanNeed {
message: Some("x [2J".into()),
..PlanNeed::default()
})),
"a feature that is not a wire token is dropped, the message is terminal-safe"
);
}
#[test]
fn plan_required_without_a_feature_keeps_the_1x_seat_text() {
let path = format!(
"{}/crates/lean-ctx-protocol/tests/fixtures/cloud/error.plan_required.json",
concat!(env!("CARGO_MANIFEST_DIR"), "/../..")
);
let body: serde_json::Value =
serde_json::from_slice(&std::fs::read(&path).expect(&path)).expect(&path);
let error = CloudError::from_response(402, &body).with_plan(CloudPlan::NoPlan);
assert_eq!(
error.to_string(),
"your plan does not include the Cloud on this machine (Personal up to 3 hosts registered, up to 2 connected at once; Team and Enterprise up to 3 hosts registered, up to 2 connected at once per seat)"
);
assert!(
matches!(&error, CloudError::PlanRequired(need) if need.plan.is_none()),
"a 1.x refusal is about the Cloud itself, not a feature of the plan"
);
}
#[test]
fn a_locked_workspace_is_one_line_whatever_the_feature() {
let locked = |reason: &str, feature: Option<&str>, needed: &str| {
let mut body = json!({"error": {"code": "plan_required", "message": "Your trial has ended: this workspace is locked.",
"plan_needed": needed, "reason": reason}});
if let Some(f) = feature {
body["error"]["feature"] = f.into();
}
CloudError::from_response(402, &body)
};
let trial = "Your Personal trial has ended: this workspace is locked (read and export only). Choose a plan in the Cloud to sync again.";
for feature in [None, Some("read_only")] {
let e = locked("trial_ended", feature, "personal");
assert_eq!(e.to_string(), trial);
assert!(matches!(&e, CloudError::PlanRequired(n) if n.locked() == Some("trial_ended")));
assert_eq!(
e.with_plan(CloudPlan::Personal).to_string(),
trial,
"the lease's plan adds nothing to the line"
);
}
assert_eq!(
locked("plan_ended", Some("read_only"), "team").to_string(),
"Your plan has ended: this workspace is locked (read and export only). Choose a plan in the Cloud to sync again."
);
assert_eq!(
locked("trial_ended", None, "team").to_string(),
"Your Team trial has ended: this workspace is locked (read and export only). Choose a plan in the Cloud to sync again."
);
let no_plan = locked("no_plan", None, "personal");
assert!(
no_plan.to_string().contains("2 connected at once"),
"{no_plan}"
);
assert!(!no_plan.to_string().contains(LOCKED_MARK));
}
#[test]
fn non_contract_bodies_become_server_errors() {
assert_eq!(
CloudError::from_response(502, &json!("<html>")),
CloudError::Server {
status: 502,
code: "unknown".into()
}
);
assert!(
CloudError::Server {
status: 502,
code: "unknown".into()
}
.is_transient()
);
}
#[test]
fn a_role_that_may_not_add_a_vault_is_forbidden() {
let body = json!({"error": {
"code": "vault_create_forbidden",
"message": "Only an owner, admin or maintainer of this team can add a new vault.",
"role_needed": "maintainer",
}});
assert_eq!(CloudError::from_response(403, &body), CloudError::Forbidden);
assert_ne!(CloudError::from_response(500, &body), CloudError::Forbidden);
}
}