use crate::split::split_message;
use aidaemon_core::{ApprovalCard, AskReason, CardAction, HighImpact};
use aidaemon_tools::command_risk::{PermissionMode, RiskLevel};
use aidaemon_tools::semantics::{ApprovalResponse, GoalConfirmationStyle};
const MAX_CMD_DISPLAY: usize = 3600;
pub fn approval_use_session_button(permission_mode: PermissionMode, risk_level: RiskLevel) -> bool {
match permission_mode {
PermissionMode::Cautious => true,
PermissionMode::Default => risk_level >= RiskLevel::Critical,
PermissionMode::Yolo => false,
}
}
pub fn risk_header(risk_level: RiskLevel) -> (&'static str, &'static str, &'static str) {
match risk_level {
RiskLevel::Safe => (
"ℹ️",
"New command",
"Your agent wants to run something it hasn't done before.",
),
RiskLevel::Medium => (
"⚠️",
"Approval needed",
"Your agent is waiting for your OK to continue.",
),
RiskLevel::High => (
"🔶",
"Review carefully",
"This action could have significant effects.",
),
RiskLevel::Critical => (
"🚨",
"Dangerous action",
"Review carefully before allowing.",
),
}
}
pub fn response_status(response: &ApprovalResponse) -> (&'static str, &'static str) {
match response {
ApprovalResponse::AllowOnce => ("✅", "Allowed this once"),
ApprovalResponse::AllowSession => ("✅", "Allowed for this session"),
ApprovalResponse::AllowAlways => ("✅", "Always allowed"),
ApprovalResponse::Deny => ("❌", "Denied"),
}
}
pub fn response_status_detail(response: &ApprovalResponse) -> Option<&'static str> {
match response {
ApprovalResponse::AllowSession => Some("You'll be asked again after restart."),
ApprovalResponse::AllowAlways => Some("You won't be asked again for this type of action."),
_ => None,
}
}
pub fn strip_approval_footer(text: &str) -> String {
let mut body = text.trim_end().to_string();
for marker in [
"\n\n\"Allow Session\"",
"\n\n\"Allow Always\"",
"\n\n<i>\"Allow Session\"",
"\n\n<i>\"Allow Always\"",
"\n\n*\"Allow Session\"",
"\n\n*\"Allow Always\"",
"\n\n_\"Allow Session\"",
"\n\n_\"Allow Always\"",
"\n\nThis approval applies only",
"\n\n<i>This approval applies only",
"\n\n*This approval applies only",
"\n\n_This approval applies only",
] {
if let Some(idx) = body.find(marker) {
body.truncate(idx);
break;
}
}
body.trim_end().to_string()
}
pub fn finalize_approval_message(original: &str, response: &ApprovalResponse) -> String {
let body = strip_approval_footer(original);
let (icon, status) = response_status(response);
let mut text = format!("{body}\n\n{icon} {status}");
if let Some(detail) = response_status_detail(response) {
text.push_str(&format!("\n{detail}"));
}
text
}
fn truncate_command(command: &str) -> String {
if command.len() > MAX_CMD_DISPLAY {
let end = aidaemon_tools::text::floor_char_boundary(command, MAX_CMD_DISPLAY);
format!(
"{}...\n[truncated — {} chars total]",
&command[..end],
command.len()
)
} else {
command.to_string()
}
}
pub fn build_approval_message_markdown(
command: &str,
risk_level: RiskLevel,
warnings: &[String],
) -> String {
let display_cmd = truncate_command(command).replace('`', "\\`");
let (risk_icon, risk_label, risk_subtitle) = risk_header(risk_level);
let mut text = format!(
"{} **{}**\n{}\n\n**Requested action**\n```\n{display_cmd}\n```",
risk_icon, risk_label, risk_subtitle
);
if !warnings.is_empty() {
text.push('\n');
for warning in warnings {
text.push_str(&format!("\n• {warning}"));
}
}
text
}
#[derive(Debug, Clone, Copy)]
pub struct ApprovalAsk<'a> {
pub command: &'a str,
pub card: Option<&'a ApprovalCard>,
pub risk_level: RiskLevel,
pub warnings: &'a [String],
pub permission_mode: PermissionMode,
pub one_time_only: bool,
}
pub const COLLAPSED_FENCE: &str = "details";
const CARD_COMMAND_CHARS: usize = 1_200;
pub fn build_approval_ask_markdown(ask: &ApprovalAsk<'_>) -> String {
match ask.card {
Some(card) => build_card_markdown(card, ask.risk_level, ask.warnings),
None => build_approval_message_markdown(ask.command, ask.risk_level, ask.warnings),
}
}
pub fn build_card_markdown(card: &ApprovalCard, risk: RiskLevel, notes: &[String]) -> String {
let mut lines = Vec::new();
if let Some(warning) = impact_warning(card.impact.as_ref(), risk) {
lines.push(warning);
}
lines.push(format!("**{}**", card_headline(card)));
if let Some(why) = card_why(card) {
lines.push(why);
}
let mut text = lines.join("\n");
let detail = match &card.action {
CardAction::Command { line, .. } => Some(shorten_middle(line, CARD_COMMAND_CHARS)),
CardAction::Use { detail, .. } | CardAction::ClickOnSite { target: detail, .. } => detail
.as_deref()
.map(|detail| shorten_middle(detail, CARD_COMMAND_CHARS)),
_ => None,
};
let notes: Vec<&str> = notes
.iter()
.map(|note| note.trim())
.filter(|note| !note.is_empty())
.collect();
if detail.is_some() || !notes.is_empty() {
let mut block = detail.unwrap_or_default();
for note in notes {
if !block.is_empty() {
block.push_str("\n\n");
}
block.push_str(note);
}
let block = block.replace("```", "'''");
text.push_str(&format!("\n\n```{COLLAPSED_FENCE}\n{block}\n```"));
}
text
}
pub fn card_as_warnings(ask: &ApprovalAsk<'_>) -> Vec<String> {
let mut warnings = ask.warnings.to_vec();
let Some(card) = ask.card else {
return warnings;
};
if let Some(warning) = impact_warning(card.impact.as_ref(), ask.risk_level) {
warnings.push(warning.trim_start_matches("⚠️ ").replace("**", ""));
}
if let Some(AskReason::ReadOutsideContent {
check_says: Some(says),
}) = &card.reason
{
warnings.push(format!("Safety check: {says}"));
}
match &card.action {
CardAction::Command { line, .. } => {
warnings.push(format!(
"Details: {}",
shorten_middle(line, CARD_COMMAND_CHARS)
));
}
CardAction::Use {
detail: Some(detail),
..
} => warnings.push(format!(
"Details: {}",
shorten_middle(detail, CARD_COMMAND_CHARS)
)),
CardAction::ClickOnSite { site, .. } => warnings.push(click_on_site_scope(site)),
_ => {}
}
warnings
}
fn shorten_middle(text: &str, cap: usize) -> String {
let length = text.chars().count();
if length <= cap {
return text.to_string();
}
let tail_chars = cap / 4;
let head: String = text.chars().take(cap - tail_chars).collect();
let tail: String = text.chars().skip(length - tail_chars).collect();
format!("{head} … ({} characters left out) … {tail}", length - cap)
}
fn impact_warning(impact: Option<&HighImpact>, risk: RiskLevel) -> Option<String> {
let line = match impact {
Some(HighImpact::Deletes { targets }) if !targets.is_empty() => {
format!("This deletes {}.", plain_list(targets, 3))
}
Some(HighImpact::Deletes { .. }) => "This deletes files.".to_string(),
Some(HighImpact::Sends) => "This sends something to another service.".to_string(),
Some(HighImpact::Spends) => "This spends money.".to_string(),
Some(HighImpact::WritesOutsideWorkArea) => {
"This writes outside your work area.".to_string()
}
Some(_) => "This has effects beyond your computer.".to_string(),
None if risk >= RiskLevel::Critical => {
"This can change a lot on your computer.".to_string()
}
None => return None,
};
Some(format!("⚠️ **{line}**"))
}
fn plain_list(items: &[String], shown: usize) -> String {
let named: Vec<&str> = items.iter().take(shown).map(String::as_str).collect();
let more = items.len().saturating_sub(shown);
match (named.split_last(), more) {
(None, _) => String::new(),
(Some((last, [])), 0) => (*last).to_string(),
(Some((last, rest)), 0) => format!("{} and {last}", rest.join(", ")),
(Some(_), more) => format!("{} and {more} more", named.join(", ")),
}
}
pub fn card_headline(card: &ApprovalCard) -> String {
let who = card
.asked_by
.as_deref()
.map(str::trim)
.filter(|name| !name.is_empty())
.unwrap_or("Your assistant");
if let CardAction::TakeOver { what, site, .. } = &card.action {
let what = what.trim().trim_end_matches('.');
let what = if what.is_empty() {
"do a step yourself"
} else {
what
};
return match site.as_deref().filter(|site| !site.is_empty()) {
Some(site) => format!("{who} needs you on {site}: {what}."),
None => format!("{who} needs you: {what}."),
};
}
let what = match &card.action {
CardAction::Command {
looks_inside,
title,
..
} => {
let title = title
.as_deref()
.map(|title| title.trim().trim_end_matches(['.', ':']))
.filter(|title| !title.is_empty());
if !looks_inside.is_empty() {
let noun = if looks_inside.len() == 1 {
"folder"
} else {
"folders"
};
format!("look inside your {} {noun}", plain_list(looks_inside, 3))
} else if let Some(title) = title {
format!("run a command on your computer: {title}")
} else {
"run a command on your computer".to_string()
}
}
CardAction::OpenLocalSite { site, .. } => {
format!("open {site} in the browser, a site on this computer")
}
CardAction::ClickOnSite { site, .. } => {
format!("click a button on {site} in the browser")
}
CardAction::Use { what, .. } => what.trim().trim_end_matches('.').to_string(),
_ => "do something that needs your OK".to_string(),
};
format!("{who} wants to {what}.")
}
fn card_why(card: &ApprovalCard) -> Option<String> {
let mut lines = Vec::new();
if let Some(AskReason::ReadOutsideContent { check_says }) = &card.reason {
lines.push(
"It read content from outside, like a web page, so I'm checking with you \
before it runs this."
.to_string(),
);
if let Some(says) = check_says
.as_deref()
.map(str::trim)
.filter(|says| !says.is_empty())
{
lines.push(format!("_Safety check: {says}_"));
}
}
if let Some(AskReason::AfterADecline { declined }) = &card.reason {
let what = declined
.as_deref()
.map(str::trim)
.filter(|what| !what.is_empty())
.unwrap_or("a step");
lines.push(format!(
"You said no to {what} earlier in this task, so I'm checking with you before \
steps whose effects I can't fully tell."
));
}
if let CardAction::TakeOver {
keeps_sign_in,
open_url,
..
} = &card.action
{
lines.push(
"Do it in the live view in the AIDaemon app, or at the computer, then press Done. The agent waits and never sees what you type."
.to_string(),
);
match keeps_sign_in {
Some(true) => lines.push("A sign-in here is kept for later tasks.".to_string()),
Some(false) => lines.push("A sign-in here lasts only for this task.".to_string()),
None => {}
}
if let Some(url) = open_url.as_deref().filter(|url| !url.is_empty()) {
lines.push(format!("Open the live view: {url}"));
}
}
if let CardAction::OpenLocalSite { once_minutes, .. } = &card.action {
lines.push(format!(
"Allow only if you asked for it. Allow covers the next {once_minutes} minutes; \
the longer choice lasts until this browser session ends."
));
}
if let CardAction::ClickOnSite { site, .. } = &card.action {
lines.push(click_on_site_scope(site));
}
(!lines.is_empty()).then(|| lines.join("\n"))
}
pub fn click_on_site_scope(site: &str) -> String {
format!(
"Allow once covers only this click; the longer choice covers buttons on {site} \
until this browser session ends. Submitting a form, a password or payment field, \
and other sites still ask."
)
}
fn goal_detail_section(detail: &str) -> (&'static str, &str) {
for (prefix, heading) in [
("Objective:", "🎯 Objective"),
("Constraints:", "🛡️ Rules it must follow"),
("Success criteria:", "✅ Success means"),
("Stop conditions:", "🛑 Stops when"),
("Guidance:", "🧭 Guidance"),
("Pinned strategy:", "🧭 Strategy"),
("Access summary:", "🔑 What it can do"),
("Observations allowed:", "👁️ Read access"),
("Allowed mutation effects:", "✍️ External changes"),
("Allowed targets:", "🎯 Allowed targets"),
("Exact operation scopes:", "🔧 Operation scope"),
("Mutation limits:", "📏 Activity limits"),
("Review interval:", "🔄 Check-in timing"),
("Expiration:", "⏱️ Ends"),
("Autonomy mode:", "🤖 How it works"),
("Owner checkpoints:", "🙋 Owner checkpoints"),
("Recovery policy:", "♻️ Recovery"),
("Confirmation binding:", "🔏 Confirmation binding"),
("Review effort:", "🧠 Review effort"),
("Review depth:", "🧠 Decision checks"),
("Policy reference:", "🔏 Approval scope"),
("Resolved token budgets:", "🧮 Compute budget"),
] {
if let Some(value) = detail.strip_prefix(prefix) {
return (heading, value.trim());
}
}
("ℹ️ Additional detail", detail.trim())
}
fn normalized_goal_text(text: &str) -> String {
text.trim()
.strip_prefix("Delegate mandate:")
.unwrap_or(text.trim())
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
.trim_end_matches(['.', ';'])
.to_ascii_lowercase()
}
fn summary_states(summary_normalized: &str, what: &str) -> bool {
let what = normalized_goal_text(what);
!what.is_empty()
&& (summary_normalized == what
|| summary_normalized
.strip_suffix(what.as_str())
.is_some_and(|lead| lead.ends_with(": ")))
}
fn readable_policy(value: &str) -> String {
value.replace(" | ", "\n")
}
fn mandate_card_body(
subject: &str,
summary: &str,
waiting_copy: &str,
card: &aidaemon_core::mandate_card::MandateConfirmationCard,
) -> String {
use aidaemon_core::mandate_card::Essential;
let mut body = format!("{subject}\n{summary}\n");
let summary_normalized = normalized_goal_text(summary);
for (essential, text) in card.essentials() {
if text.trim().is_empty() {
continue;
}
if essential == Essential::What && summary_states(&summary_normalized, text) {
continue;
}
body.push_str(&format!("\n{}: {text}", essential.label()));
}
body.push_str(&format!("\n\n{waiting_copy}"));
if !card.details.is_empty() {
body.push_str("\n\nDetails");
for detail in &card.details {
body.push_str(&format!("\n• {}", readable_policy(detail.trim())));
}
}
body
}
pub fn build_goal_confirmation_pages(
goal_description: &str,
details: &[String],
style: GoalConfirmationStyle,
) -> Vec<String> {
const PAGE_BODY_BYTES: usize = 1_500;
let summary = goal_description
.trim()
.strip_prefix("Delegate mandate:")
.unwrap_or(goal_description.trim())
.trim();
let summary_normalized = normalized_goal_text(summary);
let (subject, waiting_copy) = match style {
GoalConfirmationStyle::Standard => (
"What you’re approving",
"Nothing starts until you approve this goal.",
),
GoalConfirmationStyle::Autopilot => (
"What Autopilot will manage",
"Nothing starts until you approve this exact policy.",
),
};
let body = match aidaemon_core::mandate_card::MandateConfirmationCard::from_lines(details) {
Some(card) => mandate_card_body(subject, summary, waiting_copy, &card),
None => {
let mut body = format!("{subject}\n{summary}\n\n{waiting_copy}");
for detail in details {
let (heading, value) = goal_detail_section(detail);
if heading == "🎯 Objective" && normalized_goal_text(value) == summary_normalized
{
continue;
}
body.push_str(&format!("\n\n{heading}\n{}", readable_policy(value)));
}
body
}
};
let chunks = split_message(&body, PAGE_BODY_BYTES);
let page_count = chunks.len();
chunks
.into_iter()
.enumerate()
.map(|(index, chunk)| {
let title = match style {
GoalConfirmationStyle::Standard => "🔐 Review plan",
GoalConfirmationStyle::Autopilot => "🚀 Enable Autopilot",
};
let mut page = if page_count == 1 {
format!("{title} before activation\n\n{chunk}")
} else {
format!("{title} · {}/{page_count}\n\n{chunk}", index + 1)
};
if index + 1 == page_count {
page.push_str(match style {
GoalConfirmationStyle::Standard => {
"\n\nReady to proceed? Approve and activate this plan, or cancel below."
}
GoalConfirmationStyle::Autopilot => {
"\n\nReady to proceed? Enable this exact Autopilot policy or cancel below."
}
});
}
page
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn plain_fallback_keeps_nested_fences_inside_literal_block() {
for command in [
"```js\nexecute();\n```",
"before ````` after",
"simple command",
] {
let rendered = build_approval_message_markdown(command, RiskLevel::Medium, &[]);
let block = rendered.split("**Requested action**\n").nth(1).unwrap();
let (fence, rest) = block.split_once('\n').unwrap();
assert!(fence.len() >= 3);
assert!(fence.bytes().all(|b| b == b'`'));
let escaped = command.replace('`', "\\`");
assert!(!escaped.contains(fence));
assert_eq!(rest, format!("{escaped}\n{fence}"));
assert_eq!(block.matches("```").count(), 2);
}
}
#[test]
fn risk_header_medium_is_approval_needed() {
let (_, title, _) = risk_header(RiskLevel::Medium);
assert_eq!(title, "Approval needed");
}
#[test]
fn finalize_strips_footer_and_adds_status() {
let prompt = build_approval_message_markdown(
"Open website: https://example.com",
RiskLevel::Medium,
&[],
);
let final_msg = finalize_approval_message(&prompt, &ApprovalResponse::AllowAlways);
assert!(final_msg.contains("Always allowed"));
assert!(final_msg.contains("won't be asked again"));
assert!(final_msg.contains("Open website"));
let old = format!("{prompt}\n\n*\"Allow Always\" means you won't be asked again.*");
let final_msg = finalize_approval_message(&old, &ApprovalResponse::Deny);
assert!(!final_msg.contains("Allow Always\" means"), "{final_msg}");
}
#[test]
fn finalize_denied_shows_denied_status() {
let prompt = build_approval_message_markdown("rm -rf /", RiskLevel::Critical, &[]);
let final_msg = finalize_approval_message(&prompt, &ApprovalResponse::Deny);
assert!(final_msg.contains("❌ Denied"));
}
const INSPECT: &str = "cd /Users/alice/projects && for d in claude-drupal \
claude-wordpress; do echo \"== $d\"; ls -la $d | head -30; ls $d/.ddev 2>&1 | head; \
(cd $d && ddev describe 2>&1 | head -25); done";
fn ask<'a>(card: &'a ApprovalCard, risk: RiskLevel, warnings: &'a [String]) -> String {
build_approval_ask_markdown(&ApprovalAsk {
command: "summary a channel without cards would show",
card: Some(card),
risk_level: risk,
warnings,
permission_mode: PermissionMode::Default,
one_time_only: true,
})
}
#[test]
fn a_low_risk_read_only_card_says_what_happens_without_alarm() {
let card = ApprovalCard {
asked_by: Some("Drupal install".to_string()),
run: None,
action: CardAction::Command {
line: INSPECT.to_string(),
title: Some("check claude site dirs and docker".to_string()),
looks_inside: vec!["claude-drupal".to_string(), "claude-wordpress".to_string()],
},
reason: Some(AskReason::ReadOutsideContent {
check_says: Some(
"Looking inside these folders doesn't look needed to install Drupal."
.to_string(),
),
}),
impact: None,
};
let text = ask(&card, RiskLevel::High, &[]);
let expected = format!(
"**Drupal install wants to look inside your claude-drupal and claude-wordpress \
folders.**\n\
It read content from outside, like a web page, so I'm checking with you before \
it runs this.\n\
_Safety check: Looking inside these folders doesn't look needed to install \
Drupal._\n\n\
```{COLLAPSED_FENCE}\n{INSPECT}\n```"
);
assert_eq!(text, expected);
for gone in [
"Review carefully",
"significant effects",
"applies only",
"⚠️",
"owner",
] {
assert!(!text.contains(gone), "{gone}: {text}");
}
let fenced = text.split("```").nth(1).unwrap();
assert!(!fenced.contains("wants to"), "{text}");
}
#[test]
fn a_high_risk_delete_card_names_the_effect() {
let card = ApprovalCard {
asked_by: None,
run: None,
action: CardAction::Command {
line: "rm -rf build dist".to_string(),
title: Some("clean old builds".to_string()),
looks_inside: Vec::new(),
},
reason: None,
impact: Some(HighImpact::Deletes {
targets: vec!["build".to_string(), "dist".to_string()],
}),
};
let notes = ["Execution target: host (local) workspace /srv/app".to_string()];
let text = ask(&card, RiskLevel::High, ¬es);
assert!(
text.starts_with(
"⚠️ **This deletes build and dist.**\n\
**Your assistant wants to run a command on your computer: clean old builds.**"
),
"{text}"
);
assert!(text.ends_with(&format!(
"```{COLLAPSED_FENCE}\nrm -rf build dist\n\nExecution target: host (local) \
workspace /srv/app\n```"
)));
let plain = ApprovalCard {
impact: None,
..card
};
assert!(!ask(&plain, RiskLevel::High, &[]).contains("⚠️"));
assert!(ask(&plain, RiskLevel::Critical, &[]).starts_with("⚠️ **This can change"));
}
#[test]
fn a_browser_card_names_the_site_and_how_long_allow_lasts() {
let card = ApprovalCard {
asked_by: Some("WordPress install".to_string()),
..ApprovalCard::new(CardAction::OpenLocalSite {
site: "claude-wordpress.ddev.site".to_string(),
once_minutes: 30,
})
};
let text = ask(&card, RiskLevel::High, &[]);
assert_eq!(
text,
"**WordPress install wants to open claude-wordpress.ddev.site in the browser, a \
site on this computer.**\n\
Allow only if you asked for it. Allow covers the next 30 minutes; the longer \
choice lasts until this browser session ends."
);
}
#[test]
fn a_button_card_says_the_session_choice_covers_the_site() {
let card = ApprovalCard::new(CardAction::ClickOnSite {
site: "https://shop.example.test".to_string(),
target: Some("#add-to-cart".to_string()),
});
let text = ask(&card, RiskLevel::High, &[]);
assert!(
text.starts_with(
"**Your assistant wants to click a button on https://shop.example.test in the \
browser.**\nAllow once covers only this click; the longer choice covers \
buttons on https://shop.example.test until this browser session ends."
),
"{text}"
);
assert!(text.contains("#add-to-cart"), "{text}");
}
#[test]
fn a_long_command_is_cut_in_the_middle_and_a_fence_cannot_end_the_block() {
let line = format!("echo ```; {} tail-marker", "x".repeat(5_000));
let card = ApprovalCard::new(CardAction::Command {
line,
title: None,
looks_inside: Vec::new(),
});
let text = ask(&card, RiskLevel::Medium, &[]);
assert!(text.starts_with("**Your assistant wants to run a command on your computer.**"));
assert!(text.contains("characters left out"));
assert!(text.contains("tail-marker"));
assert_eq!(text.matches("```").count(), 2, "{text}");
assert!(text.len() < 2_000);
}
#[test]
fn a_card_flattens_for_channels_without_cards() {
let card = ApprovalCard {
reason: Some(AskReason::ReadOutsideContent {
check_says: Some("This isn't part of your request.".to_string()),
}),
impact: Some(HighImpact::Deletes {
targets: Vec::new(),
}),
..ApprovalCard::new(CardAction::Command {
line: "rm -rf x".to_string(),
title: None,
looks_inside: Vec::new(),
})
};
let notes = ["note".to_string()];
let warnings = card_as_warnings(&ApprovalAsk {
command: "Run a command on your computer?",
card: Some(&card),
risk_level: RiskLevel::High,
warnings: ¬es,
permission_mode: PermissionMode::Default,
one_time_only: true,
});
assert_eq!(
warnings,
[
"note",
"This deletes files.",
"Safety check: This isn't part of your request.",
"Details: rm -rf x"
]
);
}
#[test]
fn a_take_over_card_names_the_step_the_site_and_what_is_kept() {
let card = ApprovalCard::new(CardAction::TakeOver {
surface_id: "srf_1".into(),
what: "Sign in to your Acme account".into(),
site: Some("login.acme.example".into()),
keeps_sign_in: Some(true),
open_url: Some("https://daemon.example.invalid/client/v1/next/#surface=srf_1".into()),
});
let text = build_card_markdown(&card, RiskLevel::Safe, &[]);
assert!(text.contains(
"Your assistant needs you on login.acme.example: Sign in to your Acme account."
));
assert!(text.contains("kept for later tasks"));
assert!(text.contains("never sees what you type"));
assert!(text.contains("#surface=srf_1"));
}
#[test]
fn lists_read_plainly() {
let names = |n: usize| (1..=n).map(|i| format!("f{i}")).collect::<Vec<_>>();
assert_eq!(plain_list(&names(1), 3), "f1");
assert_eq!(plain_list(&names(2), 3), "f1 and f2");
assert_eq!(plain_list(&names(3), 3), "f1, f2 and f3");
assert_eq!(plain_list(&names(5), 3), "f1, f2, f3 and 2 more");
}
#[test]
fn goal_confirmation_pages_preserve_large_proposal_within_telegram_limit() {
let final_marker = "FINAL_AUTHORITY_DETAIL";
let details = vec![format!(
"{}{}",
"scope <unescaped> ".repeat(500),
final_marker
)];
let pages = build_goal_confirmation_pages(
"Delegate an ongoing mandate",
&details,
GoalConfirmationStyle::Standard,
);
assert!(pages.len() > 1);
assert!(pages.iter().all(|page| page.len() <= 4_096));
assert!(pages.last().unwrap().contains(final_marker));
assert!(
pages
.last()
.unwrap()
.contains("Approve and activate this plan, or cancel")
);
assert!(pages.iter().any(|page| page.contains("<unescaped>")));
}
#[test]
fn goal_confirmation_is_scannable_and_deduplicates_objective() {
let description = "Delegate mandate: Steward the synthetic account for 24 hours.";
let details = vec![
"Objective: Steward the synthetic account for 24 hours.".to_string(),
"Constraints: 1. Verify identity. | 2. Never retry an ambiguous mutation.".to_string(),
"Expiration: 86400 seconds after actual activation".to_string(),
];
let pages =
build_goal_confirmation_pages(description, &details, GoalConfirmationStyle::Standard);
let rendered = pages.join("\n");
assert!(rendered.starts_with("🔐 Review plan before activation"));
assert_eq!(rendered.matches("Steward the synthetic account").count(), 1);
assert!(rendered.contains("🛡️ Rules it must follow\n1. Verify identity.\n2. Never retry"));
assert!(rendered.contains("⏱️ Ends"));
assert!(rendered.contains("Nothing starts until you approve this goal."));
}
#[test]
fn a_mandate_card_shows_five_essentials_and_one_details_section() {
let card = aidaemon_core::mandate_card::MandateConfirmationCard {
what: "Every hour, check the synthetic status page".to_string(),
how_often: "Every hour".to_string(),
can_do: "Read only – makes no changes".to_string(),
tells_you_when: "it finds something you asked to hear about".to_string(),
ends: "Only when you stop it".to_string(),
details: vec![
"Owner checkpoints: It asks you before using a new account.".to_string(),
"Policy reference: Your approval covers only these rules (policy version 2)."
.to_string(),
"Timing note: It uses the length you gave.".to_string(),
],
};
let pages = build_goal_confirmation_pages(
"Delegate mandate \"Status\": Every hour, check the synthetic status page",
&card.into_lines(),
GoalConfirmationStyle::Standard,
);
let rendered = pages.join("\n");
assert_eq!(
rendered.matches("check the synthetic status page").count(),
1
);
for line in [
"\nHow often: Every hour",
"\nCan do: Read only – makes no changes",
"\nTells you when: it finds something you asked to hear about",
"\nEnds: Only when you stop it",
] {
assert!(rendered.contains(line), "{line}: {rendered}");
}
assert_eq!(rendered.matches("\nDetails\n").count(), 1, "{rendered}");
assert!(rendered.contains("• Timing note: It uses the length you gave."));
assert!(!rendered.contains("Additional detail"), "{rendered}");
let essentials_end = rendered.find("Ends:").unwrap();
assert!(essentials_end < rendered.find("Details").unwrap());
}
#[test]
fn the_what_line_is_kept_unless_the_summary_is_that_field() {
let card = |what: &str| aidaemon_core::mandate_card::MandateConfirmationCard {
what: what.to_string(),
how_often: "Every hour".to_string(),
can_do: "Read only".to_string(),
tells_you_when: "it finds something".to_string(),
ends: "Only when you stop it".to_string(),
details: Vec::new(),
};
let render = |summary: &str, what: &str| {
build_goal_confirmation_pages(
summary,
&card(what).into_lines(),
GoalConfirmationStyle::Standard,
)
.join("\n")
};
let partial = render("Watch the synthetic status page and the blog", "the blog");
assert!(partial.contains("\nWhat: the blog"), "{partial}");
let empty = render("Watch the synthetic status page", "");
assert!(!empty.contains("\nWhat:"), "{empty}");
let same = render(
"Watch the synthetic status page",
"Watch the synthetic status page",
);
assert_eq!(same.matches("synthetic status page").count(), 1, "{same}");
}
#[test]
fn autopilot_confirmation_is_structurally_distinct() {
let pages = build_goal_confirmation_pages(
"Maintain the synthetic account",
&["Autonomy mode: Autopilot".to_string()],
GoalConfirmationStyle::Autopilot,
);
let rendered = pages.join("\n");
assert!(rendered.starts_with("🚀 Enable Autopilot"));
assert!(rendered.contains("Enable this exact Autopilot policy"));
}
}