use std::fmt;
use std::sync::Arc;
use aither_sandbox::{
AnySandboxNotices, CommandKilled, CompletedTask, JobAlert, SandboxNotices,
StartedBackgroundTask,
};
use askama::Template;
use crate::compact_tool::CompactRequest;
use crate::external_hook::InjectedContext;
use crate::host_load::{HostCondition, HostLevel, HostLoadEvent};
use crate::secret_guard::{SecretFinding, SecretRefusal};
use crate::specialized::ForkRefusal;
use crate::working_document::DocumentAlertItem;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PreCompaction {
pub remaining_tokens: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CancelledToolCall<'a> {
pub tool: &'a str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DocumentShown<'a> {
pub document: &'a str,
pub content: Option<&'a str>,
pub changes: Option<&'a str>,
pub trees: Option<&'a str>,
}
#[derive(Debug, Clone, Copy)]
pub struct DocumentAlert<'a> {
pub document: &'a str,
pub items: &'a [DocumentAlertItem],
pub held: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommandHookRefusal<'a> {
Blocked {
hook: &'a str,
reason: &'a str,
},
Failed {
hook: &'a str,
failure: &'a str,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ReviewerDenial<'a> {
pub reviewer: &'a str,
pub reason: &'a str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ForkToolRefusal<'a> {
pub tool: &'a str,
}
#[derive(Template)]
#[template(path = "precompaction_alert.txt", escape = "none")]
struct PreCompactionTemplate {
remaining_tokens: usize,
}
#[derive(Template)]
#[template(path = "tool_call_cancelled.txt", escape = "none")]
struct CancelledTemplate<'a> {
tool: &'a str,
}
#[derive(Template)]
#[template(path = "compact_started.txt", escape = "none")]
struct CompactStarted;
#[derive(Template)]
#[template(path = "compact_in_flight.txt", escape = "none")]
struct CompactInFlight;
#[derive(Template)]
#[template(path = "compact_settled.txt", escape = "none")]
struct CompactSettled;
#[derive(Template)]
#[template(path = "compact_below_floor.txt", escape = "none")]
struct CompactBelowFloor {
floor: usize,
}
#[derive(Template)]
#[template(path = "lifecycle_context.txt", escape = "none")]
struct LifecycleContext<'a> {
hook: &'a str,
event: &'a str,
context: &'a str,
}
#[derive(Template)]
#[template(path = "command_hook_blocked.txt", escape = "none")]
struct HookBlocked<'a> {
hook: &'a str,
reason: &'a str,
}
#[derive(Template)]
#[template(path = "command_hook_failed.txt", escape = "none")]
struct HookFailed<'a> {
hook: &'a str,
failure: &'a str,
}
#[derive(Template)]
#[template(path = "reviewer_denial.txt", escape = "none")]
struct Denial<'a> {
reviewer: &'a str,
reason: &'a str,
}
#[derive(Template)]
#[template(path = "fork_tool_refused.txt", escape = "none")]
struct ForkToolRefused<'a> {
name: &'a str,
}
#[derive(Template)]
#[template(path = "secret_refusal.txt", escape = "none")]
struct SecretRefused<'a> {
findings: &'a [SecretFinding],
}
pub(crate) fn rendered(template: &impl Template) -> String {
template
.render()
.expect("a notice template renders typed input")
}
pub trait Notices: SandboxNotices {
fn host_load(&self, event: &HostLoadEvent) -> String {
event.alert_text()
}
fn host_condition(
&self,
condition: &HostCondition,
seen: Option<HostLevel>,
now: HostLevel,
reading: &HostLoadEvent,
) -> String {
HostLoadEvent::for_change(condition, seen, now, reading).alert_text()
}
fn job_alert(&self, alert: &JobAlert) -> String {
crate::agent::default_job_alert(alert)
}
fn background_started(&self, task: &StartedBackgroundTask) -> String {
crate::agent::default_background_started(task)
}
fn background_finished(&self, task: &CompletedTask) -> String {
let timeout = task
.timeout_kill
.as_ref()
.map(|kill| self.command_killed(&CommandKilled::TimedOut(kill)));
crate::agent::default_background_finished(task, timeout.as_deref())
}
fn precompaction(&self, alert: &PreCompaction) -> String {
rendered(&PreCompactionTemplate {
remaining_tokens: alert.remaining_tokens,
})
}
fn tool_call_cancelled(&self, call: &CancelledToolCall<'_>) -> String {
rendered(&CancelledTemplate { tool: call.tool })
}
fn compact(&self, request: &CompactRequest) -> String {
match *request {
CompactRequest::Started => rendered(&CompactStarted),
CompactRequest::InFlight => rendered(&CompactInFlight),
CompactRequest::Settled => rendered(&CompactSettled),
CompactRequest::BelowFloor { floor } => rendered(&CompactBelowFloor { floor }),
}
}
fn working_document(&self, shown: &DocumentShown<'_>) -> String {
crate::working_document::default_shown(shown)
}
fn document_alert(&self, alert: &DocumentAlert<'_>) -> String {
crate::working_document::default_alert(alert)
}
fn lifecycle_context(&self, injected: &InjectedContext) -> String {
rendered(&LifecycleContext {
hook: injected.hook(),
event: injected.event().occasion(),
context: injected.context(),
})
}
fn command_hook(&self, refusal: &CommandHookRefusal<'_>) -> String {
match *refusal {
CommandHookRefusal::Blocked { hook, reason } => rendered(&HookBlocked { hook, reason }),
CommandHookRefusal::Failed { hook, failure } => rendered(&HookFailed { hook, failure }),
}
}
fn reviewer_denial(&self, denial: &ReviewerDenial<'_>) -> String {
rendered(&Denial {
reviewer: denial.reviewer,
reason: denial.reason,
})
}
fn fork_refusal(&self, refusal: &ForkRefusal) -> String {
crate::specialized::default_fork_refusal(refusal)
}
fn fork_tool_refusal(&self, refusal: &ForkToolRefusal<'_>) -> String {
rendered(&ForkToolRefused { name: refusal.tool })
}
fn secret_refusal(&self, refusal: &SecretRefusal) -> String {
rendered(&SecretRefused {
findings: &refusal.findings,
})
}
#[cfg(feature = "languages")]
fn language_alerts(&self, alerts: &[crate::language::LanguageAlert]) -> String {
crate::language::default_alerts(alerts)
}
#[cfg(feature = "languages")]
fn language_refusal(&self, refusal: &crate::LanguageRefusal) -> String {
crate::language::default_refusal(refusal)
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct DefaultNotices;
impl SandboxNotices for DefaultNotices {}
impl Notices for DefaultNotices {}
#[derive(Clone)]
pub struct AnyNotices(Arc<dyn Notices>);
impl AnyNotices {
#[must_use]
pub fn new(notices: impl Notices) -> Self {
Self(Arc::new(notices))
}
#[must_use]
pub fn sandbox(&self) -> AnySandboxNotices {
let shared: Arc<dyn SandboxNotices> = self.0.clone();
AnySandboxNotices::from(shared)
}
}
impl Default for AnyNotices {
fn default() -> Self {
Self::new(DefaultNotices)
}
}
impl std::ops::Deref for AnyNotices {
type Target = dyn Notices;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
impl fmt::Debug for AnyNotices {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AnyNotices").finish_non_exhaustive()
}
}