use std::collections::{BTreeMap, HashMap, VecDeque};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
pub const KIND_KEY: &str = "cloud.kind";
pub const THREAD_KEY: &str = "cloud.thread";
pub const NOT_QUEEN: &str = "not_queen";
pub const RATE_LIMITED: &str = "rate_limited";
pub const RATE_PER_MINUTE: usize = 20;
pub const TASK_ID_MAX: usize = 128;
pub const THREAD_MAX: usize = 128;
const MINUTE_MS: i64 = 60_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum QueenKind {
Hello,
Status,
Offer,
Request,
Handoff,
Coordinate,
Conflict,
Escalate,
Ask,
Answer,
Decide,
Consent,
Execute,
Stop,
}
impl QueenKind {
pub const ALL: [Self; 14] = [
Self::Hello,
Self::Status,
Self::Offer,
Self::Request,
Self::Handoff,
Self::Coordinate,
Self::Conflict,
Self::Escalate,
Self::Ask,
Self::Answer,
Self::Decide,
Self::Consent,
Self::Execute,
Self::Stop,
];
pub fn as_str(self) -> &'static str {
match self {
Self::Hello => "hello",
Self::Status => "status",
Self::Offer => "offer",
Self::Request => "request",
Self::Handoff => "handoff",
Self::Coordinate => "coordinate",
Self::Conflict => "conflict",
Self::Escalate => "escalate",
Self::Ask => "ask",
Self::Answer => "answer",
Self::Decide => "decide",
Self::Consent => "consent",
Self::Execute => "execute",
Self::Stop => "stop",
}
}
pub fn parse(raw: &str) -> Option<Self> {
Self::ALL.into_iter().find(|k| k.as_str() == raw)
}
pub fn from_input(raw: Option<&str>) -> Result<Option<Self>, String> {
match raw.map(str::trim) {
None | Some("" | "null") => Ok(None),
Some(name) => Self::parse(name).map(Some).ok_or_else(|| {
format!(
"unknown kind {name:?}: one of {}, or null for an ordinary message",
Self::ALL.map(Self::as_str).join(", ")
)
}),
}
}
pub fn allows_high(self) -> bool {
matches!(self, Self::Conflict | Self::Escalate | Self::Stop)
}
}
pub fn check_task_id(task_id: &str) -> Result<(), String> {
let n = task_id.chars().count();
if (1..=TASK_ID_MAX).contains(&n)
&& !task_id.chars().any(|c| c.is_control() || c.is_whitespace())
{
Ok(())
} else {
Err(format!(
"task_id is 1-{TASK_ID_MAX} characters without blanks or control characters"
))
}
}
pub fn not_queen(kind: QueenKind, sender: &str) -> String {
format!(
"{NOT_QUEEN}: kind {} is posted only by the coordinator session the app registered; {sender} is not one",
kind.as_str()
)
}
pub fn check_thread(thread: &str) -> Result<(), String> {
if line_within(thread, THREAD_MAX) {
Ok(())
} else {
Err(format!(
"thread is 1-{THREAD_MAX} characters without control characters"
))
}
}
pub const QUEEN_ADDRESS: &str = "queen";
pub const NO_QUEEN: &str = "no_queen";
pub fn registered_queen(registry: &lean_ctx::core::agents::AgentRegistry) -> Option<String> {
registry
.list_active(None)
.into_iter()
.filter(|a| {
super::person_rules::Speaker::of_registry(registry, Some(&a.agent_id)).registered_queen
})
.max_by_key(|a| a.last_active)
.map(|a| a.agent_id.clone())
}
pub fn resolve_to(
registry: &lean_ctx::core::agents::AgentRegistry,
to: &str,
) -> Result<String, String> {
if to != QUEEN_ADDRESS {
return Ok(to.to_owned());
}
registered_queen(registry).ok_or_else(|| {
format!(
"{NO_QUEEN}: no registered coordinator session is active on this Instance; `{QUEEN_ADDRESS}` names the coordinator session the app registered, not an agent's role"
)
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SenderRefusal {
NotQueen(String),
RateLimited(String),
}
pub fn check_sender(
registry: &lean_ctx::core::agents::AgentRegistry,
sender: &str,
kind: Option<QueenKind>,
now_ms: i64,
) -> Result<(), SenderRefusal> {
if let Some(kind) = kind
&& !super::person_rules::Speaker::of_registry(registry, Some(sender)).registered_queen
{
return Err(SenderRefusal::NotQueen(not_queen(kind, sender)));
}
let recent = in_last_minute(
registry
.scratchpad
.iter()
.filter(|e| e.from_agent == sender && !e.is_cloud_inbound())
.map(|e| e.timestamp.timestamp_millis()),
now_ms,
);
if recent >= RATE_PER_MINUTE {
return Err(SenderRefusal::RateLimited(rate_limited(sender)));
}
Ok(())
}
pub fn in_last_minute(stamps: impl IntoIterator<Item = i64>, now_ms: i64) -> usize {
stamps
.into_iter()
.filter(|at| (0..MINUTE_MS).contains(&now_ms.saturating_sub(*at)))
.count()
}
pub fn rate_limited(sender: &str) -> String {
format!(
"{RATE_LIMITED}: {sender} posted {RATE_PER_MINUTE} messages in the last minute; try again shortly"
)
}
pub fn frame_fields(
metadata: &HashMap<String, String>,
task_id: Option<&str>,
thread: Option<&str>,
) -> Option<Map<String, Value>> {
let mut fields = Map::new();
if let Some(kind) = metadata.get(KIND_KEY).and_then(|k| QueenKind::parse(k)) {
fields.insert("kind".into(), kind.as_str().into());
}
if let Some(task_id) = task_id.filter(|t| check_task_id(t).is_ok()) {
fields.insert("task_id".into(), task_id.into());
}
if let Some(thread) = thread.filter(|t| line_within(t, THREAD_MAX)) {
fields.insert("thread".into(), thread.into());
}
(!fields.is_empty()).then_some(fields)
}
pub fn delivered_fields(message: &Value) -> (Option<QueenKind>, Option<String>, Option<String>) {
let kind = message["kind"].as_str().and_then(QueenKind::parse);
let task_id = message["task_id"]
.as_str()
.filter(|t| check_task_id(t).is_ok())
.map(str::to_owned);
let thread = message["thread"]
.as_str()
.filter(|t| line_within(t, THREAD_MAX))
.map(str::to_owned);
(kind, task_id, thread)
}
fn line_within(value: &str, max: usize) -> bool {
let n = value.chars().count();
(1..=max).contains(&n) && !value.chars().any(char::is_control)
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RelayBudget {
sent: BTreeMap<String, VecDeque<i64>>,
}
impl RelayBudget {
pub fn take(&mut self, worker: &str, now_ms: i64) -> bool {
let sent = self.sent.entry(worker.to_owned()).or_default();
while sent
.front()
.is_some_and(|at| now_ms.saturating_sub(*at) >= MINUTE_MS)
{
sent.pop_front();
}
if sent.len() >= RATE_PER_MINUTE {
return false;
}
sent.push_back(now_ms);
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn kinds_parse_exactly_and_unknown_is_refused_with_its_reason() {
for kind in QueenKind::ALL {
assert_eq!(QueenKind::from_input(Some(kind.as_str())), Ok(Some(kind)));
}
assert_eq!(QueenKind::from_input(None), Ok(None));
assert_eq!(QueenKind::from_input(Some("null")), Ok(None));
let refused = QueenKind::from_input(Some("Hello")).unwrap_err();
assert!(
refused.contains("unknown kind") && refused.contains("stop"),
"{refused}"
);
}
#[test]
fn relay_budget_refills_after_a_minute_per_worker() {
let mut budget = RelayBudget::default();
for i in 0..RATE_PER_MINUTE {
assert!(budget.take("wkr_a", 1_000 + i64::try_from(i).unwrap()));
}
assert!(!budget.take("wkr_a", 2_000), "budget spent");
assert!(budget.take("wkr_b", 2_000), "another worker has its own");
assert!(
budget.take("wkr_a", 61_000),
"the first send left the window"
);
}
#[test]
fn frame_fields_carry_task_and_thread_with_or_without_a_kind_and_round_trip() {
let mut meta = HashMap::new();
assert_eq!(
frame_fields(&meta, None, None),
None,
"a plain post's frame stays as it was"
);
let fields = frame_fields(&meta, Some("t1"), Some("task-app-3")).unwrap();
let frame = Value::Object(fields);
assert_eq!(
frame,
serde_json::json!({"task_id": "t1", "thread": "task-app-3"})
);
assert_eq!(
delivered_fields(&frame),
(None, Some("t1".into()), Some("task-app-3".into()))
);
meta.insert(KIND_KEY.into(), "coordinate".into());
let frame = Value::Object(frame_fields(&meta, Some("t1"), None).unwrap());
assert_eq!(
frame,
serde_json::json!({"kind": "coordinate", "task_id": "t1"})
);
assert_eq!(
delivered_fields(&frame),
(Some(QueenKind::Coordinate), Some("t1".into()), None)
);
}
}