use super::{RiskTier, tier_may_be_waived};
use crate::agent::OverrideRecord;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct AskOrigin {
pub explicit: bool,
pub default: bool,
pub clamp: bool,
pub intent_raised: bool,
pub hook_raised: bool,
}
pub const STARTUP_WAIVER_REASON_PREFIX: &str = "startup-override:";
#[derive(Clone, Debug)]
pub struct StartupWaiver {
nonce: String,
}
impl StartupWaiver {
pub fn from_record(record: &OverrideRecord) -> Option<Self> {
record.flags.no_hitl.then(|| Self {
nonce: record.nonce.clone(),
})
}
pub fn nonce(&self) -> &str {
&self.nonce
}
pub fn reason(&self) -> String {
format!("{STARTUP_WAIVER_REASON_PREFIX}{}", self.nonce)
}
pub fn may_waive(&self, origin: AskOrigin, tier: Option<RiskTier>) -> bool {
origin_eligible(origin) && tier_may_be_waived(tier)
}
pub fn held_by_tier(&self, origin: AskOrigin, tier: Option<RiskTier>, spend: bool) -> bool {
origin_eligible(origin) && (spend || !tier_may_be_waived(tier))
}
}
fn origin_eligible(origin: AskOrigin) -> bool {
!(origin.intent_raised || origin.hook_raised || origin.explicit)
&& (origin.default || origin.clamp)
}
pub const TIER_NOT_WAIVED: &str = "tier_not_waived";
const TIER_NOT_WAIVED_NEXT: &str =
"Retrying will wait again. Ask the user to approve it, or continue without this operation";
fn tier_not_waived_clause(tier: Option<RiskTier>) -> String {
let tier = match tier {
Some(t) => format!("{t:?}"),
None => "undeclared".to_string(),
};
format!(
"this call is {tier}-tier, and --no-hitl never waives \
Spend, Privileged or undeclared-tier calls"
)
}
pub fn tier_not_waived_reason(timed_out: &str, tier: Option<RiskTier>) -> String {
format!(
"{timed_out}: {}, so it waited for a human and nobody answered. {TIER_NOT_WAIVED_NEXT}",
tier_not_waived_clause(tier)
)
}
pub fn tier_not_waived_note(tier: Option<RiskTier>) -> String {
format!("{}. {TIER_NOT_WAIVED_NEXT}", tier_not_waived_clause(tier))
}
#[cfg(test)]
#[path = "startup_waiver_tests.rs"]
mod tests;