use aidaemon_core::mandate_card::MandateConfirmationCard;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AutomationKind {
Scheduled,
Watching,
Probe,
}
impl AutomationKind {
pub const ALL: [Self; 3] = [Self::Scheduled, Self::Watching, Self::Probe];
pub const fn as_str(self) -> &'static str {
match self {
Self::Scheduled => "scheduled",
Self::Watching => "watching",
Self::Probe => "probe",
}
}
pub fn parse(value: &str) -> Option<Self> {
Self::ALL
.into_iter()
.find(|kind| kind.as_str() == value.trim())
}
pub const fn label(self) -> &'static str {
match self {
Self::Scheduled => "Scheduled",
Self::Watching => "Watching",
Self::Probe => "Health check",
}
}
}
pub const NAME_KEY: &str = "automation_name";
pub fn name_from_context(context: Option<&str>) -> Option<String> {
let value: serde_json::Value = serde_json::from_str(context?).ok()?;
let name = value.get(NAME_KEY)?.as_str()?.trim();
(!name.is_empty()).then(|| name.to_string())
}
pub fn context_with_name(context: Option<&str>, name: &str) -> String {
let mut object = context
.and_then(|raw| serde_json::from_str::<serde_json::Value>(raw).ok())
.and_then(|value| value.as_object().cloned())
.unwrap_or_default();
object.insert(NAME_KEY.to_string(), serde_json::json!(name.trim()));
serde_json::Value::Object(object).to_string()
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum NotifyMode {
#[default]
EveryRun,
Changes,
Problems,
}
pub fn run_mission(description: &str, context: Option<&str>, previous: Option<&str>) -> String {
match quiet_run_note(context, previous) {
Some(note) => format!("{description}\n\n{note}"),
None => description.to_string(),
}
}
pub fn quiet_run_note(context: Option<&str>, previous: Option<&str>) -> Option<String> {
if NotifyMode::from_context(context) != NotifyMode::Changes {
return None;
}
let since = match previous.map(str::trim).filter(|text| !text.is_empty()) {
Some(previous) => format!(
"The previous run reported: \"{}\". Report only what changed since then; when \
nothing did, finish with manage_goal_tasks complete_goal and nothing_new: true, \
and answer in one line.",
previous.chars().take(1500).collect::<String>()
),
None => "This is its first run: report what you find.".to_string(),
};
Some(format!("[SYSTEM: {since}]"))
}
pub fn spend_line(
tokens_used_today: i64,
tokens_used_day: &str,
today: &str,
budget_per_check: Option<i64>,
budget_daily: Option<i64>,
) -> Option<String> {
let used = if tokens_used_day == today {
tokens_used_today.max(0)
} else {
0
};
let tokens = |n: i64| match n {
n if n >= 1_000_000 => format!("{:.1}M", n as f64 / 1_000_000.0),
n if n >= 1_000 => format!("{}k", n / 1_000),
n => n.to_string(),
};
let mut parts = vec![format!("{} tokens today", tokens(used))];
if let Some(daily) = budget_daily.filter(|n| *n > 0) {
parts[0] = format!("{} of {} tokens today", tokens(used), tokens(daily));
}
if let Some(per) = budget_per_check.filter(|n| *n > 0) {
parts.push(format!("up to {} per run", tokens(per)));
}
(used > 0 || budget_daily.is_some() || budget_per_check.is_some())
.then(|| format!("Spend: {}", parts.join(", ")))
}
#[cfg(test)]
mod spend_tests {
#[test]
fn spend_reads_as_todays_tokens_against_the_limits() {
assert_eq!(
super::spend_line(
12_400,
"2026-11-15",
"2026-11-15",
Some(50_000),
Some(200_000)
)
.as_deref(),
Some("Spend: 12k of 200k tokens today, up to 50k per run")
);
assert_eq!(
super::spend_line(12_400, "2026-11-14", "2026-11-15", None, Some(200_000)).as_deref(),
Some("Spend: 0 of 200k tokens today")
);
assert_eq!(super::spend_line(0, "", "2026-11-15", None, None), None);
}
}
pub const NOTHING_NEW_RUN_KEY: &str = "nothing_new_run";
pub fn run_found_nothing_new(context: Option<&str>, run_id: &str) -> bool {
context
.and_then(|raw| serde_json::from_str::<serde_json::Value>(raw).ok())
.and_then(|context| {
context
.get(NOTHING_NEW_RUN_KEY)
.and_then(serde_json::Value::as_str)
.map(|recorded| recorded == run_id)
})
.unwrap_or(false)
}
impl NotifyMode {
pub const KEY: &'static str = "notify";
pub fn parse(raw: &str) -> Option<Self> {
match raw {
"every_run" => Some(Self::EveryRun),
"changes" => Some(Self::Changes),
"problems" => Some(Self::Problems),
_ => None,
}
}
pub const fn as_str(self) -> &'static str {
match self {
Self::EveryRun => "every_run",
Self::Changes => "changes",
Self::Problems => "problems",
}
}
pub fn from_context(context: Option<&str>) -> Self {
context
.and_then(|raw| serde_json::from_str::<serde_json::Value>(raw).ok())
.and_then(|context| {
context
.get(Self::KEY)
.and_then(serde_json::Value::as_str)
.and_then(Self::parse)
})
.unwrap_or_default()
}
pub fn tells(self, notification_type: &str, nothing_new: bool) -> bool {
let done = notification_type == "completed";
match self {
Self::EveryRun => true,
Self::Changes => !(done && nothing_new),
Self::Problems => !done,
}
}
pub const fn words(self) -> &'static str {
match self {
Self::EveryRun => "every run",
Self::Changes => "only when something changes",
Self::Problems => "only about problems",
}
}
}
#[cfg(test)]
mod notify_tests {
use super::NotifyMode;
#[test]
fn each_mode_tells_what_the_owner_chose() {
assert!(NotifyMode::EveryRun.tells("completed", true));
assert!(!NotifyMode::Changes.tells("completed", true));
assert!(NotifyMode::Changes.tells("completed", false));
assert!(NotifyMode::Changes.tells("failed", true));
assert!(!NotifyMode::Problems.tells("completed", false));
assert!(NotifyMode::Problems.tells("failed", false));
assert_eq!(
NotifyMode::from_context(Some(r#"{"notify":"changes"}"#)),
NotifyMode::Changes
);
assert_eq!(NotifyMode::from_context(None), NotifyMode::EveryRun);
}
#[test]
fn a_quiet_automations_task_lead_is_told_how_to_say_nothing_new() {
let changes = Some(r#"{"notify":"changes"}"#);
let quiet = super::run_mission("Read the synthetic file", changes, Some("v1"));
assert!(quiet.starts_with("Read the synthetic file"));
assert!(
quiet.contains("The previous run reported: \"v1\""),
"{quiet}"
);
assert!(
quiet.contains("complete_goal and nothing_new: true"),
"{quiet}"
);
let first = super::run_mission("Read the synthetic file", changes, None);
assert!(first.contains("first run"), "{first}");
assert_eq!(
super::run_mission("Read the synthetic file", None, Some("v1")),
"Read the synthetic file"
);
}
#[test]
fn nothing_new_is_known_for_the_run_that_said_it() {
let context = Some(r#"{"nothing_new_run":"run-2"}"#);
assert!(super::run_found_nothing_new(context, "run-2"));
assert!(!super::run_found_nothing_new(context, "run-3"));
assert!(!super::run_found_nothing_new(None, "run-2"));
}
}
pub fn run_summary_words(summary: &str) -> String {
let summary = summary.trim();
let is_code = !summary.is_empty()
&& summary
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || matches!(c, '_' | ':' | '-'));
if !is_code {
return summary.to_string();
}
let words = |code: &str| code.replace(['_', '-'], " ");
let (head, detail) = match summary.split_once(':') {
Some((head, detail)) => (head, Some(words(detail))),
None => (summary, None),
};
let with = |text: &str| match &detail {
Some(detail) => format!("{text} ({detail})."),
None => format!("{text}."),
};
match head {
"mandate_act_satisfied" => with("Acted, and the result checked out"),
"mandate_non_action_satisfied" => with("Checked; nothing needed doing"),
"mandate_review_failed" => with("The review didn't finish"),
"mandate_reconciliation_required" => with("Its last action needs checking"),
other => {
let mut text = words(other);
if let Some(first) = text.get_mut(..1) {
first.make_ascii_uppercase();
}
with(&text)
}
}
}
#[cfg(test)]
mod run_summary_tests {
use super::run_summary_words;
#[test]
fn typed_run_reasons_read_as_words() {
assert_eq!(
run_summary_words("mandate_act_satisfied"),
"Acted, and the result checked out."
);
assert_eq!(
run_summary_words("mandate_non_action_satisfied"),
"Checked; nothing needed doing."
);
assert_eq!(
run_summary_words("mandate_reconciliation_required:act_missing_work_task"),
"Its last action needs checking (act missing work task)."
);
assert_eq!(run_summary_words("budget_exhausted"), "Budget exhausted.");
assert_eq!(
run_summary_words("Published the synthetic diary entry"),
"Published the synthetic diary entry"
);
}
}
pub const HELD_AFTER_FAILURES: &str = "held_after_failures";
pub fn state_words(status: &str) -> &'static str {
match status {
"active" => "On",
"paused" => "Paused",
"pending_confirmation" | "pending" => "Waiting for your approval",
"awaiting_input" => "Waiting for your answer",
"failed" => "Stopped after a problem",
HELD_AFTER_FAILURES => "Paused after repeated failures",
"completed" => "Done",
"cancelled" | "abandoned" => "Cancelled",
_ => "Unknown",
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScheduleSpec {
pub cron: String,
pub zone: Option<String>,
pub one_shot: bool,
pub next: Option<String>,
pub following: Vec<String>,
}
fn clock(minute: &str, hour: &str, zone: &str) -> Option<String> {
let minute: u32 = minute.parse().ok()?;
let hour: u32 = hour.parse().ok()?;
let time = chrono::NaiveTime::from_hms_opt(hour, minute, 0)?;
Some(format!("{} {zone}", time.format("%-I:%M %p")))
}
fn weekday(day: &str) -> Option<&'static str> {
Some(match day.parse::<u8>().ok()? {
0 | 7 => "Sunday",
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
6 => "Saturday",
_ => return None,
})
}
fn plural(count: u32, unit: &str) -> String {
if count == 1 {
unit.to_string()
} else {
format!("{count} {unit}s")
}
}
pub fn cadence_words(schedule: &ScheduleSpec, zone: &str) -> String {
let fallback = || match &schedule.next {
Some(next) => format!("On a fixed schedule; next {next}"),
None => "On a fixed schedule".to_string(),
};
if schedule.one_shot {
return match &schedule.next {
Some(next) => format!("Once, {next}"),
None => "Once".to_string(),
};
}
let zone = schedule.zone.as_deref().unwrap_or(zone);
let fields: Vec<&str> = schedule.cron.split_whitespace().collect();
let [minute, hour, dom, month, dow] = fields.as_slice() else {
return fallback();
};
let every = |count: &str, unit: &str| {
count
.strip_prefix("*/")
.and_then(|n| n.parse::<u32>().ok())
.map(|n| format!("Every {}", plural(n, unit)))
};
match (*minute, *hour, *dom, *month, *dow) {
("*", "*", "*", "*", "*") => "Every minute".to_string(),
(m, "*", "*", "*", "*") if m.starts_with("*/") => {
every(m, "minute").unwrap_or_else(fallback)
}
(m, "*", "*", "*", "*") if m.parse::<u32>().is_ok() => "Every hour".to_string(),
(m, h, "*", "*", "*") if m.parse::<u32>().is_ok() && h.starts_with("*/") => {
every(h, "hour").unwrap_or_else(fallback)
}
(m, h, d, "*", w) => {
let Some(at) = clock(m, h, zone) else {
return fallback();
};
match (d, w) {
("*", "*") => format!("Every day at {at}"),
("*", "1-5") => format!("Every weekday at {at}"),
("*", "0,6" | "6,0" | "6,7" | "6-7") => format!("Every weekend at {at}"),
("*", days) => {
let named: Option<Vec<&str>> = days.split(',').map(weekday).collect();
match named {
Some(named) if !named.is_empty() => {
format!("Every {} at {at}", named.join(" and "))
}
_ => fallback(),
}
}
(day, "*") if day.parse::<u32>().is_ok_and(|d| (1..=31).contains(&d)) => {
format!("Every month on day {day} at {at}")
}
_ => fallback(),
}
}
_ => fallback(),
}
}
pub fn cadence(schedules: &[ScheduleSpec], zone: &str) -> String {
if schedules.is_empty() {
return "No schedule left – it will not run".to_string();
}
schedules
.iter()
.map(|schedule| cadence_words(schedule, zone))
.collect::<Vec<_>>()
.join("; ")
}
pub fn scheduled_card(
what: &str,
reminder: Option<&str>,
schedules: &[ScheduleSpec],
zone: &str,
) -> MandateConfirmationCard {
let runs_once = !schedules.is_empty() && schedules.iter().all(|s| s.one_shot);
let (what, can_do, tells_you_when) = match reminder {
Some(message) => (
format!("Reminds you: {}", message.trim()),
"Only sends you this message – nothing else runs".to_string(),
"it goes off".to_string(),
),
None => (
what.split_whitespace().collect::<Vec<_>>().join(" "),
"Anything you can do – it runs with your full access".to_string(),
"a run finishes, or it hits a problem".to_string(),
),
};
MandateConfirmationCard {
what,
how_often: cadence(schedules, zone),
can_do,
tells_you_when,
ends: if runs_once {
"After it runs once".to_string()
} else {
"Only when you stop it".to_string()
},
details: schedules
.iter()
.flat_map(|schedule| {
schedule.next.iter().flat_map(|next| {
let runs: Vec<&String> = std::iter::once(next)
.chain(schedule.following.iter().filter(|_| !schedule.one_shot))
.take(3)
.collect();
let single = runs.len() == 1;
runs.into_iter().enumerate().map(move |(index, run)| {
if single {
format!("Next: {run}")
} else {
format!("Run {}: {run}", index + 1)
}
})
})
})
.collect(),
}
}
pub fn probe_card(kind: &str, target: &str, cron: &str, zone: &str) -> MandateConfirmationCard {
let schedule = ScheduleSpec {
cron: cron.to_string(),
zone: None,
one_shot: false,
next: None,
following: Vec::new(),
};
MandateConfirmationCard {
what: format!("Checks {} ({kind})", target.trim()),
how_often: cadence_words(&schedule, zone),
can_do: "Only runs the check – makes no changes".to_string(),
tells_you_when: "the check keeps failing".to_string(),
ends: "Only when you stop it".to_string(),
details: Vec::new(),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn recurring(cron: &str) -> ScheduleSpec {
ScheduleSpec {
cron: cron.to_string(),
zone: None,
one_shot: false,
next: Some("Thu, Nov 13, 8:00 AM ET".to_string()),
following: Vec::new(),
}
}
#[test]
fn cadence_reads_in_plain_words_in_the_owners_zone() {
let cases = [
("0 8 * * 1-5", "Every weekday at 8:00 AM ET"),
("30 18 * * *", "Every day at 6:30 PM ET"),
("0 9 * * 1", "Every Monday at 9:00 AM ET"),
("0 9 * * 1,3", "Every Monday and Wednesday at 9:00 AM ET"),
("0 9 1 * *", "Every month on day 1 at 9:00 AM ET"),
("*/15 * * * *", "Every 15 minutes"),
("0 */2 * * *", "Every 2 hours"),
("0 * * * *", "Every hour"),
(
"0 9 * 1 *",
"On a fixed schedule; next Thu, Nov 13, 8:00 AM ET",
),
];
for (cron, words) in cases {
assert_eq!(cadence_words(&recurring(cron), "ET"), words, "{cron}");
}
let once = ScheduleSpec {
cron: "0 9 13 11 *".to_string(),
zone: None,
one_shot: true,
next: Some("Thu, Nov 13, 9:00 AM ET".to_string()),
following: Vec::new(),
};
assert_eq!(cadence_words(&once, "ET"), "Once, Thu, Nov 13, 9:00 AM ET");
}
#[test]
fn a_scheduled_card_has_the_five_lines_of_a_watching_one() {
let work = scheduled_card(
"Summarize my inbox",
None,
&[recurring("0 8 * * 1-5")],
"ET",
);
assert_eq!(
work.clone().into_lines(),
vec![
"What: Summarize my inbox",
"How often: Every weekday at 8:00 AM ET",
"Can do: Anything you can do – it runs with your full access",
"Tells you when: a run finishes, or it hits a problem",
"Ends: Only when you stop it",
"Next: Thu, Nov 13, 8:00 AM ET",
]
);
assert_eq!(
MandateConfirmationCard::from_lines(&work.into_lines()).map(|card| card.what),
Some("Summarize my inbox".to_string())
);
let reminder = scheduled_card(
"Check the oven.",
Some("Check the oven."),
&[ScheduleSpec {
cron: "5 14 12 11 *".to_string(),
zone: None,
one_shot: true,
next: Some("2:05 PM ET".to_string()),
following: Vec::new(),
}],
"ET",
);
assert_eq!(reminder.what, "Reminds you: Check the oven.");
assert_eq!(
reminder.can_do,
"Only sends you this message – nothing else runs"
);
assert_eq!(reminder.ends, "After it runs once");
}
#[test]
fn scheduled_card_shows_two_following_runs_but_not_for_one_shot() {
let recurring = ScheduleSpec {
cron: "0 8 * * 1-5".to_string(),
zone: None,
one_shot: false,
next: Some("Mon, Nov 17, 8:00 AM ET".to_string()),
following: vec![
"Tue, Nov 18, 8:00 AM ET".to_string(),
"Wed, Nov 19, 8:00 AM ET".to_string(),
],
};
let card = scheduled_card("Check status", None, &[recurring], "ET");
assert_eq!(
card.details,
vec![
"Run 1: Mon, Nov 17, 8:00 AM ET",
"Run 2: Tue, Nov 18, 8:00 AM ET",
"Run 3: Wed, Nov 19, 8:00 AM ET"
]
);
let once = ScheduleSpec {
cron: "0 9 13 11 *".to_string(),
zone: None,
one_shot: true,
next: Some("Thu, Nov 13, 9:00 AM ET".to_string()),
following: Vec::new(),
};
assert_eq!(
scheduled_card("Run once", None, &[once], "ET").details,
vec!["Next: Thu, Nov 13, 9:00 AM ET"]
);
}
#[test]
fn the_name_lives_beside_other_context_keys() {
let context = crate::reminders::context_for_message("Check the oven.");
let named = context_with_name(Some(&context), " Oven ");
assert_eq!(name_from_context(Some(&named)).as_deref(), Some("Oven"));
assert_eq!(
crate::reminders::message_from_context(Some(&named)).as_deref(),
Some("Check the oven.")
);
assert_eq!(name_from_context(None), None);
assert_eq!(
name_from_context(Some(&context_with_name(None, "Inbox"))).as_deref(),
Some("Inbox")
);
}
#[test]
fn kinds_parse_from_their_typed_value() {
assert_eq!(
AutomationKind::parse("watching"),
Some(AutomationKind::Watching)
);
assert_eq!(
AutomationKind::parse("scheduled"),
Some(AutomationKind::Scheduled)
);
assert_eq!(AutomationKind::parse("mandate"), None);
}
}