use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use crate::ChatId;
use crate::failure::TurnContext;
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::runtime) struct Activity {
pub(in crate::runtime) busy: bool,
pub(in crate::runtime) last_event: Instant,
pub(in crate::runtime) turn: TurnContext,
pub(in crate::runtime) generation: u64,
pub(in crate::runtime) expired: Option<u64>,
}
pub(in crate::runtime) type Activities = Arc<Mutex<HashMap<ChatId, Activity>>>;
pub(in crate::runtime) fn stalled(
now: Instant,
activity: &Activity,
stall_after: Duration,
) -> bool {
activity.busy && now.saturating_duration_since(activity.last_event) >= stall_after
}
pub(in crate::runtime) fn observe(
chats: &HashMap<ChatId, Activity>,
now: Instant,
stall_after: Duration,
) -> Vec<(ChatId, u64)> {
chats
.iter()
.filter(|(_, activity)| stalled(now, activity, stall_after))
.map(|(chat, activity)| (*chat, activity.generation))
.collect()
}
fn running<'a>(
chats: &'a mut HashMap<ChatId, Activity>,
(chat, generation): &(ChatId, u64),
) -> Option<&'a mut Activity> {
chats
.get_mut(chat)
.filter(|activity| activity.busy && activity.generation == *generation)
}
pub(in crate::runtime) fn mark_alive(
chats: &mut HashMap<ChatId, Activity>,
observed: &[(ChatId, u64)],
now: Instant,
) {
for turn in observed {
if let Some(activity) = running(chats, turn) {
activity.last_event = now;
}
}
}
pub(in crate::runtime) fn expire(
chats: &mut HashMap<ChatId, Activity>,
observed: &[(ChatId, u64)],
now: Instant,
stall_after: Duration,
) -> Vec<(ChatId, TurnContext)> {
let mut ended = Vec::new();
for turn in observed {
let Some(activity) = running(chats, turn) else {
continue;
};
if now.saturating_duration_since(activity.last_event) < stall_after {
continue;
}
activity.busy = false;
activity.expired = Some(turn.1);
ended.push((turn.0, activity.turn.clone()));
}
ended
}
pub(in crate::runtime) fn update(
chats: &mut HashMap<ChatId, Activity>,
chat: ChatId,
mut activity: Activity,
) {
if let Some(old) = chats.get(&chat) {
if old.generation > activity.generation {
return;
}
if old.expired == Some(activity.generation) {
activity.busy = false;
activity.expired = old.expired;
}
}
chats.insert(chat, activity);
}
pub(in crate::runtime) fn expired(
chats: &HashMap<ChatId, Activity>,
chat: ChatId,
generation: u64,
) -> bool {
chats
.get(&chat)
.is_some_and(|activity| activity.expired == Some(generation))
}
pub(in crate::runtime) fn settle(
chats: &mut HashMap<ChatId, Activity>,
chat: ChatId,
generation: u64,
) -> bool {
let Some(activity) = chats.get_mut(&chat) else {
return true;
};
if activity.expired == Some(generation) {
return false;
}
if activity.generation == generation {
activity.busy = false;
}
true
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::runtime) enum Late {
Normal,
Ignore,
Rearm,
}
pub(in crate::runtime) fn late(expired: bool, kind: &str) -> Late {
if !expired {
return Late::Normal;
}
match kind {
"session.idle" | "session.error" => Late::Ignore,
"assistant.message_delta"
| "assistant.reasoning_delta"
| "assistant.message"
| "assistant.reasoning"
| "assistant.turn_start"
| "tool.execution_start"
| "tool.execution_complete" => Late::Rearm,
_ => Late::Normal,
}
}
#[cfg(test)]
#[path = "fence_tests.rs"]
mod tests;