Skip to main content

infinity_daemon/session/
observer.rs

1//! The daemon's [`ThreadObserver`]: fans agent events out to attached
2//! clients as [`DaemonMessage`]s and persists derived presentation state.
3
4use infinity_agent_core::ThreadId;
5use std::collections::HashMap;
6use std::sync::Arc;
7
8use infinity_agent_core::system::{AgentEvent, ReplaySnapshot, ThreadObserver};
9use infinity_protocol::DaemonMessage;
10use tokio::sync::mpsc;
11
12use super::display::{agent_event_to_daemon, history_message_to_daemon};
13use crate::memory_store::PersistentConversationStore;
14
15/// A subscriber attached to a thread's display events.
16#[derive(Clone)]
17pub struct Subscriber {
18    pub tx: mpsc::UnboundedSender<DaemonMessage>,
19    /// When `false`, this subscriber does not prevent the session from
20    /// idling out (e.g. the Slack bot's persistent connections).
21    pub keeps_session_alive: bool,
22}
23
24/// A request to subscribe to a thread's display events.
25pub struct SubscribeRequest {
26    pub tx: mpsc::UnboundedSender<DaemonMessage>,
27    pub wants_replay: bool,
28    pub keeps_session_alive: bool,
29}
30
31/// Shared subscriber list for one thread.
32pub type ThreadSubscribers = Arc<std::sync::Mutex<Vec<Subscriber>>>;
33
34/// Maps thread_id → subscriber list (for inheriting to children and idle
35/// detection).
36pub type SubscriberMap = Arc<std::sync::Mutex<HashMap<ThreadId, ThreadSubscribers>>>;
37
38/// Deliver `msg` to every subscriber in the list, pruning subscribers whose
39/// client has disconnected (their channel is closed). This is the single
40/// fan-out primitive; every broadcast in the daemon goes through it so
41/// delivery and pruning semantics cannot drift between call sites.
42///
43/// Returns `true` when a delivery went out on the same channel as
44/// `requester` (`None` always returns `false`), so a caller responding to a
45/// client request can avoid double-sending to a requester that is also
46/// subscribed.
47pub fn broadcast_pruning(
48    subs: &ThreadSubscribers,
49    msg: &DaemonMessage,
50    requester: Option<&mpsc::UnboundedSender<DaemonMessage>>,
51) -> bool {
52    let mut requester_reached = false;
53    let mut subs = subs.lock().expect("bug: mutex poisoned");
54    subs.retain(|sub| {
55        let delivered = sub.tx.send(msg.clone()).is_ok();
56        if delivered && requester.is_some_and(|r| r.same_channel(&sub.tx)) {
57            requester_reached = true;
58        }
59        delivered
60    });
61    requester_reached
62}
63
64/// Look up `thread_id`'s subscriber list and [`broadcast_pruning`] to it.
65/// The map guard is released before the list is locked, so this never holds
66/// both locks at once.
67pub fn broadcast_to_thread(
68    map: &SubscriberMap,
69    thread_id: &ThreadId<str>,
70    msg: &DaemonMessage,
71    requester: Option<&mpsc::UnboundedSender<DaemonMessage>>,
72) -> bool {
73    let subs = map
74        .lock()
75        .expect("bug: mutex poisoned")
76        .get(thread_id)
77        .cloned();
78    match subs {
79        Some(subs) => broadcast_pruning(&subs, msg, requester),
80        None => false,
81    }
82}
83
84/// Per-thread observer wired up by the session's observer factory.
85///
86/// Event fan-out and state updates happen synchronously in
87/// [`on_event`](ThreadObserver::on_event) — the runtime invokes it at the
88/// exact emission point and handles subscriber attachment on the same task,
89/// so each event is either reflected in the replay snapshot a new subscriber
90/// receives or broadcast to it afterwards, never both.
91pub struct DaemonObserver {
92    pub(crate) subscribers: ThreadSubscribers,
93    pub(crate) conversation_store: PersistentConversationStore,
94}
95
96impl DaemonObserver {
97    fn broadcast(&self, dm: DaemonMessage) {
98        broadcast_pruning(&self.subscribers, &dm, None);
99    }
100}
101
102#[async_trait::async_trait(?Send)]
103impl ThreadObserver for DaemonObserver {
104    type SubscribeRequest = SubscribeRequest;
105
106    fn on_event(&self, thread_id: &ThreadId<str>, event: &AgentEvent) {
107        // Persist state derived from the event before broadcasting it, so a
108        // client can never observe a message whose session state is missing.
109        match event {
110            AgentEvent::CompletionFinished { usage } => {
111                // Only persist usage the provider actually reported; a
112                // response without usage metadata must not reset the stored
113                // total to zero.
114                if let Some(usage) = usage {
115                    self.conversation_store
116                        .set_total_tokens_used(thread_id, usage.total_tokens as usize);
117                }
118                self.conversation_store
119                    .set_last_updated(thread_id, &chrono::Utc::now().to_rfc3339());
120            }
121            // Reset the persisted context usage once compaction is applied:
122            // the stored pre-compaction total is stale and would otherwise be
123            // shown (and replayed on reconnect) until the next response
124            // reports fresh usage.
125            AgentEvent::CompactionApplied => {
126                self.conversation_store.set_total_tokens_used(thread_id, 0);
127            }
128            _ => {}
129        }
130
131        self.broadcast(agent_event_to_daemon(thread_id, event));
132    }
133
134    fn on_subscribe(
135        &self,
136        thread_id: &ThreadId<str>,
137        request: SubscribeRequest,
138        snapshot: ReplaySnapshot,
139    ) {
140        if request.wants_replay {
141            let mut history: Vec<DaemonMessage> = snapshot
142                .history
143                .iter()
144                .filter_map(|m| history_message_to_daemon(m, thread_id, &snapshot.history))
145                .collect();
146            // Include the in-progress thinking (streamed reasoning is only
147            // committed to history once it completes) so a client attaching
148            // mid-thinking recomputes a live "thinking" state from the end of
149            // the replay instead of appearing idle.
150            if let Some(thinking) = snapshot.current_thinking {
151                history.push(DaemonMessage::ThinkingStart {
152                    thread_id: Some(infinity_protocol::ThreadRef::local(thread_id.to_owned())),
153                });
154                history.push(DaemonMessage::ThinkingChunk {
155                    thread_id: Some(infinity_protocol::ThreadRef::local(thread_id.to_owned())),
156                    chunk: thinking,
157                });
158            }
159            let choices = snapshot
160                .pending_choices
161                .iter()
162                .map(|choice| DaemonMessage::UserChoiceRequired {
163                    thread_id: Some(infinity_protocol::ThreadRef::local(thread_id.to_owned())),
164                    id: choice.id.clone(),
165                    prompt: choice.prompt.clone(),
166                    choices: choice.choices.clone(),
167                    default: choice.default,
168                })
169                .collect::<Vec<_>>();
170            let views = self.conversation_store.get_views(thread_id);
171            if !history.is_empty() || !choices.is_empty() || !views.is_empty() {
172                let _ = request.tx.send(DaemonMessage::Replay {
173                    history,
174                    pending_choices: choices,
175                    views,
176                    // Only an actual in-flight completion counts: while
177                    // waiting on a tool result the clients derive their
178                    // "waiting for tool call" spinner from the trailing
179                    // ToolCall in the history instead.
180                    in_progress: snapshot.in_progress,
181                });
182            }
183        }
184        let mut subs = self.subscribers.lock().expect("bug: mutex poisoned");
185        // A client can legitimately re-send Connect on the same connection
186        // (e.g. its connect-retry timer fires while a slow resume is still in
187        // flight). Replace any existing subscription on the same channel
188        // instead of stacking a duplicate, which would deliver every display
189        // event to that client N times from then on.
190        subs.retain(|sub| !sub.tx.same_channel(&request.tx));
191        subs.push(Subscriber {
192            tx: request.tx,
193            keeps_session_alive: request.keeps_session_alive,
194        });
195    }
196}