use std::collections::{HashMap, HashSet, VecDeque}; use std::sync::{Arc, Mutex, MutexGuard}; use std::time::Duration; use chrono::{DateTime, Utc}; use serde::Serialize; use store::{ AgentMeetingRecord, AgentProposalMention, AgentProposalRecord, AgentReactionRecord, AttachmentImage, Store, }; use tokio::sync::mpsc::UnboundedSender; use tokio::sync::{Notify, Semaphore}; use crate::policy::AgentPolicy; use crate::source::{MeetingDraft, ReactionInfo}; const EVENT_CAPACITY: usize = 1000; const CAPTION_CAPACITY: usize = 2000; const PENDING_STATE: &str = "pending"; const PROPOSAL_PERMITS: usize = 4; const MAX_PENDING_MEETINGS: usize = 5; pub const MIN_WAKEUP_DELAY: Duration = Duration::from_secs(60); pub const MAX_WAKEUP_DELAY: Duration = Duration::from_secs(24 * 60 * 60); const NOT_AN_APP_SESSION: &str = "schedule_wakeup only works in a session rusty-teams started (Answer, Build this or a send to a session); in your own session use your client's scheduling"; /// A run started by rusty-teams that asks to be resumed later with `prompt`. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Wakeup { pub session_id: String, pub due_at: DateTime, pub prompt: String, pub reason: String, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct ProposalMention { pub user_id: String, pub name: String, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct ProposalImage { pub name: String, pub mime_type: String, pub bytes: Arc>, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct FreeWritingAsk { pub minutes: u32, pub reason: String, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct FreeWriting { pub until: DateTime, pub agent: Option, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct Proposal { pub id: String, pub conversation_id: String, pub reply_to_message_id: Option, pub text: String, pub mentions: Vec, pub images: Vec, pub free_writing_ask: Option, pub agent: Option, pub session_id: Option, pub created_at: DateTime, pub direct: bool, } #[derive(Debug, Clone, Default)] pub struct NewProposal { pub conversation_id: String, pub text: String, pub reply_to_message_id: Option, pub mentions: Vec, pub images: Vec, pub free_writing_ask: Option, pub agent: Option, pub session_id: Option, pub direct: bool, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct ReactionProposal { pub id: String, pub conversation_id: String, pub message_id: String, pub emoji: String, pub agent: Option, pub session_id: Option, pub created_at: DateTime, pub direct: bool, } #[derive(Debug, Clone, Default)] pub struct NewReactionProposal { pub conversation_id: String, pub message_id: String, pub emoji: String, pub agent: Option, pub session_id: Option, pub direct: bool, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct MeetingProposal { pub id: String, pub draft: MeetingDraft, pub agent: Option, pub session_id: Option, pub created_at: DateTime, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] #[serde(rename_all = "snake_case")] pub enum MeetingOutcome { Created, Discarded, Failed, } #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct CaptionLine { #[serde(skip)] pub caption_id: String, pub speaker: String, pub text: String, pub at: DateTime, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] #[serde(rename_all = "snake_case")] pub enum Outcome { Sent, Edited, Discarded, Stale, Superseded, } impl Outcome { fn state(self) -> &'static str { match self { Outcome::Sent => "sent", Outcome::Edited => "edited", Outcome::Discarded => "discarded", Outcome::Stale => "stale", Outcome::Superseded => "superseded", } } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] #[serde(rename_all = "snake_case")] pub enum ReactionOutcome { Accepted, Discarded, Stale, Superseded, } impl ReactionOutcome { fn state(self) -> &'static str { match self { ReactionOutcome::Accepted => "accepted", ReactionOutcome::Discarded => "discarded", ReactionOutcome::Stale => "stale", ReactionOutcome::Superseded => "superseded", } } } #[derive(Debug, Clone, PartialEq, Eq, Serialize)] #[serde(tag = "type", rename_all = "snake_case")] pub enum EventKind { NewMessage { conversation_id: String, message_id: String, sender: Option, preview: String, }, UserTyping { conversation_id: String, }, UserSent { conversation_id: String, }, ProposalResolved { proposal_id: String, conversation_id: String, outcome: Outcome, }, ReactionResolved { proposal_id: String, conversation_id: String, message_id: String, outcome: ReactionOutcome, }, ReactionChanged { conversation_id: String, message_id: String, reactions: Vec, }, FreeWritingChanged { conversation_id: String, until: Option>, }, MeetingResolved { proposal_id: String, outcome: MeetingOutcome, #[serde(skip_serializing_if = "Option::is_none")] join_url: Option, }, } impl EventKind { fn visible(&self, policy: &AgentPolicy) -> bool { match self { EventKind::MeetingResolved { .. } => policy.can_organize_meetings(), other => policy.can_read_any_kind(other.conversation_id()), } } pub fn conversation_id(&self) -> &str { match self { EventKind::MeetingResolved { .. } => "", EventKind::NewMessage { conversation_id, .. } | EventKind::UserTyping { conversation_id } | EventKind::UserSent { conversation_id } | EventKind::ProposalResolved { conversation_id, .. } | EventKind::ReactionResolved { conversation_id, .. } | EventKind::ReactionChanged { conversation_id, .. } | EventKind::FreeWritingChanged { conversation_id, .. } => conversation_id, } } } #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct Event { pub seq: u64, #[serde(flatten)] pub kind: EventKind, } #[derive(Debug, Clone, PartialEq, Eq)] pub enum UiEvent { ProposalChanged(Proposal), ProposalStale(Proposal), ProposalRemoved { conversation_id: String, }, ReactionProposalChanged(ReactionProposal), ReactionProposalRemoved { conversation_id: String, message_id: String, }, MeetingProposalsChanged, WakeupScheduled(Wakeup), } #[derive(Default)] struct HubState { policy: AgentPolicy, pending: HashMap, pending_reactions: HashMap<(String, String), ReactionProposal>, events: VecDeque, last_seq: u64, captions: HashMap>, free_writing: HashMap, pending_meetings: Vec, app_sessions: HashSet, } pub struct AgentHub { state: Mutex, wake: Notify, proposal_permits: Arc, store: Arc, ui: UnboundedSender, } impl AgentHub { pub fn new(store: Arc, policy: AgentPolicy, ui: UnboundedSender) -> Arc { Arc::new(AgentHub { state: Mutex::new(HubState { policy, ..HubState::default() }), wake: Notify::new(), proposal_permits: Arc::new(Semaphore::new(PROPOSAL_PERMITS)), store, ui, }) } pub fn proposal_permits(&self) -> Arc { self.proposal_permits.clone() } pub fn set_app_sessions(&self, session_ids: HashSet) { self.lock().app_sessions = session_ids; } pub fn schedule_wakeup( &self, session_id: &str, delay: Duration, prompt: &str, reason: &str, ) -> Result { if !self.lock().app_sessions.contains(session_id) { return Err(NOT_AN_APP_SESSION.to_owned()); } let delay = delay.clamp(MIN_WAKEUP_DELAY, MAX_WAKEUP_DELAY); let wakeup = Wakeup { session_id: session_id.to_owned(), due_at: Utc::now() + chrono::Duration::from_std(delay).unwrap_or_default(), prompt: prompt.to_owned(), reason: reason.to_owned(), }; let _ = self.ui.send(UiEvent::WakeupScheduled(wakeup.clone())); Ok(wakeup) } pub fn set_policy(&self, policy: AgentPolicy) { self.lock().policy = policy; } pub fn policy(&self) -> AgentPolicy { self.lock().policy.clone() } pub fn free_writing(&self, conversation_id: &str) -> Option { self.lock() .free_writing .get(conversation_id) .filter(|grant| grant.until > Utc::now()) .cloned() } pub fn grant_free_writing( &self, conversation_id: &str, until: DateTime, agent: Option, ) { let mut state = self.lock(); state .free_writing .insert(conversation_id.to_owned(), FreeWriting { until, agent }); push_event( &mut state, EventKind::FreeWritingChanged { conversation_id: conversation_id.to_owned(), until: Some(until), }, ); drop(state); self.wake.notify_waiters(); } pub fn stop_free_writing(&self, conversation_id: &str) { let mut state = self.lock(); if state.free_writing.remove(conversation_id).is_none() { return; } push_event( &mut state, EventKind::FreeWritingChanged { conversation_id: conversation_id.to_owned(), until: None, }, ); drop(state); self.wake.notify_waiters(); } pub fn needs_review(&self, conversation_id: &str, agent: Option<&str>) -> bool { let granted = self .free_writing(conversation_id) .is_some_and(|grant| grant.agent.is_none() || grant.agent.as_deref() == agent); self.policy().review && !granted } pub fn pending_proposal(&self, conversation_id: &str) -> Option { self.lock().pending.get(conversation_id).cloned() } pub fn pending_reactions(&self, conversation_id: &str) -> Vec { self.lock() .pending_reactions .values() .filter(|reaction| reaction.conversation_id == conversation_id) .cloned() .collect() } pub fn conversations_with_pending_reactions(&self) -> Vec { let mut conversations: Vec = self .lock() .pending_reactions .values() .map(|reaction| reaction.conversation_id.clone()) .collect(); conversations.sort(); conversations.dedup(); conversations } pub fn restore_pending(&self) -> store::Result<()> { self.store.sweep_agent_proposal_images()?; let records = self.store.pending_agent_proposals()?; let reactions = self.store.pending_agent_reactions()?; let meetings = self.store.agent_meetings()?; let mut state = self.lock(); state.pending_meetings = meetings.into_iter().map(meeting_from_record).collect(); if !state.pending_meetings.is_empty() { let _ = self.ui.send(UiEvent::MeetingProposalsChanged); } for record in records { let proposal = proposal_from_record(record); let _ = self.ui.send(UiEvent::ProposalChanged(proposal.clone())); state .pending .insert(proposal.conversation_id.clone(), proposal); } for record in reactions { let reaction = reaction_from_record(record); let _ = self .ui .send(UiEvent::ReactionProposalChanged(reaction.clone())); state .pending_reactions .insert(reaction_key(&reaction), reaction); } Ok(()) } pub fn add_proposal(&self, new: NewProposal) -> store::Result { let proposal = Proposal { id: uuid::Uuid::new_v4().to_string(), conversation_id: new.conversation_id, reply_to_message_id: new.reply_to_message_id, text: new.text, mentions: new.mentions, images: new.images, free_writing_ask: new.free_writing_ask, agent: new.agent, session_id: new.session_id, created_at: now_millis(), direct: new.direct, }; self.store.insert_agent_proposal(&AgentProposalRecord { id: proposal.id.clone(), conversation_id: proposal.conversation_id.clone(), reply_to_message_id: proposal.reply_to_message_id.clone(), text: proposal.text.clone(), mentions: proposal .mentions .iter() .map(|mention| AgentProposalMention { user_id: mention.user_id.clone(), name: mention.name.clone(), }) .collect(), images: proposal .images .iter() .map(|image| AttachmentImage { name: image.name.clone(), format: image.mime_type.clone(), bytes: image.bytes.to_vec(), width: None, height: None, }) .collect(), agent: proposal.agent.clone(), session_id: proposal.session_id.clone(), created_at: proposal.created_at, state: PENDING_STATE.to_owned(), resolved_at: None, })?; let mut state = self.lock(); if let Some(previous) = state .pending .insert(proposal.conversation_id.clone(), proposal.clone()) { self.finish(&mut state, &previous, Outcome::Superseded); } let _ = self.ui.send(UiEvent::ProposalChanged(proposal.clone())); drop(state); self.wake.notify_waiters(); Ok(proposal) } pub fn resolve(&self, proposal_id: &str, outcome: Outcome) -> bool { let mut state = self.lock(); let Some(conversation_id) = state .pending .values() .find(|proposal| proposal.id == proposal_id) .map(|proposal| proposal.conversation_id.clone()) else { return false; }; let Some(proposal) = state.pending.remove(&conversation_id) else { return false; }; self.finish(&mut state, &proposal, outcome); let _ = self.ui.send(UiEvent::ProposalRemoved { conversation_id }); drop(state); self.wake.notify_waiters(); true } pub fn add_reaction_proposal( &self, new: NewReactionProposal, ) -> store::Result { let reaction = ReactionProposal { id: uuid::Uuid::new_v4().to_string(), conversation_id: new.conversation_id, message_id: new.message_id, emoji: new.emoji, agent: new.agent, session_id: new.session_id, created_at: now_millis(), direct: new.direct, }; self.store.insert_agent_reaction(&AgentReactionRecord { id: reaction.id.clone(), conversation_id: reaction.conversation_id.clone(), message_id: reaction.message_id.clone(), emoji: reaction.emoji.clone(), agent: reaction.agent.clone(), session_id: reaction.session_id.clone(), created_at: reaction.created_at, state: PENDING_STATE.to_owned(), resolved_at: None, })?; let mut state = self.lock(); if let Some(previous) = state .pending_reactions .insert(reaction_key(&reaction), reaction.clone()) { self.finish_reaction(&mut state, &previous, ReactionOutcome::Superseded); } let _ = self .ui .send(UiEvent::ReactionProposalChanged(reaction.clone())); drop(state); self.wake.notify_waiters(); Ok(reaction) } pub fn resolve_reaction(&self, reaction_id: &str, outcome: ReactionOutcome) -> bool { let mut state = self.lock(); let Some(key) = state .pending_reactions .iter() .find(|(_, reaction)| reaction.id == reaction_id) .map(|(key, _)| key.clone()) else { return false; }; let Some(reaction) = state.pending_reactions.remove(&key) else { return false; }; self.finish_reaction(&mut state, &reaction, outcome); let _ = self.ui.send(UiEvent::ReactionProposalRemoved { conversation_id: reaction.conversation_id, message_id: reaction.message_id, }); drop(state); self.wake.notify_waiters(); true } pub fn reaction_changed( &self, conversation_id: &str, message_id: &str, reactions: Vec, ) { self.push(EventKind::ReactionChanged { conversation_id: conversation_id.to_owned(), message_id: message_id.to_owned(), reactions, }); } pub fn push_caption(&self, conversation_id: &str, caption_id: &str, speaker: &str, text: &str) { let mut state = self.lock(); let lines = state .captions .entry(conversation_id.to_owned()) .or_default(); let line = CaptionLine { caption_id: caption_id.to_owned(), speaker: speaker.to_owned(), text: text.to_owned(), at: now_millis(), }; match lines.back_mut() { Some(last) if last.caption_id == caption_id => *last = line, _ => lines.push_back(line), } if lines.len() > CAPTION_CAPACITY { lines.pop_front(); } } pub fn live_captions(&self, conversation_id: &str) -> Vec { self.lock() .captions .get(conversation_id) .map(|lines| lines.iter().cloned().collect()) .unwrap_or_default() } pub fn clear_captions(&self, conversation_id: &str) { self.lock().captions.remove(conversation_id); } pub fn user_typing(&self, conversation_id: &str) { self.push(EventKind::UserTyping { conversation_id: conversation_id.to_owned(), }); } pub fn user_sent(&self, conversation_id: &str) { self.push(EventKind::UserSent { conversation_id: conversation_id.to_owned(), }); self.stale_pending(conversation_id); } pub fn message_arrived( &self, conversation_id: &str, message_id: &str, sender_is_me: bool, created_at: DateTime, sender: Option, preview: &str, ) { if sender_is_me { let answers_pending = self .pending_proposal(conversation_id) .is_some_and(|proposal| created_at >= proposal.created_at); if answers_pending { self.stale_pending(conversation_id); } return; } self.push(EventKind::NewMessage { conversation_id: conversation_id.to_owned(), message_id: message_id.to_owned(), sender, preview: preview.to_owned(), }); } pub async fn wait_events(&self, since: Option, timeout: Duration) -> (Vec, u64) { let deadline = tokio::time::Instant::now() + timeout; let last_seq = self.lock().last_seq; let mut cursor = match since { Some(cursor) if cursor <= last_seq => cursor, Some(_) => 0, None => last_seq, }; loop { let notified = self.wake.notified(); tokio::pin!(notified); notified.as_mut().enable(); let (events, next) = self.events_after(cursor); if !events.is_empty() { return (events, next); } cursor = next; if tokio::time::timeout_at(deadline, notified).await.is_err() { return (Vec::new(), cursor); } } } pub fn propose_meeting( &self, draft: MeetingDraft, agent: Option, session_id: Option, ) -> Result { let mut state = self.lock(); if state.pending_meetings.len() >= MAX_PENDING_MEETINGS { return Err(format!( "{MAX_PENDING_MEETINGS} meetings already wait for the user's review; wait for meeting_resolved" )); } let proposal = MeetingProposal { id: uuid::Uuid::new_v4().to_string(), draft, agent, session_id, created_at: Utc::now(), }; self.store .insert_agent_meeting(&meeting_record(&proposal)) .map_err(|error| error.to_string())?; state.pending_meetings.push(proposal.clone()); drop(state); let _ = self.ui.send(UiEvent::MeetingProposalsChanged); Ok(proposal) } pub fn pending_meetings(&self) -> Vec { self.lock().pending_meetings.clone() } pub fn take_meeting(&self, proposal_id: &str) -> Option { let mut state = self.lock(); let index = state .pending_meetings .iter() .position(|proposal| proposal.id == proposal_id)?; Some(state.pending_meetings.remove(index)) } pub fn resolve_meeting( &self, proposal_id: &str, outcome: MeetingOutcome, join_url: Option, ) { { let mut state = self.lock(); state .pending_meetings .retain(|proposal| proposal.id != proposal_id); let _ = self.store.delete_agent_meeting(proposal_id); push_event( &mut state, EventKind::MeetingResolved { proposal_id: proposal_id.to_owned(), outcome, join_url, }, ); } self.wake.notify_waiters(); let _ = self.ui.send(UiEvent::MeetingProposalsChanged); } fn events_after(&self, cursor: u64) -> (Vec, u64) { let state = self.lock(); let events = state .events .iter() .filter(|event| event.seq > cursor) .filter(|event| event.kind.visible(&state.policy)) .cloned() .collect(); (events, state.last_seq.max(cursor)) } fn stale_pending(&self, conversation_id: &str) { let mut state = self.lock(); let Some(proposal) = state.pending.remove(conversation_id) else { return; }; self.finish(&mut state, &proposal, Outcome::Stale); let _ = self.ui.send(UiEvent::ProposalStale(proposal)); drop(state); self.wake.notify_waiters(); } fn finish_reaction( &self, state: &mut HubState, reaction: &ReactionProposal, outcome: ReactionOutcome, ) { if let Err(error) = self.store .set_agent_reaction_state(&reaction.id, outcome.state(), Some(now_millis())) { eprintln!("mcp: could not store reaction proposal state: {error}"); } push_event( state, EventKind::ReactionResolved { proposal_id: reaction.id.clone(), conversation_id: reaction.conversation_id.clone(), message_id: reaction.message_id.clone(), outcome, }, ); } fn finish(&self, state: &mut HubState, proposal: &Proposal, outcome: Outcome) { if let Err(error) = self.store .set_agent_proposal_state(&proposal.id, outcome.state(), Some(now_millis())) { eprintln!("mcp: could not store proposal state: {error}"); } push_event( state, EventKind::ProposalResolved { proposal_id: proposal.id.clone(), conversation_id: proposal.conversation_id.clone(), outcome, }, ); } fn push(&self, kind: EventKind) { push_event(&mut self.lock(), kind); self.wake.notify_waiters(); } fn lock(&self) -> MutexGuard<'_, HubState> { self.state .lock() .unwrap_or_else(|poisoned| poisoned.into_inner()) } } fn now_millis() -> DateTime { DateTime::from_timestamp_millis(Utc::now().timestamp_millis()).unwrap_or_default() } fn push_event(state: &mut HubState, kind: EventKind) { state.last_seq += 1; if state.events.len() == EVENT_CAPACITY { state.events.pop_front(); } state.events.push_back(Event { seq: state.last_seq, kind, }); } fn reaction_key(reaction: &ReactionProposal) -> (String, String) { ( reaction.conversation_id.clone(), reaction.message_id.clone(), ) } fn reaction_from_record(record: AgentReactionRecord) -> ReactionProposal { ReactionProposal { id: record.id, conversation_id: record.conversation_id, message_id: record.message_id, emoji: record.emoji, agent: record.agent, session_id: record.session_id, created_at: record.created_at, direct: false, } } fn meeting_record(proposal: &MeetingProposal) -> AgentMeetingRecord { AgentMeetingRecord { id: proposal.id.clone(), subject: proposal.draft.subject.clone(), start: proposal.draft.start, end: proposal.draft.end, attendees: proposal.draft.attendees.clone(), description: proposal.draft.description.clone(), agent: proposal.agent.clone(), session_id: proposal.session_id.clone(), created_at: proposal.created_at, } } fn meeting_from_record(record: AgentMeetingRecord) -> MeetingProposal { MeetingProposal { id: record.id, draft: MeetingDraft { subject: record.subject, start: record.start, end: record.end, attendees: record.attendees, description: record.description, }, agent: record.agent, session_id: record.session_id, created_at: record.created_at, } } fn proposal_from_record(record: AgentProposalRecord) -> Proposal { Proposal { id: record.id, conversation_id: record.conversation_id, reply_to_message_id: record.reply_to_message_id, text: record.text, mentions: record .mentions .into_iter() .map(|mention| ProposalMention { user_id: mention.user_id, name: mention.name, }) .collect(), images: record .images .into_iter() .map(|image| ProposalImage { name: image.name, mime_type: image.format, bytes: Arc::new(image.bytes), }) .collect(), free_writing_ask: None, agent: record.agent, session_id: record.session_id, created_at: record.created_at, direct: false, } } #[cfg(test)] mod tests { use tokio::sync::mpsc::{UnboundedReceiver, unbounded_channel}; use super::*; use crate::policy::Access; const SHORT: Duration = Duration::from_millis(50); fn open_policy() -> AgentPolicy { AgentPolicy { enabled: true, chats: Access::All, channels: Access::All, write: true, ..AgentPolicy::default() } } fn hub_with(policy: AgentPolicy) -> (Arc, UnboundedReceiver, Arc) { let store = Arc::new(Store::open_in_memory().unwrap()); let (sender, receiver) = unbounded_channel(); ( AgentHub::new(store.clone(), policy, sender), receiver, store, ) } fn propose(hub: &AgentHub, conversation_id: &str, text: &str) -> Proposal { hub.add_proposal(NewProposal { conversation_id: conversation_id.to_owned(), text: text.to_owned(), ..NewProposal::default() }) .unwrap() } fn react(hub: &AgentHub, conversation_id: &str, message_id: &str) -> ReactionProposal { hub.add_reaction_proposal(NewReactionProposal { conversation_id: conversation_id.to_owned(), message_id: message_id.to_owned(), emoji: "\u{1F44D}".to_owned(), ..NewReactionProposal::default() }) .unwrap() } #[test] fn at_most_five_meetings_wait_for_review() { let (hub, _receiver, _store) = hub_with(open_policy()); let draft = crate::source::MeetingDraft { subject: "Sync".to_owned(), start: Utc::now(), end: Utc::now(), attendees: vec!["a@example.com".to_owned()], description: String::new(), }; let first = hub.propose_meeting(draft.clone(), None, None).unwrap(); for _ in 0..4 { hub.propose_meeting(draft.clone(), None, None).unwrap(); } assert!(hub.propose_meeting(draft.clone(), None, None).is_err()); hub.resolve_meeting(&first.id, MeetingOutcome::Discarded, None); assert!(hub.propose_meeting(draft, None, None).is_ok()); } #[test] fn a_meeting_waiting_for_review_survives_a_restart() { let (hub, _receiver, store) = hub_with(open_policy()); let draft = crate::source::MeetingDraft { subject: "Sync".to_owned(), start: Utc::now(), end: Utc::now(), attendees: vec!["a@example.com".to_owned()], description: "Agenda".to_owned(), }; let kept = hub .propose_meeting(draft.clone(), Some("claude".to_owned()), None) .unwrap(); let resolved = hub.propose_meeting(draft, None, None).unwrap(); hub.resolve_meeting(&resolved.id, MeetingOutcome::Created, None); let (sender, _restarted_receiver) = unbounded_channel(); let restarted = AgentHub::new(store, open_policy(), sender); restarted.restore_pending().unwrap(); let restored = restarted.pending_meetings(); assert_eq!(restored.len(), 1); assert_eq!( ( restored[0].id.as_str(), restored[0].draft.description.as_str(), restored[0].agent.as_deref() ), (kept.id.as_str(), "Agenda", Some("claude")) ); } #[test] fn a_free_writing_window_skips_review_until_it_ends_or_stops() { let (hub, _receiver, _store) = hub_with(open_policy()); let claude = Some("claude"); assert!(hub.needs_review("c1", claude)); hub.grant_free_writing( "c1", Utc::now() + chrono::Duration::minutes(15), Some("claude".to_owned()), ); assert!(!hub.needs_review("c1", claude)); assert!(hub.needs_review("c1", Some("codex"))); assert!(hub.needs_review("c1", None)); assert!(hub.needs_review("c2", claude)); hub.stop_free_writing("c1"); assert!(hub.needs_review("c1", claude)); hub.grant_free_writing("c1", Utc::now() - chrono::Duration::seconds(1), None); assert!(hub.needs_review("c1", claude)); let kinds: Vec>> = hub .events_after(0) .0 .into_iter() .filter_map(|event| match event.kind { EventKind::FreeWritingChanged { until, .. } => Some(until), _ => None, }) .collect(); assert_eq!(kinds.len(), 3); assert_eq!(kinds[1], None); } #[test] fn a_new_proposal_supersedes_the_pending_one() { let (hub, mut ui, store) = hub_with(open_policy()); let first = propose(&hub, "c1", "one"); let second = propose(&hub, "c1", "two"); assert_eq!(hub.pending_proposal("c1"), Some(second.clone())); let pending = store.pending_agent_proposals().unwrap(); assert_eq!(pending.len(), 1); assert_eq!(pending[0].id, second.id); assert_eq!( ui.try_recv().unwrap(), UiEvent::ProposalChanged(first.clone()) ); assert_eq!(ui.try_recv().unwrap(), UiEvent::ProposalChanged(second)); let state = hub.lock(); assert!(matches!( state.events[0].kind, EventKind::ProposalResolved { outcome: Outcome::Superseded, .. } )); } #[test] fn resolve_clears_the_proposal_and_notifies_the_ui() { let (hub, mut ui, store) = hub_with(open_policy()); let proposal = propose(&hub, "c1", "hi"); ui.try_recv().unwrap(); assert!(hub.resolve(&proposal.id, Outcome::Sent)); assert!(!hub.resolve(&proposal.id, Outcome::Sent)); assert!(hub.pending_proposal("c1").is_none()); assert!(store.pending_agent_proposals().unwrap().is_empty()); assert_eq!( ui.try_recv().unwrap(), UiEvent::ProposalRemoved { conversation_id: "c1".to_owned() } ); } #[test] fn user_sent_and_own_messages_make_the_proposal_stale() { let (hub, mut ui, _store) = hub_with(open_policy()); let stale = propose(&hub, "c1", "hi"); ui.try_recv().unwrap(); hub.user_sent("c1"); assert!(hub.pending_proposal("c1").is_none()); assert_eq!(ui.try_recv().unwrap(), UiEvent::ProposalStale(stale)); propose(&hub, "c2", "yo"); hub.message_arrived("c2", "m1", false, Utc::now(), None, "other"); assert!(hub.pending_proposal("c2").is_some()); hub.message_arrived("c2", "m2", true, Utc::now(), None, "mine"); assert!(hub.pending_proposal("c2").is_none()); } #[test] fn restore_pending_reloads_unresolved_proposals() { let (hub, _ui, store) = hub_with(open_policy()); let proposal = propose(&hub, "c1", "hi"); let (sender, mut receiver) = unbounded_channel(); let restored = AgentHub::new(store, open_policy(), sender); restored.restore_pending().unwrap(); assert_eq!(restored.pending_proposal("c1"), Some(proposal.clone())); assert_eq!( receiver.try_recv().unwrap(), UiEvent::ProposalChanged(proposal) ); } #[test] fn captions_update_partials_and_clear() { let (hub, _ui, _store) = hub_with(open_policy()); hub.push_caption("c1", "1", "Ana", "Hel"); hub.push_caption("c1", "1", "Ana", "Hello"); hub.push_caption("c1", "2", "Bo", "Hi"); let lines = hub.live_captions("c1"); assert_eq!(lines.len(), 2); assert_eq!(lines[0].text, "Hello"); assert!(hub.live_captions("c2").is_empty()); hub.clear_captions("c1"); assert!(hub.live_captions("c1").is_empty()); } #[tokio::test] async fn waiting_without_a_cursor_only_sees_new_events() { let (hub, _ui, _store) = hub_with(open_policy()); hub.user_typing("c1"); let (events, next) = hub.wait_events(None, SHORT).await; assert!(events.is_empty()); assert_eq!(next, 1); hub.user_typing("c1"); let (events, next) = hub.wait_events(Some(next), SHORT).await; assert_eq!(events.len(), 1); assert_eq!(next, 2); } #[tokio::test] async fn a_cursor_from_before_a_restart_starts_over() { let (hub, _ui, _store) = hub_with(open_policy()); hub.user_typing("c1"); let (events, next) = hub.wait_events(Some(500), SHORT).await; assert_eq!(events.len(), 1); assert_eq!(next, 1); } #[tokio::test] async fn a_waiter_wakes_when_an_event_arrives() { let (hub, _ui, _store) = hub_with(open_policy()); let waiter = { let hub = hub.clone(); tokio::spawn(async move { hub.wait_events(None, Duration::from_secs(5)).await }) }; tokio::time::sleep(Duration::from_millis(20)).await; hub.message_arrived( "c1", "m1", false, Utc::now(), Some("Ada".to_owned()), "hello", ); let (events, next) = waiter.await.unwrap(); assert_eq!(events.len(), 1); assert_eq!(next, events[0].seq); } #[tokio::test] async fn events_are_filtered_by_the_policy_at_read_time() { let policy = AgentPolicy { chats: Access::Selected(["c1".to_owned()].into()), channels: Access::None, ..open_policy() }; let (hub, _ui, _store) = hub_with(policy); hub.user_typing("c1"); hub.user_typing("c2"); let (events, next) = hub.wait_events(Some(0), SHORT).await; assert_eq!(events.len(), 1); assert_eq!(events[0].kind.conversation_id(), "c1"); assert_eq!(next, 2); hub.set_policy(AgentPolicy::default()); assert!(hub.wait_events(Some(0), SHORT).await.0.is_empty()); } #[test] fn the_ring_buffer_drops_the_oldest_events() { let (hub, _ui, _store) = hub_with(open_policy()); for _ in 0..EVENT_CAPACITY + 5 { hub.user_typing("c1"); } let state = hub.lock(); assert_eq!(state.events.len(), EVENT_CAPACITY); assert_eq!(state.events[0].seq, 6); assert_eq!(state.last_seq, EVENT_CAPACITY as u64 + 5); } #[test] fn reaction_proposals_live_beside_the_message_proposal() { let (hub, mut ui, _store) = hub_with(open_policy()); let proposal = propose(&hub, "c1", "hi"); let first = react(&hub, "c1", "m1"); let second = react(&hub, "c1", "m2"); assert_eq!(hub.pending_proposal("c1"), Some(proposal.clone())); assert_eq!(hub.pending_reactions("c1").len(), 2); assert_eq!(ui.try_recv().unwrap(), UiEvent::ProposalChanged(proposal)); assert_eq!( ui.try_recv().unwrap(), UiEvent::ReactionProposalChanged(first) ); assert_eq!( ui.try_recv().unwrap(), UiEvent::ReactionProposalChanged(second) ); assert!(hub.lock().events.is_empty()); } #[test] fn a_second_reaction_on_the_same_message_replaces_the_first_as_superseded() { let (hub, _ui, store) = hub_with(open_policy()); react(&hub, "c1", "m1"); let second = react(&hub, "c1", "m1"); assert_eq!(hub.pending_reactions("c1"), [second.clone()]); assert_eq!(store.pending_agent_reactions().unwrap().len(), 1); assert!(matches!( hub.lock().events[0].kind, EventKind::ReactionResolved { outcome: ReactionOutcome::Superseded, .. } )); } #[test] fn resolving_a_reaction_leaves_the_message_proposal_alone() { let (hub, mut ui, store) = hub_with(open_policy()); let proposal = propose(&hub, "c1", "hi"); let reaction = react(&hub, "c1", "m1"); while ui.try_recv().is_ok() {} assert!(hub.resolve_reaction(&reaction.id, ReactionOutcome::Accepted)); assert!(!hub.resolve_reaction(&reaction.id, ReactionOutcome::Accepted)); assert_eq!(hub.pending_proposal("c1"), Some(proposal)); assert!(store.pending_agent_reactions().unwrap().is_empty()); assert_eq!( ui.try_recv().unwrap(), UiEvent::ReactionProposalRemoved { conversation_id: "c1".to_owned(), message_id: "m1".to_owned() } ); assert!(matches!( hub.lock().events[0].kind, EventKind::ReactionResolved { outcome: ReactionOutcome::Accepted, .. } )); } #[test] fn restore_pending_reloads_reaction_proposals() { let (hub, _ui, store) = hub_with(open_policy()); let reaction = react(&hub, "c1", "m1"); let (sender, mut receiver) = unbounded_channel(); let restored = AgentHub::new(store, open_policy(), sender); restored.restore_pending().unwrap(); assert_eq!(restored.pending_reactions("c1"), [reaction.clone()]); assert_eq!( receiver.try_recv().unwrap(), UiEvent::ReactionProposalChanged(reaction) ); } fn propose_with_image(hub: &AgentHub, conversation_id: &str) -> Proposal { hub.add_proposal(NewProposal { conversation_id: conversation_id.to_owned(), text: "look".to_owned(), images: vec![ProposalImage { name: "image-1.png".to_owned(), mime_type: "image/png".to_owned(), bytes: Arc::new(vec![1, 2, 3]), }], ..NewProposal::default() }) .unwrap() } #[test] fn proposal_images_are_deleted_when_the_proposal_ends() { let (hub, _ui, store) = hub_with(open_policy()); let count = |proposal: &Proposal| store.agent_proposal_image_count(&proposal.id).unwrap(); let resolved = propose_with_image(&hub, "c1"); assert_eq!(count(&resolved), 1); hub.resolve(&resolved.id, Outcome::Sent); assert_eq!(count(&resolved), 0); let superseded = propose_with_image(&hub, "c2"); let current = propose_with_image(&hub, "c2"); assert_eq!(count(&superseded), 0); assert_eq!(count(¤t), 1); hub.user_sent("c2"); assert_eq!(count(¤t), 0); } #[test] fn the_restore_sweep_drops_images_of_proposals_that_are_not_pending() { let (hub, _ui, store) = hub_with(open_policy()); let kept = propose_with_image(&hub, "c1"); store .insert_agent_proposal(&AgentProposalRecord { id: "old".to_owned(), conversation_id: "c3".to_owned(), reply_to_message_id: None, text: "x".to_owned(), mentions: Vec::new(), images: vec![AttachmentImage { name: "i".to_owned(), format: "image/png".to_owned(), bytes: vec![9], width: None, height: None, }], agent: None, session_id: None, created_at: now_millis(), state: "sent".to_owned(), resolved_at: None, }) .unwrap(); assert_eq!(store.agent_proposal_image_count("old").unwrap(), 1); let (sender, _receiver) = unbounded_channel(); AgentHub::new(store.clone(), open_policy(), sender) .restore_pending() .unwrap(); assert_eq!(store.agent_proposal_image_count(&kept.id).unwrap(), 1); assert_eq!(store.agent_proposal_image_count("old").unwrap(), 0); } #[test] fn only_sessions_the_app_started_can_schedule_a_wakeup() { let (hub, mut events, _store) = hub_with(open_policy()); assert!( hub.schedule_wakeup("desktop", Duration::from_secs(600), "check", "PR") .is_err() ); hub.set_app_sessions(HashSet::from(["app-run".to_owned()])); let before = Utc::now(); let wakeup = hub .schedule_wakeup("app-run", Duration::from_secs(5), "check CI", "PR 7") .unwrap(); assert!(wakeup.due_at >= before + chrono::Duration::seconds(60)); assert_eq!(events.try_recv().unwrap(), UiEvent::WakeupScheduled(wakeup)); } }