Skip to main content

infinity_agent_core/
event_processor.rs

1use std::{
2    cell::{Cell, RefCell},
3    collections::{HashMap, HashSet},
4    time::Duration,
5};
6
7use futures_util::StreamExt;
8use infinity_provider_protocol::{
9    CompletionRequest, StreamChunk, ToolCallDeltaContent, ToolDefinition,
10    message::{AssistantContent, Message, ToolResult, ToolResultContent, UserContent},
11};
12use rap_protocol::{ChoiceId, ProviderCallId, ThreadId, ToolCallId};
13use serde::Serialize;
14use tracing;
15
16use crate::message::{
17    InfinityMessage, InputMessage, InputMessageContent, SyntheticKind, TaggedSyntheticKind,
18};
19use crate::system::AgentEvent;
20use crate::tools::{Tool, ToolContext};
21use crate::traits::{ConversationStore, InputSender, SpawnContext, StateStore};
22use infinity_provider_protocol::{ErrorClass, FinalResponse, ModelProvider};
23
24// ── Public types ──
25
26type BoxError = Box<dyn std::error::Error + Send + Sync>;
27
28#[derive(Debug, Serialize)]
29pub struct OutputMessage {
30    pub text: String,
31    pub metadata: serde_json::Value,
32}
33
34#[derive(Debug, Serialize)]
35pub struct OAuthOutputMessage {
36    #[serde(rename = "type")]
37    pub message_type: String,
38    pub auth_url: String,
39    pub metadata: serde_json::Value,
40}
41
42#[derive(Debug, Serialize)]
43pub struct UserChoiceOutputMessage {
44    #[serde(rename = "type")]
45    pub message_type: String,
46    pub id: String,
47    pub prompt: String,
48    pub choices: Vec<String>,
49    pub default: usize,
50    pub metadata: serde_json::Value,
51}
52
53#[derive(Debug, Serialize)]
54pub struct UserChoiceCompleteOutputMessage {
55    #[serde(rename = "type")]
56    pub message_type: String,
57    pub choice_id: String,
58    pub metadata: serde_json::Value,
59}
60
61/// The result of preparing an input message before sending it to the model.
62#[derive(Debug, PartialEq, Serialize)]
63pub enum PrepareResult {
64    /// The input was processed and the history manager is ready for completion.
65    Ready,
66    /// The input was handled without needing a completion (e.g. duplicate, closed thread).
67    Handled,
68    /// An OAuth challenge must be forwarded to the user.
69    OAuthRequired { auth_url: String },
70    /// A user choice prompt must be surfaced to the user.
71    UserChoiceRequired {
72        id: ChoiceId,
73        prompt: String,
74        choices: Vec<String>,
75        default: usize,
76        response_url: String,
77    },
78    /// Compaction was applied to the in-memory history.
79    CompactionApplied,
80}
81
82/// What the model wants to do after a completion stream finishes.
83pub enum CompletionAction {
84    /// Model produced text and is done (no tool call).
85    Done(FinalResponse),
86    /// Model wants to execute a tool call. Under the RAP protocol tools are
87    /// fire-and-forget: the agent loop stops after dispatching the call and
88    /// the result arrives as a new input message later.
89    ExecuteToolCall {
90        tool_name: String,
91        tool_args: serde_json::Value,
92        tool_call_id: ToolCallId,
93        call_id: Option<ProviderCallId>,
94        display_as: Option<String>,
95    },
96}
97
98/// Items yielded by the completion stream.
99pub enum CompletionEvent {
100    /// A chunk of text from the model.
101    TextChunk(String),
102    /// The terminal event — what to do next.
103    Action(CompletionAction),
104    /// A tool call that was synchronously processed.
105    SyncToolCall {
106        tool_name: String,
107        tool_args: serde_json::Value,
108        display_as: Option<String>,
109    },
110    /// The model has started thinking (reasoning).
111    ThinkingStart,
112    /// The model has stopped thinking (reasoning).
113    ThinkingEnd,
114    /// A chunk of thinking/reasoning text from the model.
115    ThinkingChunk(String),
116    /// A synchronous tool result.
117    SyncToolResult(ToolResult),
118    /// Some piece of information to log to the user.
119    Info(String),
120}
121
122// ── HistoryManager (unchanged from before) ──
123
124#[derive(Serialize, Clone)]
125pub struct PendingItem {
126    message: InfinityMessage,
127    message_id: String,
128}
129
130/// Result of matching an incoming tool result against the history tail (see
131/// [`HistoryManager::match_tool_result`]).
132enum ToolResultMatch {
133    /// A matching, still-unanswered tool call exists: the result is fresh.
134    Unanswered,
135    /// The matching call already has a result in history: duplicate delivery.
136    AlreadyAnswered,
137    /// No matching call in the live tail: the result is stale.
138    NoPendingCall,
139}
140
141pub struct HistoryManager<C: ConversationStore, S: StateStore> {
142    conversation_store: C,
143    state_store: S,
144    pub thread_id: ThreadId,
145    pub root_thread_id: ThreadId,
146    ancestor_chain: Vec<ThreadId>,
147    pub history: RefCell<Vec<InfinityMessage>>,
148    processed_message_ids: RefCell<HashSet<String>>,
149    metadata: RefCell<Option<serde_json::Value>>,
150    // Un-persisted content moves through three phases:
151    //
152    //   1. `unvalidated_items` — inputs (user text, tool results, injected
153    //      synthetic results) that the model has not produced output for
154    //      yet. They are part of the in-memory `history` (so completions
155    //      include them) but are **not** persisted by [`Self::sync`]: one
156    //      of them could be the oversized input that blows up the model's
157    //      context window, and persisting it would permanently wedge the
158    //      thread on a poison message.
159    //   2. `pending_items` — known-safe content awaiting persistence.
160    //      Inputs are promoted here by
161    //      [`Self::mark_inputs_model_validated`] as soon as the model
162    //      streams any output for them (proof the context did not
163    //      overflow); model-produced content is appended here directly.
164    //   3. Synced — persisted to the conversation store by [`Self::sync`].
165    //
166    // Sequentiality invariant: known-safe content is never appended while
167    // unvalidated items exist (enforced by an assert), so `pending_items`
168    // always precedes `unvalidated_items` in history order.
169    pending_items: RefCell<Vec<PendingItem>>,
170    unvalidated_items: RefCell<Vec<PendingItem>>,
171    /// until a turn is complete the data lives here. If errors occur,
172    /// it'll get discarded, if the turn completes, then it will get flushed to
173    /// _both_ pending_items and history.
174    turn_buffer: RefCell<Vec<PendingItem>>,
175    /// Tool call IDs that were interrupted by a new user message during
176    /// `handle_content`. Callers can drain this via `take_interrupted_tool_calls`
177    /// to send best-effort cancellation notifications to RAP tool servers.
178    interrupted_tool_calls: RefCell<Vec<ToolCallId>>,
179    /// Tracks the absolute store index that the current in-memory compaction
180    /// summary covers up to. Used to compute the correct relative split
181    /// position when a second compaction is applied on top of an existing one.
182    compacted_up_to: RefCell<Option<i64>>,
183    /// Number of ancestor messages prepended to the in-memory history.
184    /// These messages are NOT in this thread's own store, so they must be
185    /// subtracted when computing absolute store indices. Reset to 0 after
186    /// compaction replaces ancestors with a summary.
187    ancestor_prefix_len: Cell<usize>,
188}
189
190impl<C: ConversationStore, S: StateStore> HistoryManager<C, S> {
191    pub async fn new_with_history(
192        conversation_store: C,
193        state_store: S,
194        thread_id: ThreadId,
195    ) -> Result<Self, BoxError> {
196        let _ = conversation_store.ensure_root_thread(&thread_id).await;
197
198        let ancestor_chain: Vec<ThreadId> = conversation_store
199            .get_ancestor_chain(&thread_id)
200            .await
201            .map(|links| links.iter().map(|(tid, _)| tid.clone()).collect())
202            .unwrap_or_default();
203        let root_thread_id = ancestor_chain
204            .first()
205            .cloned()
206            .unwrap_or_else(|| thread_id.clone());
207
208        let (history, compacted_up_to, ancestor_prefix_len) = conversation_store
209            .load_history_with_ancestors(&thread_id)
210            .await
211            .map_err(|e| Box::new(e) as BoxError)?;
212
213        let metadata = state_store
214            .get_metadata(&root_thread_id)
215            .await
216            .unwrap_or(None);
217
218        let processed_message_ids = state_store
219            .get_processed_ids(&thread_id)
220            .await
221            .unwrap_or_default();
222
223        Ok(Self {
224            conversation_store,
225            state_store,
226            thread_id,
227            root_thread_id,
228            ancestor_chain,
229            history: RefCell::new(history),
230            processed_message_ids: RefCell::new(processed_message_ids),
231            metadata: RefCell::new(metadata),
232            pending_items: RefCell::new(Vec::new()),
233            unvalidated_items: RefCell::new(Vec::new()),
234            turn_buffer: RefCell::new(Vec::new()),
235            interrupted_tool_calls: RefCell::new(Vec::new()),
236            compacted_up_to: RefCell::new(compacted_up_to),
237            ancestor_prefix_len: Cell::new(ancestor_prefix_len),
238        })
239    }
240
241    pub fn handle_content(
242        &self,
243        message: InfinityMessage,
244        message_id: String,
245    ) -> Result<bool, BoxError> {
246        if self.processed_message_ids.borrow().contains(&message_id) {
247            tracing::info!("Message {} already processed, skipping", message_id);
248            return Ok(false);
249        }
250
251        // SubscriptionEvent with an embedded invocation is self-contained —
252        // treat it like a non-tool-result (may interrupt a pending call).
253        let is_self_contained_subscription = matches!(
254            message,
255            InfinityMessage::SubscriptionEvent {
256                invocation: Some(_),
257                ..
258            }
259        );
260
261        if !is_self_contained_subscription {
262            if let Some(tool_result) = message.tool_result() {
263                match self.match_tool_result(&tool_result.id) {
264                    ToolResultMatch::Unanswered => {}
265                    ToolResultMatch::AlreadyAnswered => {
266                        tracing::info!(
267                            "Tool call {} already processed, ignoring duplicate",
268                            tool_result.id
269                        );
270                        self.processed_message_ids.borrow_mut().insert(message_id);
271                        return Ok(false);
272                    }
273                    ToolResultMatch::NoPendingCall => {
274                        tracing::info!(
275                            "Got tool call result for wrong call, ignoring {:?}",
276                            tool_result
277                        );
278                        return Ok(false);
279                    }
280                }
281            } else {
282                self.interrupt_pending_tool_call();
283            }
284        } else {
285            self.interrupt_pending_tool_call();
286        }
287
288        self.append_unvalidated(message, message_id.clone());
289        self.processed_message_ids.borrow_mut().insert(message_id);
290        Ok(true)
291    }
292
293    /// Match an incoming tool result against the tail of history, walking
294    /// backwards just in time instead of maintaining a separate index of
295    /// answered tool calls. The walk only crosses tool calls and tool
296    /// results (future-proofing for concurrent calls, e.g. `tc tc tr tr`);
297    /// anything else — user text, assistant content, subscription events —
298    /// is a turn boundary: every call before it is settled, so the result
299    /// must be stale.
300    fn match_tool_result(&self, result_id: &str) -> ToolResultMatch {
301        for msg in self.history.borrow().iter().rev() {
302            match msg {
303                InfinityMessage::ToolCall { call, .. } => {
304                    if call.id == result_id {
305                        // Its result would have been seen before the call in
306                        // a backwards walk, so this call is unanswered.
307                        return ToolResultMatch::Unanswered;
308                    }
309                }
310                InfinityMessage::ToolResult { result, .. } => {
311                    if result.id == result_id {
312                        return ToolResultMatch::AlreadyAnswered;
313                    }
314                }
315                _ => return ToolResultMatch::NoPendingCall,
316            }
317        }
318        ToolResultMatch::NoPendingCall
319    }
320
321    /// If the last history entry is an unanswered tool call, inject a
322    /// synthetic "interrupted" result. (A tool call at the tail is unanswered
323    /// by construction: its result would have been appended after it.)
324    fn interrupt_pending_tool_call(&self) {
325        let last_call = self.history.borrow().last().and_then(|m| {
326            if let InfinityMessage::ToolCall { call, .. } = m {
327                Some(call.clone())
328            } else {
329                None
330            }
331        });
332        if let Some(tool_call) = last_call {
333            tracing::info!("Tool call {} interrupted by incoming message", tool_call.id);
334            self.interrupted_tool_calls
335                .borrow_mut()
336                .push(ToolCallId::from(tool_call.id.clone()));
337            let synthetic_result = InfinityMessage::ToolResult {
338                result: ToolResult {
339                    id: tool_call.id.clone(),
340                    call_id: tool_call.call_id.clone(),
341                    content: vec![ToolResultContent::Text(
342                        infinity_provider_protocol::message::Text {
343                            text: TOOL_CALL_INTERRUPTED_TEXT.to_owned(),
344                        },
345                    )],
346                },
347                display_segments: None,
348            };
349            self.append_unvalidated(synthetic_result, format!("{}-interrupted", tool_call.id));
350        }
351    }
352
353    /// Buffer a streamed assistant chunk for the current turn. The chunk is
354    /// **not** committed to `history` yet — it accumulates in `turn_buffer`
355    /// until the turn reaches a flush point ([`Self::flush_turn`]) or is
356    /// discarded on failure ([`Self::discard_turn`]). This keeps partial,
357    /// possibly-abandoned turns out of committed history.
358    pub fn handle_completion(
359        &self,
360        completion: &StreamChunk,
361        completion_id: String,
362        display_as: Option<String>,
363    ) {
364        if self.processed_message_ids.borrow().contains(&completion_id) {
365            return;
366        }
367        // Coalesce consecutive streamed text chunks into a single buffer entry
368        // so that a multi-chunk assistant response is persisted as one message
369        // rather than one row per chunk (which blows up disk usage).
370        if let StreamChunk::Text(text) = completion
371            && self.try_merge_buffer_text(text)
372        {
373            return;
374        }
375        let infinity_message = match completion {
376            StreamChunk::Text(text) => InfinityMessage::Assistant {
377                content: AssistantContent::text(text.clone()),
378            },
379            StreamChunk::Reasoning(r) => InfinityMessage::Assistant {
380                content: AssistantContent::Reasoning(r.clone()),
381            },
382            StreamChunk::ToolCall(call) => InfinityMessage::ToolCall {
383                call: call.clone(),
384                display_as,
385            },
386            StreamChunk::ToolCallDelta { .. }
387            | StreamChunk::ReasoningDelta { .. }
388            | StreamChunk::Final(_) => {
389                return;
390            }
391        };
392        self.turn_buffer.borrow_mut().push(PendingItem {
393            message: infinity_message,
394            message_id: completion_id,
395        });
396    }
397
398    /// Commit the buffered current-turn content into `history` and
399    /// `pending_items`. Called at a flush point: a completed turn (`Final`) or
400    /// a turn-ending tool call. After this, the buffered messages are part of
401    /// committed history and will be persisted by the next [`Self::sync`].
402    ///
403    /// Callers must have promoted any unvalidated inputs first (see
404    /// [`Self::mark_inputs_model_validated`]): the buffered content is model
405    /// output, and model output for a request that included unvalidated
406    /// inputs is exactly the proof required to validate them.
407    pub fn flush_turn(&self) {
408        let drained = std::mem::take(&mut *self.turn_buffer.borrow_mut());
409        for item in drained {
410            self.append_known_safe(item.message, item.message_id);
411        }
412    }
413
414    /// Like [`Self::flush_turn`], but first drops any trailing assistant
415    /// reasoning (and empty text) from the buffer so the committed turn does not
416    /// end on a reasoning block.
417    pub fn flush_turn_trimming_reasoning(&self) {
418        {
419            let mut buffer = self.turn_buffer.borrow_mut();
420            // Pop trailing reasoning and empty text together (an empty text
421            // entry between two reasoning blocks must not strand the earlier
422            // one), stopping at the first non-empty text or any other content.
423            while let Some(PendingItem {
424                message: InfinityMessage::Assistant { content },
425                ..
426            }) = buffer.last()
427            {
428                match content {
429                    AssistantContent::Reasoning(_) => {}
430                    AssistantContent::Text(text) if text.text.trim().is_empty() => {}
431                    _ => break,
432                }
433                buffer.pop();
434            }
435        }
436        self.flush_turn();
437    }
438
439    /// Drop the buffered current-turn content without committing it. Called
440    /// when a turn fails mid-stream (timeout, disconnect) and will be retried:
441    /// the retry rebuilds the request from clean committed `history`, so the
442    /// abandoned partial turn must not linger.
443    pub fn discard_turn(&self) {
444        self.turn_buffer.borrow_mut().clear();
445    }
446
447    pub fn turn_buffer_is_empty(&self) -> bool {
448        self.turn_buffer.borrow().is_empty()
449    }
450
451    /// Committed history followed by the in-flight buffered turn. Used for
452    /// mid-turn subscriber replay so a client connecting while the model is
453    /// streaming still sees the partial assistant message.
454    pub fn current_turn_view(&self) -> Vec<InfinityMessage> {
455        let history = self.history.borrow();
456        let buffer = self.turn_buffer.borrow();
457        history
458            .iter()
459            .cloned()
460            .chain(buffer.iter().map(|item| item.message.clone()))
461            .collect()
462    }
463
464    /// Append an *input* (user text, tool result, injected synthetic
465    /// result) to the in-memory history. The item is not persistable yet:
466    /// it stays in `unvalidated_items` until the model produces output for
467    /// it (see [`Self::mark_inputs_model_validated`]).
468    fn append_unvalidated(&self, message: InfinityMessage, message_id: String) {
469        assert!(
470            self.turn_buffer.borrow().is_empty(),
471            "bug: append_unvalidated called with un-flushed turn_buffer content"
472        );
473
474        self.history.borrow_mut().push(message.clone());
475        self.unvalidated_items.borrow_mut().push(PendingItem {
476            message,
477            message_id,
478        });
479    }
480
481    /// Append *model-produced* content (assistant text/reasoning, tool
482    /// calls) to the in-memory history and the known-safe persistence
483    /// queue.
484    fn append_known_safe(&self, message: InfinityMessage, message_id: String) {
485        assert!(
486            self.turn_buffer.borrow().is_empty(),
487            "bug: append_known_safe called with un-flushed turn_buffer content"
488        );
489        // Sequentiality safeguard: known-safe content must never be
490        // committed while unvalidated inputs exist, otherwise sync() would
491        // persist the model's output without the inputs it answers.
492        assert!(
493            self.unvalidated_items.borrow().is_empty(),
494            "bug: committing model output while unvalidated inputs exist \
495             (mark_inputs_model_validated must run first)"
496        );
497
498        self.history.borrow_mut().push(message.clone());
499        self.pending_items.borrow_mut().push(PendingItem {
500            message,
501            message_id,
502        });
503    }
504
505    /// Promote all unvalidated inputs to the known-safe persistence queue.
506    ///
507    /// Called as soon as the model streams any output for a request that
508    /// included them: output means the request was accepted, i.e. the
509    /// context window did not overflow, so the inputs are safe to persist.
510    pub fn mark_inputs_model_validated(&self) {
511        let drained = std::mem::take(&mut *self.unvalidated_items.borrow_mut());
512        self.pending_items.borrow_mut().extend(drained);
513    }
514
515    /// Number of inputs still awaiting model validation.
516    pub fn unvalidated_len(&self) -> usize {
517        self.unvalidated_items.borrow().len()
518    }
519
520    /// Drop all unvalidated *user* inputs: items that are neither tool
521    /// results nor subscription events (those are placeholder-replaceable,
522    /// see [`Self::replace_unvalidated_tool_results`], and answering a tool
523    /// call is always better than stranding it). Dropped items are removed
524    /// from the in-memory history and their dedup IDs forgotten so a
525    /// redelivery is not silently ignored. Returns how many items were
526    /// dropped.
527    ///
528    /// Used when the model reports a context overflow: an oversized user
529    /// input has no safe substitute, and it must not be persisted (or kept
530    /// in memory) or the thread would be permanently wedged on it.
531    pub fn drop_unvalidated_user_inputs(&self) -> usize {
532        let mut unvalidated = self.unvalidated_items.borrow_mut();
533        if unvalidated.is_empty() {
534            return 0;
535        }
536        let mut history = self.history.borrow_mut();
537        // By the sequentiality invariant the unvalidated items are exactly
538        // the in-memory history tail.
539        let tail_start = history.len() - unvalidated.len();
540        let mut processed = self.processed_message_ids.borrow_mut();
541
542        let mut kept = Vec::new();
543        let mut dropped = 0;
544        for item in unvalidated.drain(..) {
545            if matches!(
546                item.message,
547                InfinityMessage::ToolResult { .. } | InfinityMessage::SubscriptionEvent { .. }
548            ) {
549                kept.push(item);
550            } else {
551                processed.remove(&item.message_id);
552                dropped += 1;
553            }
554        }
555        history.truncate(tail_start);
556        history.extend(kept.iter().map(|item| item.message.clone()));
557        *unvalidated = kept;
558        dropped
559    }
560
561    /// Replace the content of every unvalidated tool result — including the
562    /// bodies of subscription events, which carry a tool result — with
563    /// `placeholder` (in both the persistence queue and the in-memory
564    /// history). Returns `true` if at least one was replaced.
565    ///
566    /// Used on context overflow: unlike user text, a tool result cannot
567    /// simply be dropped without stranding its tool call (and a
568    /// subscription event should still record that an event arrived), but
569    /// both *can* be answered with a fixed placeholder. Bodies already
570    /// equal to the placeholder are not counted, so a second overflow
571    /// reports `false` and the caller falls back to dropping the inputs.
572    pub fn replace_unvalidated_tool_results(&self, placeholder: &str) -> bool {
573        let mut unvalidated = self.unvalidated_items.borrow_mut();
574        let mut history = self.history.borrow_mut();
575        // By the sequentiality invariant the unvalidated items are exactly
576        // the in-memory history tail.
577        let tail_start = history.len() - unvalidated.len();
578        let mut replaced = false;
579        for (i, item) in unvalidated.iter_mut().enumerate() {
580            let result = match &mut item.message {
581                InfinityMessage::ToolResult {
582                    result,
583                    display_segments,
584                } => {
585                    *display_segments = None;
586                    result
587                }
588                InfinityMessage::SubscriptionEvent { result, .. } => result.as_mut(),
589                _ => continue,
590            };
591            if matches!(
592                result.content.first(),
593                Some(ToolResultContent::Text(t)) if t.text == placeholder
594            ) {
595                continue;
596            }
597            result.content = vec![ToolResultContent::Text(
598                infinity_provider_protocol::message::Text {
599                    text: placeholder.to_owned(),
600                },
601            )];
602            history[tail_start + i] = item.message.clone();
603            replaced = true;
604        }
605        replaced
606    }
607
608    /// If the last buffered turn entry is an assistant text message, append
609    /// `text` to it and return `true`. Otherwise return `false` so the caller
610    /// pushes a new buffer entry. This coalesces consecutive text chunks within
611    /// a turn into one message.
612    fn try_merge_buffer_text(&self, text: &str) -> bool {
613        let mut buffer = self.turn_buffer.borrow_mut();
614        let Some(last) = buffer.last_mut() else {
615            return false;
616        };
617        let InfinityMessage::Assistant {
618            content: AssistantContent::Text(existing),
619        } = &mut last.message
620        else {
621            return false;
622        };
623        existing.text.push_str(text);
624        true
625    }
626
627    pub async fn sync(&self) -> Result<(), BoxError> {
628        // `sync` only persists known-safe (`pending_items`) content.
629        // Unvalidated inputs deliberately stay in memory (see the field
630        // docs); any in-flight turn must have been flushed or discarded
631        // before this point.
632        assert!(
633            self.turn_buffer.borrow().is_empty(),
634            "bug: sync() called with un-flushed turn_buffer content"
635        );
636
637        let pending_items = std::mem::take(&mut *self.pending_items.borrow_mut());
638        if pending_items.is_empty() {
639            return Ok(());
640        }
641        let msgs: Vec<(InfinityMessage, String)> = pending_items
642            .iter()
643            .map(|item| (item.message.clone(), item.message_id.clone()))
644            .collect();
645        self.conversation_store
646            .append_messages(&self.thread_id, msgs)
647            .await
648            .map_err(|e| Box::new(e) as BoxError)?;
649        // Only inputs that are not naturally idempotent need durable dedup
650        // IDs: user text and subscription events (a redelivered subscription
651        // event would mint a fresh invocation and be appended again). Tool
652        // results are deduplicated against the history tail itself, and
653        // assistant/tool-call items only exist downstream of a deduped input.
654        let msg_ids: Vec<String> = pending_items
655            .iter()
656            .filter(|i| {
657                matches!(
658                    i.message,
659                    InfinityMessage::User { .. } | InfinityMessage::SubscriptionEvent { .. }
660                )
661            })
662            .map(|i| i.message_id.clone())
663            .collect();
664        if !msg_ids.is_empty() {
665            let _ = self
666                .state_store
667                .add_processed_message_ids(&self.thread_id, msg_ids)
668                .await;
669        }
670        Ok(())
671    }
672
673    pub async fn update_metadata(&self, metadata: serde_json::Value) -> Result<(), BoxError> {
674        *self.metadata.borrow_mut() = Some(metadata.clone());
675        self.state_store
676            .set_metadata(&self.root_thread_id, metadata)
677            .await
678            .map_err(|e| Box::new(e) as BoxError)
679    }
680
681    pub fn get_metadata(&self) -> Option<serde_json::Value> {
682        self.metadata.borrow().clone()
683    }
684    /// Build the model-facing chat history. When `supports_image_input` is
685    /// `false`, image tool-result content is replaced with a text placeholder
686    /// so the history can be sent to models without image support.
687    pub fn get_history(&self, supports_image_input: bool) -> Vec<Message> {
688        self.history
689            .borrow()
690            .iter()
691            .flat_map(|m| m.clone().into_messages())
692            .map(|mut msg| {
693                if !supports_image_input {
694                    strip_image_tool_results(&mut msg);
695                }
696                msg
697            })
698            .collect()
699    }
700
701    /// Returns the full thread stack: `[root, ..ancestors, current_thread]`.
702    /// For the root thread this is just the root thread ID.
703    pub fn get_thread_stack(&self) -> Vec<ThreadId> {
704        let mut stack = self.ancestor_chain.clone();
705        stack.push(self.thread_id.clone());
706        stack
707    }
708
709    pub fn conversation_store(&self) -> &C {
710        &self.conversation_store
711    }
712
713    /// Apply the latest compaction summary: reload from store, truncate
714    /// in-memory history up to the compaction point, and prepend the summary.
715    pub async fn apply_compaction(&self) -> Result<bool, BoxError> {
716        if let Ok(Some((summary, up_to_order))) = self
717            .conversation_store
718            .load_latest_compaction_summary_up_to(&self.thread_id, None)
719            .await
720        {
721            // Compute the relative split position in the in-memory history.
722            // If a previous compaction already replaced indices 0..prev with a
723            // single summary message, the in-memory index 0 corresponds to
724            // absolute index (prev - 1) in the store (the -1 accounts for the
725            // summary message itself occupying slot 0).
726            let offset = self
727                .compacted_up_to
728                .borrow()
729                .map_or(0, |prev| prev as usize - 1);
730            // Add ancestor_prefix_len because those messages occupy the beginning
731            // of the in-memory history but are not counted by up_to_order (which
732            // is relative to this thread's own store).
733            let up_to =
734                (up_to_order as usize).saturating_sub(offset) + self.ancestor_prefix_len.get();
735            let mut history = self.history.borrow_mut();
736            if up_to <= history.len() {
737                let remaining = history.split_off(up_to);
738                *history = vec![InfinityMessage::Assistant {
739                    content: AssistantContent::text(format!(
740                        "[Compacted conversation summary]\n{}",
741                        summary
742                    )),
743                }];
744                history.extend(remaining);
745                *self.compacted_up_to.borrow_mut() = Some(up_to_order);
746                // After compaction, ancestors are consumed into the summary.
747                self.ancestor_prefix_len.set(0);
748                return Ok(true);
749            }
750        }
751        Ok(false)
752    }
753
754    /// Drain and return tool call IDs that were interrupted by new user messages.
755    /// Callers use this to send best-effort cancellation notifications to RAP
756    /// tool servers so they can abort in-flight operations.
757    pub fn take_interrupted_tool_calls(&self) -> Vec<ToolCallId> {
758        std::mem::take(&mut *self.interrupted_tool_calls.borrow_mut())
759    }
760
761    /// Find a tool call by ID, unwinding past this thread's compaction
762    /// boundary.
763    ///
764    /// Compaction folds old messages into a summary in the loaded *view*
765    /// only — the store still holds every row. A synthetic event that
766    /// references a summarized-away call (e.g. a child's close report whose
767    /// `spawn_thread` call was compacted while the parent slept) must still
768    /// resolve it, so on an in-memory miss the thread's own persisted rows
769    /// are searched as well.
770    ///
771    /// Own rows suffice because every synthetic event resolves against a
772    /// call the receiving thread owns: reports name the parent's own
773    /// `spawn_thread` call, subscription events the subscribing thread's own
774    /// call, and parent messages the copy of the spawn call seeded into the
775    /// child's store at spawn time. (Threads spawned before spawn-call
776    /// seeding saw the call only through their inherited slice; for those,
777    /// an event referencing a compacted-away inherited call is still
778    /// dropped.)
779    pub async fn find_tool_call(
780        &self,
781        tool_call_id: &str,
782    ) -> Option<infinity_provider_protocol::message::ToolCall> {
783        // Search each slice backwards: the referenced call is almost always
784        // recent (e.g. the spawn call of a still-open child).
785        let find_in = |messages: &[InfinityMessage]| {
786            messages.iter().rev().find_map(|msg| {
787                if let InfinityMessage::ToolCall { call, .. } = msg
788                    && call.id == tool_call_id
789                {
790                    Some(call.clone())
791                } else {
792                    None
793                }
794            })
795        };
796
797        if let Some(call) = find_in(&self.history.borrow()) {
798            return Some(call);
799        }
800
801        // This thread's own rows, including any compacted-away prefix.
802        match self
803            .conversation_store
804            .load_history_up_to(&self.thread_id, None, None)
805            .await
806        {
807            Ok(messages) => find_in(&messages),
808            Err(e) => {
809                tracing::warn!(
810                    "failed to load own history while resolving tool call {tool_call_id}: {e}"
811                );
812                None
813            }
814        }
815    }
816
817    /// Compute a safe spawn point that excludes trailing unanswered tool calls
818    /// and any unvalidated (not yet persistable) inputs. Returns an absolute
819    /// store order (accounting for prior compaction offset and ancestor
820    /// prefix) suitable for use as `spawn_order_override`.
821    pub fn safe_spawn_point(&self) -> usize {
822        let history = self.history.borrow();
823        // Walk the trailing run of tool calls / tool results (future-proofing
824        // for concurrent calls, e.g. `tc tc tr tr`), tracking which calls
825        // have their result. The safe point cuts before the deepest
826        // unanswered call — excluding it and everything after it. Anything
827        // else (user text, assistant content, subscription events) ends the
828        // run: every call before it is settled.
829        let mut answered: HashSet<&str> = HashSet::new();
830        let mut safe = history.len();
831        for (i, msg) in history.iter().enumerate().rev() {
832            match msg {
833                InfinityMessage::ToolCall { call, .. } => {
834                    if !answered.contains(call.id.as_str()) {
835                        safe = i;
836                    }
837                }
838                InfinityMessage::ToolResult { result, .. } => {
839                    answered.insert(result.id.as_str());
840                }
841                _ => break,
842            }
843        }
844        // Child threads inherit history *from the store*, so the spawn
845        // point must also stay before any unvalidated inputs: they occupy
846        // the in-memory history tail but have not been persisted yet.
847        let validated_len = history.len() - self.unvalidated_items.borrow().len();
848        safe = safe.min(validated_len);
849        // Convert in-memory index to absolute store order by adding the offset
850        // from any prior compaction. The -1 accounts for the compaction summary
851        // message occupying slot 0 in the in-memory history.
852        let offset = self
853            .compacted_up_to
854            .borrow()
855            .map_or(0, |prev| prev as usize - 1);
856        // Subtract ancestor_prefix_len because those messages are not in this
857        // thread's own store (they come from parent/ancestor threads).
858        safe.saturating_sub(self.ancestor_prefix_len.get()) + offset
859    }
860
861    /// Record a subscription in the current thread's metadata. The
862    /// `tool_call_id` is the ID of the tool call whose result had
863    /// `subscription: true`. Ownership is implicit — a subscription is
864    /// stored in the thread that created it.
865    pub async fn track_subscription(&self, tool_call_id: &ToolCallId<str>) -> Result<(), BoxError> {
866        self.state_store
867            .add_active_subscription(&self.thread_id, tool_call_id)
868            .await
869            .map_err(|e| Box::new(e) as BoxError)
870    }
871
872    /// Remove a subscription from the current thread's active tracking.
873    pub async fn remove_subscription(
874        &self,
875        tool_call_id: &ToolCallId<str>,
876    ) -> Result<(), BoxError> {
877        self.state_store
878            .remove_active_subscription(&self.thread_id, tool_call_id)
879            .await
880            .map_err(|e| Box::new(e) as BoxError)
881    }
882}
883
884// ═══════════════════════════════════════════════════════════════════════
885// (a) prepare_input — process the raw InputMessage into history, handling
886//     synthetics, subscription events, OAuth, dedup, closed threads.
887// ═══════════════════════════════════════════════════════════════════════
888
889pub async fn prepare_input<C, S, M>(
890    input_msg: InputMessage,
891    message_id: String,
892    current_history: &HistoryManager<C, S>,
893    conversation_store: &C,
894    message_sender: &M,
895) -> Result<PrepareResult, BoxError>
896where
897    C: ConversationStore,
898    S: StateStore,
899    M: InputSender,
900{
901    // Skip messages for closed threads
902    if conversation_store
903        .is_thread_closed(&input_msg.group_id)
904        .await
905        .unwrap_or(false)
906    {
907        tracing::warn!(
908            "Received message for closed thread {}, skipping",
909            input_msg.group_id
910        );
911        return Ok(PrepareResult::Handled);
912    }
913
914    // Handle compaction complete: apply compaction to in-memory history, no LLM needed
915    if input_msg
916        .synthetic
917        .as_ref()
918        .is_some_and(SyntheticKind::is_compaction_complete)
919    {
920        tracing::info!("Applying compaction to thread {}", input_msg.group_id);
921        current_history.apply_compaction().await?;
922        return Ok(PrepareResult::CompactionApplied);
923    }
924
925    // Handle compaction trigger: spawn a compaction child thread
926    if input_msg
927        .synthetic
928        .as_ref()
929        .is_some_and(SyntheticKind::is_compaction)
930    {
931        let spawn_call_id = ToolCallId::from(uuid::Uuid::new_v4().to_string());
932
933        // Compute a safe compaction point: exclude trailing unanswered tool calls
934        // from the compaction range so they aren't lost when apply_compaction runs.
935        let safe_point = current_history.safe_spawn_point();
936
937        let sub_thread_id = conversation_store
938            .spawn_thread(
939                &input_msg.group_id,
940                &spawn_call_id,
941                false,
942                SpawnContext::InheritUpTo(safe_point),
943            )
944            .await
945            .map_err(|e| Box::new(e) as BoxError)?;
946        conversation_store
947            .mark_thread_as_compaction(&sub_thread_id)
948            .await
949            .map_err(|e| Box::new(e) as BoxError)?;
950
951        tracing::info!(
952            "Spawned compaction thread {} for parent {}",
953            sub_thread_id,
954            input_msg.group_id
955        );
956
957        // Write spawn tool call directly to child's store.
958        // No need to prepend an "interrupted" result for trailing tool calls
959        // because the safe_spawn_point already excludes them from the child's
960        // inherited history.
961        let spawn_tool_call = InfinityMessage::ToolCall {
962            call: infinity_provider_protocol::message::ToolCall {
963                id: spawn_call_id.as_str().to_owned(),
964                call_id: None,
965                function: infinity_provider_protocol::message::ToolFunction {
966                    name: "__harness_begin_compaction__".to_owned(),
967                    arguments: serde_json::json!({}),
968                },
969            },
970            display_as: None,
971        };
972        conversation_store
973            .append_messages(
974                &sub_thread_id,
975                vec![(
976                    spawn_tool_call,
977                    format!("{}-compaction-call", spawn_call_id),
978                )],
979            )
980            .await
981            .map_err(|e| Box::new(e) as BoxError)?;
982
983        // Send child its instructions via message sender
984        let child_msg = InputMessage {
985            content: InputMessageContent::User(UserContent::ToolResult(ToolResult {
986                id: spawn_call_id.as_str().to_owned(),
987                call_id: None,
988                content: vec![ToolResultContent::Text(
989                    infinity_provider_protocol::message::Text {
990                        text: format!(
991                            "This tool call was synthetically injected by the harness. You are now INSIDE a compaction thread. You can see the full conversation history inherited from your parent thread, including all ancestor thread context. \
992                        Summarize ALL of this content into a concise but comprehensive summary that preserves: all important context, decisions made, \
993                        current task progress, relevant code changes and file paths, and any pending work. \
994                        Then call close_thread with your thread ID ({}) and include the summary in report_to_parent.",
995                            sub_thread_id
996                        ),
997                    },
998                )],
999            })),
1000            group_id: sub_thread_id.clone(),
1001            metadata: None,
1002            synthetic: None,
1003            display_as: None,
1004            subscription: false,
1005        };
1006        message_sender
1007            .send_to_input_queue(child_msg, spawn_call_id.as_str())
1008            .await
1009            .map_err(|e| Box::new(e) as BoxError)?;
1010
1011        return Ok(PrepareResult::Handled);
1012    }
1013
1014    // Update metadata if provided
1015    if let Some(metadata) = input_msg.metadata {
1016        current_history.update_metadata(metadata).await?;
1017    }
1018
1019    // Handle OAuth required messages — return to caller, don't add to history
1020    if let InputMessageContent::OAuth(oauth) = &input_msg.content {
1021        assert!(oauth.content_type == "oauth_required");
1022        tracing::info!("Received OAuth required message, returning to caller");
1023        return Ok(PrepareResult::OAuthRequired {
1024            auth_url: oauth.auth_url.clone(),
1025        });
1026    }
1027
1028    // Handle user choice required messages — return to caller, don't add to history
1029    if let InputMessageContent::UserChoice(choice) = &input_msg.content {
1030        assert!(choice.content_type == "user_choice_required");
1031        tracing::info!("Received user choice required message, returning to caller");
1032        return Ok(PrepareResult::UserChoiceRequired {
1033            id: choice.id.clone(),
1034            prompt: choice.prompt.clone(),
1035            choices: choice.choices.clone(),
1036            default: choice.default,
1037            response_url: choice.response_url.clone(),
1038        });
1039    }
1040
1041    let is_subscription = input_msg.subscription;
1042
1043    let user_content = match input_msg.content {
1044        InputMessageContent::User(content) => content,
1045        InputMessageContent::OAuth(_) | InputMessageContent::UserChoice(_) => {
1046            return Ok(PrepareResult::Handled);
1047        }
1048    };
1049
1050    // Handle synthetic tool results (subscription events / thread reports)
1051    // Capture metadata for SubscriptionEvent variant before synthetic_kind is consumed.
1052    let subscription_event_meta: Option<(ToolCallId, Option<ThreadId>)> =
1053        input_msg.synthetic.as_ref().and_then(|s| {
1054            if s.is_thread_report() || s.is_associative() || s.is_parent_message() {
1055                let child_id = if let SyntheticKind::Tagged(TaggedSyntheticKind::ThreadReport {
1056                    child_thread_id,
1057                    ..
1058                }) = s
1059                {
1060                    Some(child_thread_id.clone())
1061                } else {
1062                    None
1063                };
1064                Some((s.tool_call_id().to_owned(), child_id))
1065            } else {
1066                None
1067            }
1068        });
1069
1070    // Will be set to the synthetic invocation ToolCall for inlined subscription events.
1071    let mut subscription_invocation: Option<infinity_provider_protocol::message::ToolCall> = None;
1072
1073    let content = if let Some(synthetic_kind) = input_msg.synthetic {
1074        let original_tool_call_id = synthetic_kind.tool_call_id().to_owned();
1075        let is_final_subscription = synthetic_kind.is_final();
1076        tracing::info!(
1077            "Processing synthetic tool result for tool call: {}",
1078            original_tool_call_id
1079        );
1080
1081        // Resolve the originating tool call, unwinding past compaction
1082        // boundaries: the call may have been folded into a compaction
1083        // summary in the loaded view, but the store still holds it (and
1084        // dropping the event would lose e.g. a child's close report
1085        // forever, leaving a sleeping parent parked with no wake-up).
1086        let original_call = current_history
1087            .find_tool_call(original_tool_call_id.as_str())
1088            .await;
1089
1090        let Some(original_call) = original_call else {
1091            tracing::warn!(
1092                "Could not find original tool call for synthetic message: {}, dropping",
1093                original_tool_call_id
1094            );
1095            return Ok(PrepareResult::Handled);
1096        };
1097
1098        if synthetic_kind.is_thread_report()
1099            || synthetic_kind.is_associative()
1100            || synthetic_kind.is_parent_message()
1101        {
1102            let new_tool_call_id = uuid::Uuid::new_v4().to_string();
1103            if let UserContent::ToolResult(mut tool_result) = user_content {
1104                subscription_invocation = Some(infinity_provider_protocol::message::ToolCall {
1105                    id: new_tool_call_id.clone(),
1106                    call_id: None,
1107                    function: infinity_provider_protocol::message::ToolFunction {
1108                        name: "receive_event__injected".to_owned(),
1109                        arguments: serde_json::json!({
1110                            "original_tool_name": original_call.function.name,
1111                            "original_tool_call_id": original_tool_call_id,
1112                            "original_args": original_call.function.arguments,
1113                        }),
1114                    },
1115                });
1116                tool_result.id = new_tool_call_id;
1117                // Remove subscription if this is the final event
1118                if is_final_subscription {
1119                    current_history
1120                        .remove_subscription(&original_tool_call_id)
1121                        .await
1122                        .ok();
1123                }
1124                UserContent::ToolResult(tool_result)
1125            } else {
1126                return Err("Synthetic message is not a tool result".into());
1127            }
1128        } else {
1129            // Subscription events spawn a new subthread via message sender
1130            tracing::info!(
1131                "Spawning subthread for subscription event from tool call: {}",
1132                original_tool_call_id
1133            );
1134
1135            // Compute safe point excluding trailing unanswered tool calls,
1136            // so the child doesn't inherit them as "interrupted".
1137            let safe_point = current_history.safe_spawn_point();
1138
1139            let sub_thread_id = conversation_store
1140                .spawn_thread(
1141                    &input_msg.group_id,
1142                    &original_tool_call_id,
1143                    true,
1144                    SpawnContext::InheritUpTo(safe_point),
1145                )
1146                .await
1147                .map_err(|e| Box::new(e) as BoxError)?;
1148
1149            tracing::info!(
1150                "Created subthread {} for subscription event in parent {}",
1151                sub_thread_id,
1152                input_msg.group_id
1153            );
1154
1155            let event_call_id = ToolCallId::from(uuid::Uuid::new_v4().to_string());
1156            let spawn_call_id = ToolCallId::from(uuid::Uuid::new_v4().to_string());
1157
1158            let event_content = if let UserContent::ToolResult(mut tool_result) = user_content {
1159                tool_result.id = event_call_id.as_str().to_owned();
1160                tool_result.call_id = None;
1161                tool_result
1162            } else {
1163                return Err("Synthetic subscription event is not a tool result".into());
1164            };
1165
1166            // No need to prepend an "interrupted" result for trailing tool calls
1167            // because the safe_spawn_point already excludes them from the child's
1168            // inherited history.
1169            let mut child_messages: Vec<(InfinityMessage, String)> = Vec::new();
1170
1171            // Write event + spawn tool calls directly to child's store
1172            let event_tool_call = InfinityMessage::ToolCall {
1173                call: infinity_provider_protocol::message::ToolCall {
1174                    id: event_call_id.as_str().to_owned(),
1175                    call_id: None,
1176                    function: infinity_provider_protocol::message::ToolFunction {
1177                        name: "receive_event__injected".to_owned(),
1178                        arguments: serde_json::json!({
1179                            "original_tool_name": original_call.function.name,
1180                            "original_tool_call_id": original_tool_call_id,
1181                            "original_args": original_call.function.arguments,
1182                        }),
1183                    },
1184                },
1185                display_as: None,
1186            };
1187            let spawn_tool_call = InfinityMessage::ToolCall {
1188                call: infinity_provider_protocol::message::ToolCall {
1189                    id: spawn_call_id.as_str().to_owned(),
1190                    call_id: None,
1191                    function: infinity_provider_protocol::message::ToolFunction {
1192                        name: "spawn_thread".to_owned(),
1193                        arguments: serde_json::json!({
1194                            "instructions": "Spawning thread to process incoming event."
1195                        }),
1196                    },
1197                },
1198                display_as: None,
1199            };
1200            let spawn_tool_result = InfinityMessage::ToolResult {
1201                result: ToolResult {
1202                    id: spawn_call_id.as_str().to_owned(),
1203                    call_id: None,
1204                    content: vec![ToolResultContent::Text(
1205                        infinity_provider_protocol::message::Text {
1206                            text: format!(
1207                                "You are now INSIDE the thread for processing the single event above. Your thread ID is {}, the parent which is still subscribing is {}. Process the single subscription event above, report to the parent if appropriate, then close the thread after processing this event. Your outputs are NOT VISIBLE to the user, if you want to show them something, send a report to your parent.",
1208                                sub_thread_id, input_msg.group_id
1209                            ),
1210                        },
1211                    )],
1212                },
1213                display_segments: None,
1214            };
1215            child_messages.extend(vec![
1216                (spawn_tool_call, format!("{}-spawn-call", spawn_call_id)),
1217                (spawn_tool_result, format!("{}-spawn-result", spawn_call_id)),
1218                (event_tool_call, format!("{}-event-call", event_call_id)),
1219            ]);
1220            conversation_store
1221                .append_messages(&sub_thread_id, child_messages)
1222                .await
1223                .map_err(|e| Box::new(e) as BoxError)?;
1224
1225            // Send child its instructions via message sender
1226            let child_msg = InputMessage {
1227                content: InputMessageContent::User(UserContent::ToolResult(event_content)),
1228                group_id: sub_thread_id.clone(),
1229                metadata: None,
1230                synthetic: None,
1231                display_as: None,
1232                subscription: false,
1233            };
1234            message_sender
1235                .send_to_input_queue(child_msg, event_call_id.as_str())
1236                .await
1237                .map_err(|e| Box::new(e) as BoxError)?;
1238
1239            // Remove subscription if this is the final event
1240            if is_final_subscription {
1241                current_history
1242                    .remove_subscription(&original_tool_call_id)
1243                    .await
1244                    .ok();
1245            }
1246
1247            return Ok(PrepareResult::Handled);
1248        }
1249    } else {
1250        user_content
1251    };
1252
1253    // Capture tool call ID before `content` is moved, so we can track
1254    // subscriptions after the message is added to history.
1255    let subscription_tool_call_id = if is_subscription {
1256        match &content {
1257            UserContent::ToolResult(result) => Some(result.id.clone()),
1258            _ => None,
1259        }
1260    } else {
1261        None
1262    };
1263
1264    let infinity_msg = if let Some((tool_call_id, child_thread_id)) = subscription_event_meta {
1265        if let UserContent::ToolResult(result) = content {
1266            InfinityMessage::SubscriptionEvent {
1267                result: Box::new(result),
1268                tool_call_id,
1269                child_thread_id,
1270                invocation: subscription_invocation.map(Box::new),
1271            }
1272        } else {
1273            InfinityMessage::User { content }
1274        }
1275    } else {
1276        match content {
1277            UserContent::ToolResult(result) => InfinityMessage::ToolResult {
1278                result,
1279                display_segments: input_msg.display_as.clone(),
1280            },
1281            other => InfinityMessage::User { content: other },
1282        }
1283    };
1284
1285    let is_new = current_history.handle_content(infinity_msg, message_id.clone())?;
1286
1287    if !is_new {
1288        tracing::info!("Message was duplicate or ignored, skipping agent processing");
1289        return Ok(PrepareResult::Handled);
1290    }
1291
1292    // Track subscription if this tool result started one
1293    if let Some(ref tool_call_id) = subscription_tool_call_id {
1294        tracing::info!(
1295            "Tracking subscription {} in thread {}",
1296            tool_call_id,
1297            current_history.thread_id
1298        );
1299        current_history
1300            .track_subscription(ToolCallId::from_ref(tool_call_id))
1301            .await?;
1302    }
1303
1304    Ok(PrepareResult::Ready)
1305}
1306
1307/// Compute the [`AgentEvent`] for an input that was just accepted into
1308/// history. Returns `None` for inputs with no display representation.
1309pub fn input_event<C, S>(
1310    current_history: &HistoryManager<C, S>,
1311    input_msg: &InputMessage,
1312) -> Option<AgentEvent>
1313where
1314    C: ConversationStore,
1315    S: StateStore,
1316{
1317    if let Some(synth) = input_msg.synthetic.as_ref() {
1318        if let InputMessageContent::User(UserContent::ToolResult(res)) = &input_msg.content
1319            && let Some(ToolResultContent::Text(text)) = res.content.first()
1320        {
1321            // A thread report is named after its child thread; everything
1322            // else is named after the originating tool call. That call may
1323            // have been folded into a compaction summary (it still resolves
1324            // from the store for processing, see
1325            // [`HistoryManager::find_tool_call`]), in which case the display
1326            // falls back to the call ID rather than hiding the event.
1327            let name = if let SyntheticKind::Tagged(TaggedSyntheticKind::ThreadReport {
1328                ref child_thread_id,
1329                ..
1330            }) = *synth
1331            {
1332                format!("Report from child thread {}", child_thread_id)
1333            } else {
1334                let orig_call = current_history.get_history(true).into_iter().find(|h| {
1335                    if let Message::Assistant { content, .. } = h
1336                        && let Some(AssistantContent::ToolCall(c)) = content.first()
1337                    {
1338                        c.id == synth.tool_call_id().as_str()
1339                    } else {
1340                        false
1341                    }
1342                });
1343                if let Some(Message::Assistant { content, .. }) = orig_call
1344                    && let Some(AssistantContent::ToolCall(c)) = content.first()
1345                {
1346                    format!("{}({})", c.function.name, c.function.arguments)
1347                } else {
1348                    format!("Event for tool call {}", synth.tool_call_id())
1349                }
1350            };
1351            return Some(AgentEvent::SubscriptionEvent {
1352                name,
1353                text: text.text.clone(),
1354            });
1355        }
1356        None
1357    } else if let InputMessageContent::User(UserContent::ToolResult(res)) = &input_msg.content
1358        && let Some(ToolResultContent::Text(text)) = res.content.first()
1359    {
1360        Some(AgentEvent::ToolResult {
1361            segments: rap_protocol::build_display_segments(
1362                input_msg.display_as.as_deref(),
1363                &text.text,
1364            ),
1365        })
1366    } else if let InputMessageContent::User(UserContent::Text(ref text)) = input_msg.content {
1367        let display_text = text.text.strip_prefix("<interrupt>").unwrap_or(&text.text);
1368        Some(AgentEvent::UserInput {
1369            text: display_text.to_owned(),
1370        })
1371    } else {
1372        None
1373    }
1374}
1375
1376// ═══════════════════════════════════════════════════════════════════════
1377// (b) run_completion — yields CompletionEvent items (text chunks and a
1378//     terminal Action). Handles stream errors and unknown tools internally.
1379// ═══════════════════════════════════════════════════════════════════════
1380
1381/// Placeholder text substituted for image tool-result content when the
1382/// active model does not support image inputs.
1383pub const IMAGE_OMITTED_PLACEHOLDER: &str =
1384    "[image omitted: the current model does not support image inputs]";
1385
1386/// Fixed text substituted for a pending tool result when the model reports
1387/// a context overflow ([`ErrorClass::ContextOverflow`]): the oversized
1388/// result cannot be sent, but its tool call must still be answered, so the
1389/// call is resolved with this placeholder instead.
1390pub const TOOL_RESULT_TOO_LARGE_PLACEHOLDER: &str =
1391    "[tool result omitted: it was too large for the model's remaining context window]";
1392
1393/// Text used to settle a tool call as interrupted. Injected when a user
1394/// message arrives while the call is unanswered, and also substituted for a
1395/// pending tool result during overflow recovery when user input had to be
1396/// dropped: the next thing the model sees is a fresh user message, and
1397/// "interrupted by user" describes that situation accurately, whereas a
1398/// "too large" placeholder could mislead the model into re-running the
1399/// tool.
1400pub const TOOL_CALL_INTERRUPTED_TEXT: &str = "Tool call interrupted by user";
1401
1402/// Maximum number of retries for one completion round (transient and
1403/// throttled provider errors).
1404const MAX_COMPLETION_RETRIES: u32 = 10;
1405/// Backoff before retrying a [`ErrorClass::Throttled`] provider error.
1406const THROTTLED_RETRY_DELAY: Duration = Duration::from_secs(30);
1407/// Backoff before retrying a [`ErrorClass::Transient`] request-initiation
1408/// error.
1409const TRANSIENT_RETRY_DELAY: Duration = Duration::from_secs(5);
1410
1411/// How [`run_completion`] recovered from a provider-declared context
1412/// overflow (see [`ErrorClass::ContextOverflow`]).
1413enum OverflowRecovery {
1414    /// The not-known-safe queue was exactly one replaceable item (tool
1415    /// result / subscription event) — the culprit is unambiguous, so it was
1416    /// replaced with [`TOOL_RESULT_TOO_LARGE_PLACEHOLDER`] and the
1417    /// completion should be retried.
1418    RetryWithPlaceholder,
1419    /// Anything else: replaceable items were settled with
1420    /// [`TOOL_CALL_INTERRUPTED_TEXT`], user inputs were dropped (count
1421    /// `usize`, they have no safe substitute), and the completion must
1422    /// fail. Dropping is never followed by a retry — the user clearly
1423    /// wanted to say something, and silently re-running the round without
1424    /// their words would act on stale instructions.
1425    DroppedInputs(usize),
1426}
1427
1428/// Recover from a context overflow.
1429///
1430/// * If exactly one unvalidated item is pending and it is replaceable (a
1431///   tool result or subscription event), it must be what overflowed:
1432///   replace its body with the "too large" placeholder and retry.
1433/// * Otherwise the culprit is ambiguous (or is user text, which has no safe
1434///   substitute): settle replaceable items with the same "interrupted by
1435///   user" text a user interruption would inject — the next thing the model
1436///   sees is a fresh user message, and a "too large" note could mislead it
1437///   into re-running the tool — drop the user inputs, and stop.
1438///
1439/// TODO(deferral): when the *committed* history is what overflows (shrinking
1440/// the fresh inputs does not help), the thread should block on a
1441/// lower-threshold compaction instead of erroring. That needs deferral
1442/// support for "wait for the in-flight compaction child", which does not
1443/// exist yet.
1444fn recover_from_overflow<C: ConversationStore, S: StateStore>(
1445    history: &HistoryManager<C, S>,
1446) -> OverflowRecovery {
1447    if history.unvalidated_len() == 1
1448        && history.replace_unvalidated_tool_results(TOOL_RESULT_TOO_LARGE_PLACEHOLDER)
1449    {
1450        return OverflowRecovery::RetryWithPlaceholder;
1451    }
1452    history.replace_unvalidated_tool_results(TOOL_CALL_INTERRUPTED_TEXT);
1453    OverflowRecovery::DroppedInputs(history.drop_unvalidated_user_inputs())
1454}
1455
1456/// Replace image tool-result content with a text placeholder, in place. Used
1457/// to sanitize the chat history before invoking a model that does not declare
1458/// image input support (see `ModelEntry::supports_image_input`).
1459fn strip_image_tool_results(msg: &mut Message) {
1460    if let Message::User { content } = msg {
1461        for c in content.iter_mut() {
1462            if let UserContent::ToolResult(result) = c {
1463                for item in result.content.iter_mut() {
1464                    if matches!(item, ToolResultContent::Image(_)) {
1465                        *item =
1466                            ToolResultContent::Text(infinity_provider_protocol::message::Text {
1467                                text: IMAGE_OMITTED_PLACEHOLDER.to_owned(),
1468                            });
1469                    }
1470                }
1471            }
1472        }
1473    }
1474}
1475
1476#[expect(
1477    clippy::too_many_arguments,
1478    reason = "completion orchestration requires many parameters"
1479)]
1480pub fn run_completion<'a: 'b, 'b, P, C, S, M>(
1481    provider: &'a P,
1482    model_id: &'a str,
1483    // Whether the active model accepts image inputs (from its `ModelEntry`).
1484    // When `false`, image tool results are replaced with a text placeholder
1485    // before the model is invoked.
1486    supports_image_input: bool,
1487    history: &'a HistoryManager<C, S>,
1488    tool_names: &'a HashSet<String>,
1489    tools: &'a [ToolDefinition],
1490    tool_registry: &'a HashMap<String, &'a dyn Tool<M>>,
1491    tool_context: &'a ToolContext<M>,
1492    group_id: &'a ThreadId,
1493    message_id: &'a str,
1494    extra_system_prompt: Option<&'a str>,
1495    cancel_rx: tokio::sync::oneshot::Receiver<()>,
1496) -> impl futures_util::Stream<Item = Result<CompletionEvent, BoxError>> + 'b
1497where
1498    P: ModelProvider + ?Sized,
1499    C: ConversationStore,
1500    S: StateStore,
1501    M: InputSender + 'static,
1502{
1503    async_stream::try_stream! {
1504        let mut cancel_rx = cancel_rx;
1505        let mut completion_counter: usize = 0;
1506        let mut is_thinking = false;
1507        let mut retry_count = 0;
1508
1509        let preamble = {
1510            let base = include_str!("default_prompt.md");
1511            let thread_info = format!("\n\nYour current thread ID is `{}`. The root thread ID is `{}`.", history.thread_id, history.root_thread_id);
1512            match extra_system_prompt {
1513                Some(extra) => format!("{}{}\n\n{}", base, thread_info, extra),
1514                None => format!("{}{}", base, thread_info),
1515            }
1516        };
1517
1518        'outer: loop {
1519            // The turn buffer should be flushed or discarded at the end of each turn.
1520            assert!(
1521                history.turn_buffer_is_empty(),
1522                "bug: entered completion loop with un-flushed turn buffer"
1523            );
1524
1525            let stream_result = provider
1526                .invoke_model(model_id, CompletionRequest {
1527                    preamble: Some(preamble.clone()),
1528                    chat_history: history.get_history(supports_image_input),
1529                    tools: tools.to_vec(),
1530                    max_tokens: None,
1531                    additional_params: None,
1532                });
1533
1534            // No initiation timeout here: request timeouts are the
1535            // provider's responsibility (e.g. infinity-provider-bedrock
1536            // applies its own 60s initiation timeout and reports it as an
1537            // `ErrorClass::Transient` error). Only cancellation ends the
1538            // wait.
1539            let stream_result = tokio::select! {
1540                r = stream_result => r,
1541                _ = &mut cancel_rx => {
1542                    tracing::info!("Completion cancelled during request initiation");
1543                    // The model never accepted this request, so this
1544                    // round's inputs remain unvalidated: they stay in
1545                    // memory for the next round but are not persisted.
1546                    return;
1547                }
1548            };
1549
1550            let mut llm_stream = match stream_result {
1551                Ok(s) => s,
1552                Err(e) => {
1553                    tracing::error!(error = %e, class = ?e.class(), "Completion stream initiation failed");
1554
1555                    match e.class() {
1556                        ErrorClass::Throttled if retry_count < MAX_COMPLETION_RETRIES => {
1557                            tracing::warn!("Stream error (rate limit), retrying...");
1558                            yield CompletionEvent::Info(format!(
1559                                "Stream error (rate limit), retrying after {} seconds...",
1560                                THROTTLED_RETRY_DELAY.as_secs()
1561                            ));
1562                            tokio::select! {
1563                                _ = tokio::time::sleep(THROTTLED_RETRY_DELAY) => {}
1564                                _ = &mut cancel_rx => {
1565                                    tracing::info!("Completion cancelled during retry wait");
1566                                    return;
1567                                }
1568                            }
1569                            retry_count += 1;
1570                            continue 'outer;
1571                        }
1572                        ErrorClass::Transient if retry_count < MAX_COMPLETION_RETRIES => {
1573                            tracing::warn!("Stream error ({e}), retrying...");
1574                            yield CompletionEvent::Info(format!("Stream error ({e}), retrying..."));
1575                            tokio::select! {
1576                                _ = tokio::time::sleep(TRANSIENT_RETRY_DELAY) => {}
1577                                _ = &mut cancel_rx => {
1578                                    tracing::info!("Completion cancelled during retry wait");
1579                                    return;
1580                                }
1581                            }
1582                            retry_count += 1;
1583                            continue 'outer;
1584                        }
1585                        ErrorClass::ContextOverflow => {
1586                            match recover_from_overflow(history) {
1587                                OverflowRecovery::RetryWithPlaceholder => {
1588                                    tracing::warn!("Context overflow: replaced oversized tool result with a placeholder, retrying...");
1589                                    yield CompletionEvent::Info("Tool result too large for the model's context window; replaced it with a placeholder and retrying...".to_owned());
1590                                    retry_count += 1;
1591                                    continue 'outer;
1592                                }
1593                                OverflowRecovery::DroppedInputs(dropped) => {
1594                                    if dropped > 0 {
1595                                        tracing::warn!("Context overflow: dropped {dropped} oversized input message(s)");
1596                                        yield CompletionEvent::Info("The last input was too large for the model's context window and has been discarded.".to_owned());
1597                                    }
1598                                    Err(Into::<BoxError>::into(e))?;
1599                                    unreachable!()
1600                                }
1601                            }
1602                        }
1603                        _ => {
1604                            Err(Into::<BoxError>::into(e))?;
1605                            unreachable!()
1606                        }
1607                    }
1608                }
1609            };
1610
1611            let mut has_emitted_tool_call = false;
1612            let mut should_loop_back = false;
1613
1614            loop {
1615                // Race between LLM output and cancellation signal.
1616                // We avoid `yield` inside `select!` (async_stream limitation)
1617                // by capturing the result into locals first.
1618                //
1619                // Deliberately no inactivity timer here: once a stream is
1620                // live it is never artificially cut off by us. Stall
1621                // handling, if any, belongs to the provider.
1622                let cancelled;
1623                let llm_next = tokio::select! {
1624                    res = llm_stream.next() => { cancelled = false; res },
1625                    _ = &mut cancel_rx => { cancelled = true; None },
1626                };
1627
1628                if cancelled {
1629                    tracing::info!("Completion cancelled");
1630                    // Terminal: keep the visible partial text, but trim trailing
1631                    // reasoning. This path fires on user interruption, so the next
1632                    // message is a user turn and must not follow a reasoning block.
1633                    // If the model produced no output yet, this is a no-op and the
1634                    // round's inputs stay unvalidated (in memory, unpersisted).
1635                    history.flush_turn_trimming_reasoning();
1636                    if is_thinking {
1637                        yield CompletionEvent::ThinkingEnd;
1638                    }
1639                    return;
1640                }
1641
1642                let Some(res) = llm_next else {
1643                    if is_thinking {
1644                        is_thinking = false;
1645                        yield CompletionEvent::ThinkingEnd;
1646                    }
1647                    if retry_count < MAX_COMPLETION_RETRIES {
1648                        // Retry rebuilds the request from committed history, so
1649                        // drop the abandoned partial turn.
1650                        history.discard_turn();
1651                        yield CompletionEvent::Info("Stream error (unexpected end), retrying...".to_owned());
1652                        tracing::warn!("Stream ended unexpectedly, discarding partial turn and retrying...");
1653                        tokio::time::sleep(Duration::from_secs(1)).await;
1654                        retry_count += 1;
1655                        continue 'outer;
1656                    } else {
1657                        // Giving up: keep visible text, trim trailing reasoning
1658                        // (the next appended message is a user turn).
1659                        history.flush_turn_trimming_reasoning();
1660                        Err(Into::<BoxError>::into("Stream ended unexpectedly"))?;
1661                        unreachable!()
1662                    }
1663                };
1664
1665                let chunk = match res {
1666                    Ok(c) => {
1667                        retry_count = 0;
1668                        // The model produced output for this request, so its
1669                        // context accepted the inputs: they are now safe to
1670                        // persist.
1671                        history.mark_inputs_model_validated();
1672                        c
1673                    },
1674                    Err(e) => {
1675                        if is_thinking {
1676                            is_thinking = false;
1677                            yield CompletionEvent::ThinkingEnd;
1678                        }
1679                        tracing::error!(error = %e, class = ?e.class(), "Completion stream error");
1680                        match e.class() {
1681                            ErrorClass::Transient if retry_count < MAX_COMPLETION_RETRIES => {
1682                                // Retry rebuilds from committed history.
1683                                history.discard_turn();
1684                                yield CompletionEvent::Info(format!("Stream error ({e}), retrying..."));
1685                                tracing::warn!("Stream error (transient), discarding partial turn and retrying...");
1686                                tokio::time::sleep(Duration::from_secs(1)).await;
1687                                retry_count += 1;
1688                                continue 'outer;
1689                            }
1690                            ErrorClass::Throttled if retry_count < MAX_COMPLETION_RETRIES => {
1691                                history.discard_turn();
1692                                yield CompletionEvent::Info(format!(
1693                                    "Stream error (rate limit), retrying after {} seconds...",
1694                                    THROTTLED_RETRY_DELAY.as_secs()
1695                                ));
1696                                tokio::select! {
1697                                    _ = tokio::time::sleep(THROTTLED_RETRY_DELAY) => {}
1698                                    _ = &mut cancel_rx => {
1699                                        tracing::info!("Completion cancelled during retry wait");
1700                                        return;
1701                                    }
1702                                }
1703                                retry_count += 1;
1704                                continue 'outer;
1705                            }
1706                            ErrorClass::ContextOverflow => {
1707                                // The request never fit the model's context;
1708                                // any partial turn is unusable.
1709                                history.discard_turn();
1710                                match recover_from_overflow(history) {
1711                                    OverflowRecovery::RetryWithPlaceholder => {
1712                                        tracing::warn!("Context overflow: replaced oversized tool result with a placeholder, retrying...");
1713                                        yield CompletionEvent::Info("Tool result too large for the model's context window; replaced it with a placeholder and retrying...".to_owned());
1714                                        retry_count += 1;
1715                                        continue 'outer;
1716                                    }
1717                                    OverflowRecovery::DroppedInputs(dropped) => {
1718                                        if dropped > 0 {
1719                                            tracing::warn!("Context overflow: dropped {dropped} oversized input message(s)");
1720                                            yield CompletionEvent::Info("The last input was too large for the model's context window and has been discarded.".to_owned());
1721                                        }
1722                                        Err(Into::<BoxError>::into(e))?;
1723                                        unreachable!()
1724                                    }
1725                                }
1726                            }
1727                            _ => {
1728                                // Giving up: keep visible text, trim trailing reasoning
1729                                // (the next appended message is a user turn).
1730                                history.flush_turn_trimming_reasoning();
1731                                Err(Into::<BoxError>::into(e))?;
1732                                unreachable!()
1733                            }
1734                        }
1735                    }
1736                };
1737
1738                // Skip incomplete reasoning chunks
1739                if let StreamChunk::Reasoning(ref r) = chunk
1740                    && r.first_signature().is_none() { continue; }
1741
1742                let completion_id = format!("{}-{}-completion-{}", group_id, message_id, completion_counter);
1743                completion_counter += 1;
1744
1745                // After the turn's first tool call, the model may keep
1746                // streaming: Bedrock emits *concurrent* tool calls in one
1747                // assistant message (each closed content block yields
1748                // another `ToolCall`), with interleaved reasoning between
1749                // them. Only the first call is executed — its result
1750                // arrives in a later round — so everything streamed after
1751                // it must be dropped here. Forwarding it would surface the
1752                // ignored calls' argument deltas and reasoning as
1753                // "thinking" that reaches clients *before* the executed
1754                // call's result, and committing trailing reasoning/text to
1755                // history after the tool call would break the
1756                // `history.last()` match when the result arrives (dropping
1757                // the result and stranding the call as unanswered). `Final`
1758                // still flows through below to flush the turn and finish
1759                // the round.
1760                if has_emitted_tool_call && !matches!(chunk, StreamChunk::Final(_)) {
1761                    tracing::info!("Ignoring post-tool-call stream content: {:?}", chunk);
1762                } else {
1763                    // Compute display_as for tool calls before inserting into history.
1764                    let tool_display_as = if let StreamChunk::ToolCall(ref call) = chunk {
1765                        let ds = tool_registry
1766                            .get(call.function.name.as_str())
1767                            .and_then(|t| t.display_script().map(String::from));
1768                        crate::tools::eval_display_script(ds.as_deref(), &call.function.arguments)
1769                    } else {
1770                        None
1771                    };
1772                    history.handle_completion(&chunk, completion_id, tool_display_as.clone());
1773                    match chunk {
1774                        StreamChunk::Text(text) => {
1775                            if is_thinking {
1776                                is_thinking = false;
1777                                yield CompletionEvent::ThinkingEnd;
1778                            }
1779                            tracing::info!("[Text] {}", &text);
1780                            yield CompletionEvent::TextChunk(text);
1781                        }
1782                        StreamChunk::ToolCall(call) => {
1783                            if is_thinking {
1784                                is_thinking = false;
1785                                yield CompletionEvent::ThinkingEnd;
1786                            }
1787                            tracing::info!("[Tool Call: {} with arguments {}]", &call.function.name, &call.function.arguments);
1788
1789                            has_emitted_tool_call = true;
1790                            // A tool call ends the turn: commit the buffered
1791                            // assistant content (any preceding text/reasoning
1792                            // plus this tool call) so it is in `history` before
1793                            // the tool-result `handle_content` calls below match
1794                            // on `history.last()`, and so an async tool call is
1795                            // persisted by the caller's `sync()` before its
1796                            // result arrives on a later turn.
1797                            history.flush_turn();
1798                            if call.function.name == "receive_event__injected" {
1799                                let tool_result = InfinityMessage::ToolResult {
1800                                    result: ToolResult {
1801                                        id: call.id.clone(),
1802                                        call_id: call.call_id.clone(),
1803                                        content: vec![ToolResultContent::Text(infinity_provider_protocol::message::Text {
1804                                            text: format!("Error: you cannot directly invoke {}, invocations will automatically be injected when events arrive.", call.function.name),
1805                                        })],
1806                                    },
1807                                    display_segments: None,
1808                                };
1809                                history.handle_content(tool_result, format!("{}-unknown-tool", call.id))?;
1810                                should_loop_back = true;
1811                                continue;
1812                            } else if !tool_names.contains(call.function.name.as_str()) {
1813                                // Unknown tool — inject error and retry the whole completion
1814                                tracing::warn!("Unknown tool '{}' called, injecting error and retrying", call.function.name);
1815                                let tool_result = InfinityMessage::ToolResult {
1816                                    result: ToolResult {
1817                                        id: call.id.clone(),
1818                                        call_id: call.call_id.clone(),
1819                                        content: vec![ToolResultContent::Text(infinity_provider_protocol::message::Text {
1820                                            text: format!("Error: tool '{}' does not exist", call.function.name),
1821                                        })],
1822                                    },
1823                                    display_segments: None,
1824                                };
1825                                history.handle_content(tool_result, format!("{}-unknown-tool", call.id))?;
1826                                should_loop_back = true;
1827                                continue;
1828                            }
1829
1830                            // Check for synchronous execution — if the tool provides
1831                            // synchronous results, inject into history immediately and
1832                            // continue the completion loop instead of returning. This
1833                            // prevents race conditions where a concurrent event makes
1834                            // the tool call appear cancelled.
1835                            let tool = tool_registry.get(call.function.name.as_str()).expect("bug: tool not found in registry after call");
1836                            if tool.supports_sync() {
1837                                history.sync().await?; // we must sync the history so that thread spawning uses the correct state
1838
1839                                // The call being executed is the (unanswered)
1840                                // history tail, so the safe spawn point cuts
1841                                // right before it — `spawn_thread` spawns the
1842                                // child there and seeds the child's own store
1843                                // with a copy of the call.
1844                                let sync_context = ToolContext {
1845                                    safe_spawn_point: Some(history.safe_spawn_point()),
1846                                    ..tool_context.clone()
1847                                };
1848                                let res = tool.execute_synchronous(
1849                                    &call.function.arguments,
1850                                    ToolCallId::from_ref(&call.id),
1851                                    call.call_id.as_deref().map(ProviderCallId::from_ref),
1852                                    &sync_context,
1853                                ).await.expect("bug: synchronous tool execution failed");
1854
1855                                yield CompletionEvent::SyncToolCall {
1856                                    tool_name: call.function.name.clone(),
1857                                    tool_args: call.function.arguments.clone(),
1858                                    display_as: tool_display_as,
1859                                };
1860                                yield CompletionEvent::SyncToolResult(res.clone());
1861
1862                                let sync_id = format!("{}-sync-result-{}", call.id, completion_counter);
1863                                completion_counter += 1;
1864                                history.handle_content(
1865                                    InfinityMessage::ToolResult {
1866                                        result: res,
1867                                        display_segments: None,
1868                                    },
1869                                    sync_id,
1870                                )?;
1871                                should_loop_back = true;
1872                            } else {
1873                                yield CompletionEvent::Action(CompletionAction::ExecuteToolCall {
1874                                    tool_name: call.function.name.clone(),
1875                                    tool_args: call.function.arguments.clone(),
1876                                    tool_call_id: ToolCallId::from(call.id.clone()),
1877                                    call_id: call.call_id.clone().map(ProviderCallId::from),
1878                                    display_as: tool_display_as,
1879                                });
1880                            }
1881                        }
1882                        StreamChunk::ToolCallDelta { content, .. } => {
1883                            match content {
1884                                ToolCallDeltaContent::Name(n) => {
1885                                    yield CompletionEvent::ThinkingChunk(format!("Invoking tool: {}", n));
1886                                }
1887                                ToolCallDeltaContent::Delta(d) => {
1888                                    yield CompletionEvent::ThinkingChunk(d)
1889                                }
1890                            }
1891                        }
1892                        StreamChunk::Reasoning(reasoning) => {
1893                            if is_thinking {
1894                                is_thinking = false;
1895                                yield CompletionEvent::ThinkingEnd;
1896                            }
1897                            tracing::info!("[Reasoning: {:?}]", reasoning.first_text());
1898                        }
1899                        StreamChunk::ReasoningDelta { text: reasoning, .. } => {
1900                            if !is_thinking {
1901                                is_thinking = true;
1902                                yield CompletionEvent::ThinkingStart;
1903                            }
1904                            yield CompletionEvent::ThinkingChunk(reasoning);
1905                        }
1906                        StreamChunk::Final(r) => {
1907                            if is_thinking {
1908                                yield CompletionEvent::ThinkingEnd;
1909                            }
1910                            tracing::info!("Received final message");
1911                            // Turn complete
1912                            history.flush_turn();
1913                            yield CompletionEvent::Action(CompletionAction::Done(r));
1914
1915                            if should_loop_back {
1916                                continue 'outer;
1917                            } else {
1918                                return;
1919                            }
1920                        }
1921                    }
1922                }
1923            }
1924        }
1925    }
1926}
1927
1928// ═══════════════════════════════════════════════════════════════════════
1929// (c) execute_action — dispatch the CompletionAction (execute tool call
1930//     or emit output).
1931// ═══════════════════════════════════════════════════════════════════════
1932
1933pub async fn execute_action<M>(
1934    action: CompletionAction,
1935    tool_registry: &HashMap<String, &dyn Tool<M>>,
1936    tool_context: &ToolContext<M>,
1937) -> Result<(), BoxError>
1938where
1939    M: InputSender + 'static,
1940{
1941    match action {
1942        CompletionAction::Done(_) => {}
1943        CompletionAction::ExecuteToolCall {
1944            tool_name,
1945            tool_args,
1946            tool_call_id,
1947            call_id,
1948            display_as: _,
1949        } => {
1950            let tool = tool_registry
1951                .get(&tool_name)
1952                .expect("tool must exist after run_completion");
1953            tool.execute(tool_args, tool_call_id, call_id, tool_context)
1954                .await?;
1955        }
1956    }
1957    Ok(())
1958}
1959
1960/// Dispatch `action`; when asynchronous tool execution fails, enqueue a
1961/// generic error `ToolResult` (with the original tool/call IDs) so the agent
1962/// can recover instead of waiting forever, then surface the original error.
1963pub(crate) async fn execute_action_with_error_result<M>(
1964    action: CompletionAction,
1965    tool_registry: &HashMap<String, &dyn Tool<M>>,
1966    tool_context: &ToolContext<M>,
1967) -> Result<(), crate::tools::ToolError>
1968where
1969    M: InputSender + 'static,
1970{
1971    let failed_tool_call = match &action {
1972        CompletionAction::ExecuteToolCall {
1973            tool_call_id,
1974            call_id,
1975            ..
1976        } => Some((tool_call_id.clone(), call_id.clone())),
1977        CompletionAction::Done(_) => None,
1978    };
1979
1980    if let Err(error) = execute_action(action, tool_registry, tool_context).await {
1981        if let Some((tool_call_id, call_id)) = failed_tool_call
1982            && let Err(send_error) = crate::tools::send_tool_error(
1983                tool_context,
1984                &tool_call_id,
1985                call_id,
1986                "Tool call failed",
1987            )
1988            .await
1989        {
1990            tracing::error!(
1991                "failed to send fallback result after tool execution error: {}",
1992                send_error
1993            );
1994        }
1995        return Err(error);
1996    }
1997
1998    Ok(())
1999}
2000
2001#[cfg(test)]
2002#[expect(
2003    clippy::collapsible_if,
2004    clippy::type_complexity,
2005    reason = "test readability"
2006)]
2007mod tests {
2008    use super::*;
2009    use crate::message::{
2010        InputMessage, InputMessageContent, OAuthRequired, SyntheticKind, TaggedSyntheticKind,
2011    };
2012    use crate::stores::{InMemoryConversationStore, InMemoryStateStore};
2013    use crate::traits::{ConversationStore, InputSender};
2014    use async_trait::async_trait;
2015    use infinity_provider_protocol::message::{
2016        AssistantContent, Message, ToolCall, ToolFunction, ToolResult, ToolResultContent,
2017        UserContent,
2018    };
2019    use std::collections::HashSet;
2020
2021    // ── No-op InputSender ──
2022
2023    #[derive(Clone)]
2024    struct StubSender;
2025
2026    #[async_trait]
2027    impl InputSender for StubSender {
2028        type Error = std::io::Error;
2029        async fn send_to_input_queue(
2030            &self,
2031            _message: crate::message::InputMessage,
2032            _dedup_id: &str,
2033        ) -> Result<(), std::io::Error> {
2034            Ok(())
2035        }
2036    }
2037
2038    // ── Helpers ──
2039
2040    async fn make_history(
2041        store: &InMemoryConversationStore,
2042        initial_history: Vec<Message>,
2043    ) -> HistoryManager<InMemoryConversationStore, InMemoryStateStore> {
2044        let hm = HistoryManager::new_with_history(
2045            store.clone(),
2046            InMemoryStateStore::new(),
2047            "thread-1".into(),
2048        )
2049        .await
2050        .expect("create history manager");
2051        *hm.history.borrow_mut() = initial_history
2052            .into_iter()
2053            .map(InfinityMessage::from_message)
2054            .collect();
2055        hm
2056    }
2057
2058    fn user_text_msg(group_id: &str, text: &str) -> InputMessage {
2059        InputMessage {
2060            content: InputMessageContent::User(UserContent::text(text)),
2061            group_id: group_id.into(),
2062            metadata: None,
2063            synthetic: None,
2064            display_as: None,
2065            subscription: false,
2066        }
2067    }
2068
2069    fn tool_call_msg(id: &str, name: &str, args: serde_json::Value) -> Message {
2070        Message::Assistant {
2071            content: vec![AssistantContent::ToolCall(ToolCall {
2072                id: id.to_owned(),
2073                call_id: None,
2074                function: ToolFunction {
2075                    name: name.to_owned(),
2076                    arguments: args,
2077                },
2078            })],
2079        }
2080    }
2081
2082    fn tool_result_input(
2083        group_id: &str,
2084        tool_call_id: &str,
2085        result_text: &str,
2086        synthetic: Option<SyntheticKind>,
2087    ) -> InputMessage {
2088        InputMessage {
2089            content: InputMessageContent::User(UserContent::ToolResult(ToolResult {
2090                id: tool_call_id.to_owned(),
2091                call_id: None,
2092                content: vec![ToolResultContent::Text(
2093                    infinity_provider_protocol::message::Text {
2094                        text: result_text.to_owned(),
2095                    },
2096                )],
2097            })),
2098            group_id: group_id.into(),
2099            metadata: None,
2100            synthetic,
2101            display_as: None,
2102            subscription: false,
2103        }
2104    }
2105
2106    // ── Tests ──
2107
2108    #[tokio::test]
2109    async fn simple_user_message_on_empty_history() {
2110        let store = InMemoryConversationStore::new();
2111        let hm = make_history(&store, vec![]).await;
2112
2113        let result = prepare_input(
2114            user_text_msg("thread-1", "hello"),
2115            "msg-1".to_owned(),
2116            &hm,
2117            &store,
2118            &StubSender,
2119        )
2120        .await
2121        .expect("prepare input");
2122
2123        assert_eq!(result, PrepareResult::Ready);
2124        insta::assert_json_snapshot!(hm.history.into_inner());
2125    }
2126
2127    #[tokio::test]
2128    async fn consecutive_text_chunks_are_coalesced() {
2129        let store = InMemoryConversationStore::new();
2130        let hm = make_history(&store, vec![]).await;
2131
2132        let text_chunk = |s: &str| StreamChunk::Text(s.to_owned());
2133
2134        hm.handle_completion(&text_chunk("Hello"), "c-1".to_owned(), None);
2135        hm.handle_completion(&text_chunk(", "), "c-2".to_owned(), None);
2136        hm.handle_completion(&text_chunk("world"), "c-3".to_owned(), None);
2137
2138        // While streaming, the chunks accumulate in the buffer (not yet
2139        // committed) and coalesce into a single entry.
2140        assert_eq!(hm.turn_buffer.borrow().len(), 1);
2141        assert!(hm.history.borrow().is_empty());
2142
2143        // Committing the turn moves the single coalesced message into both the
2144        // pending items (to be persisted) and the in-memory history.
2145        hm.flush_turn();
2146        insta::assert_json_snapshot!(hm.pending_items.borrow().clone());
2147        insta::assert_json_snapshot!(hm.history.borrow().clone());
2148    }
2149
2150    #[tokio::test]
2151    async fn text_chunks_not_coalesced_across_non_text_item() {
2152        let store = InMemoryConversationStore::new();
2153        let hm = make_history(&store, vec![]).await;
2154
2155        let text_chunk = |s: &str| StreamChunk::Text(s.to_owned());
2156        let tool_call = StreamChunk::ToolCall(ToolCall {
2157            id: "tc-1".to_owned(),
2158            call_id: None,
2159            function: ToolFunction {
2160                name: "some_tool".to_owned(),
2161                arguments: serde_json::json!({}),
2162            },
2163        });
2164
2165        hm.handle_completion(&text_chunk("before"), "c-1".to_owned(), None);
2166        // A non-text item in between breaks the run of text chunks.
2167        hm.handle_completion(&tool_call, "c-2".to_owned(), None);
2168        hm.handle_completion(&text_chunk("after"), "c-3".to_owned(), None);
2169
2170        // The tool call between the two text chunks should prevent them from
2171        // being coalesced, leaving three distinct buffered items which commit
2172        // in order on flush.
2173        hm.flush_turn();
2174        insta::assert_json_snapshot!(hm.pending_items.borrow().clone());
2175        insta::assert_json_snapshot!(hm.history.borrow().clone());
2176    }
2177
2178    #[tokio::test]
2179    async fn buffered_turn_is_not_committed_until_flush() {
2180        let store = InMemoryConversationStore::new();
2181        let hm = make_history(
2182            &store,
2183            vec![Message::User {
2184                content: vec![UserContent::text("do the thing")],
2185            }],
2186        )
2187        .await;
2188
2189        let reasoning = StreamChunk::Reasoning(
2190            infinity_provider_protocol::message::Reasoning::new_with_signature(
2191                "thinking",
2192                Some("sig".to_owned()),
2193            ),
2194        );
2195        let text = StreamChunk::Text("partial answer".to_owned());
2196        hm.handle_completion(&reasoning, "c-1".to_owned(), None);
2197        hm.handle_completion(&text, "c-2".to_owned(), None);
2198
2199        // Mid-turn: committed history still ends on the user message; the
2200        // streamed content lives only in the buffer, so nothing is persisted or
2201        // sent on a rebuild.
2202        assert_eq!(hm.history.borrow().len(), 1);
2203        assert!(hm.pending_items.borrow().is_empty());
2204        assert_eq!(hm.turn_buffer.borrow().len(), 2);
2205
2206        hm.flush_turn();
2207
2208        // After flush the buffered content is committed to history + pending
2209        // items and the buffer is empty.
2210        assert_eq!(hm.history.borrow().len(), 3);
2211        assert_eq!(hm.pending_items.borrow().len(), 2);
2212        assert!(hm.turn_buffer.borrow().is_empty());
2213    }
2214
2215    #[tokio::test]
2216    async fn discard_turn_drops_buffer_leaving_history_clean() {
2217        let store = InMemoryConversationStore::new();
2218        let hm = make_history(
2219            &store,
2220            vec![Message::User {
2221                content: vec![UserContent::text("do the thing")],
2222            }],
2223        )
2224        .await;
2225
2226        let text = StreamChunk::Text("partial answer".to_owned());
2227        hm.handle_completion(&text, "c-1".to_owned(), None);
2228        assert_eq!(hm.turn_buffer.borrow().len(), 1);
2229
2230        hm.discard_turn();
2231
2232        // The abandoned partial turn is gone; committed history is untouched, so
2233        // a rebuilt request ends on the user message.
2234        assert!(hm.turn_buffer.borrow().is_empty());
2235        assert_eq!(hm.history.borrow().len(), 1);
2236        assert!(matches!(
2237            hm.history.borrow().last(),
2238            Some(InfinityMessage::User { .. })
2239        ));
2240    }
2241
2242    #[tokio::test]
2243    async fn flush_turn_trimming_reasoning_keeps_text_drops_trailing_reasoning() {
2244        let store = InMemoryConversationStore::new();
2245        let hm = make_history(
2246            &store,
2247            vec![Message::User {
2248                content: vec![UserContent::text("do the thing")],
2249            }],
2250        )
2251        .await;
2252
2253        // A turn that streamed visible text and then a (complete) reasoning
2254        // block before being abandoned — e.g. the user interrupted it.
2255        let text = StreamChunk::Text("here is the answer".to_owned());
2256        let reasoning = StreamChunk::Reasoning(
2257            infinity_provider_protocol::message::Reasoning::new_with_signature(
2258                "still thinking",
2259                Some("sig".to_owned()),
2260            ),
2261        );
2262        hm.handle_completion(&text, "c-1".to_owned(), None);
2263        hm.handle_completion(&reasoning, "c-2".to_owned(), None);
2264        assert_eq!(hm.turn_buffer.borrow().len(), 2);
2265
2266        hm.flush_turn_trimming_reasoning();
2267
2268        // The visible text is preserved, but the trailing reasoning is dropped
2269        // so committed history does not end on a reasoning block (which the next
2270        // user turn would then illegally follow).
2271        assert!(hm.turn_buffer.borrow().is_empty());
2272        let history = hm.history.borrow();
2273        assert_eq!(history.len(), 2);
2274        assert!(matches!(
2275            history.last(),
2276            Some(InfinityMessage::Assistant {
2277                content: AssistantContent::Text(t),
2278            }) if t.text == "here is the answer"
2279        ));
2280    }
2281
2282    #[tokio::test]
2283    async fn flush_turn_trimming_reasoning_trims_interleaved_empty_text() {
2284        let store = InMemoryConversationStore::new();
2285        let hm = make_history(
2286            &store,
2287            vec![Message::User {
2288                content: vec![UserContent::text("do the thing")],
2289            }],
2290        )
2291        .await;
2292
2293        // reasoning, then an empty-text entry, then more reasoning at the tail:
2294        // trimming must remove all three (the empty text must not strand the
2295        // earlier reasoning block), leaving history ending on the user message.
2296        let reasoning = |s: &str| {
2297            StreamChunk::Reasoning(
2298                infinity_provider_protocol::message::Reasoning::new_with_signature(
2299                    s,
2300                    Some("sig".to_owned()),
2301                ),
2302            )
2303        };
2304        let empty_text = StreamChunk::Text("  ".to_owned());
2305        hm.handle_completion(&reasoning("first"), "c-1".to_owned(), None);
2306        hm.handle_completion(&empty_text, "c-2".to_owned(), None);
2307        hm.handle_completion(&reasoning("second"), "c-3".to_owned(), None);
2308
2309        hm.flush_turn_trimming_reasoning();
2310
2311        assert!(hm.turn_buffer.borrow().is_empty());
2312        let history = hm.history.borrow();
2313        assert_eq!(history.len(), 1);
2314        assert!(matches!(history.last(), Some(InfinityMessage::User { .. })));
2315    }
2316
2317    #[tokio::test]
2318    async fn current_turn_view_appends_buffer_to_history() {
2319        let store = InMemoryConversationStore::new();
2320        let hm = make_history(
2321            &store,
2322            vec![Message::User {
2323                content: vec![UserContent::text("hi")],
2324            }],
2325        )
2326        .await;
2327
2328        // With an empty buffer the view equals committed history.
2329        assert_eq!(hm.current_turn_view().len(), 1);
2330
2331        let text = StreamChunk::Text("streaming...".to_owned());
2332        hm.handle_completion(&text, "c-1".to_owned(), None);
2333
2334        // Mid-turn the view includes the buffered partial message even though
2335        // it is not yet in committed history (for subscriber replay).
2336        let view = hm.current_turn_view();
2337        assert_eq!(view.len(), 2);
2338        assert_eq!(hm.history.borrow().len(), 1);
2339        assert!(matches!(
2340            view.last(),
2341            Some(InfinityMessage::Assistant { .. })
2342        ));
2343    }
2344
2345    #[tokio::test]
2346    async fn closed_thread_ignores() {
2347        let store = InMemoryConversationStore::new();
2348        store
2349            .ensure_root_thread(ThreadId::from_ref("thread-1"))
2350            .await
2351            .expect("testing");
2352        store
2353            .close_thread(ThreadId::from_ref("thread-1"))
2354            .await
2355            .expect("testing");
2356        let hm = make_history(&store, vec![]).await;
2357
2358        let result = prepare_input(
2359            user_text_msg("thread-1", "hello"),
2360            "msg-1".to_owned(),
2361            &hm,
2362            &store,
2363            &StubSender,
2364        )
2365        .await
2366        .expect("prepare input");
2367
2368        assert_eq!(result, PrepareResult::Handled);
2369        assert!(hm.history.into_inner().is_empty());
2370    }
2371
2372    #[tokio::test]
2373    async fn oauth_required_returns_auth_url() {
2374        let store = InMemoryConversationStore::new();
2375        let hm = make_history(&store, vec![]).await;
2376
2377        let input = InputMessage {
2378            content: InputMessageContent::OAuth(OAuthRequired {
2379                content_type: "oauth_required".to_owned(),
2380                id: "oauth-1".into(),
2381                call_id: None,
2382                auth_url: "https://example.com/auth".to_owned(),
2383            }),
2384            group_id: "thread-1".into(),
2385            metadata: None,
2386            synthetic: None,
2387            display_as: None,
2388            subscription: false,
2389        };
2390
2391        let result = prepare_input(input, "msg-1".to_owned(), &hm, &store, &StubSender)
2392            .await
2393            .expect("prepare input");
2394
2395        insta::assert_json_snapshot!(result);
2396        assert!(hm.history.into_inner().is_empty());
2397    }
2398
2399    #[tokio::test]
2400    async fn duplicate_message_returns_handled() {
2401        let store = InMemoryConversationStore::new();
2402        let hm = make_history(&store, vec![]).await;
2403
2404        // First call succeeds
2405        let r1 = prepare_input(
2406            user_text_msg("thread-1", "hello"),
2407            "msg-1".to_owned(),
2408            &hm,
2409            &store,
2410            &StubSender,
2411        )
2412        .await
2413        .expect("prepare input");
2414        assert!(matches!(r1, PrepareResult::Ready));
2415
2416        // Same message_id again
2417        let r2 = prepare_input(
2418            user_text_msg("thread-1", "hello"),
2419            "msg-1".to_owned(),
2420            &hm,
2421            &store,
2422            &StubSender,
2423        )
2424        .await
2425        .expect("prepare input");
2426
2427        assert_eq!(r2, PrepareResult::Handled);
2428        // History should still have only one user message
2429        insta::assert_json_snapshot!(hm.history.into_inner());
2430    }
2431
2432    #[tokio::test]
2433    async fn user_message_interrupts_pending_tool_call() {
2434        let store = InMemoryConversationStore::new();
2435        // History has a user msg, then an assistant tool call that hasn't been answered
2436        let initial = vec![
2437            Message::User {
2438                content: vec![UserContent::text("do something")],
2439            },
2440            tool_call_msg("tc-1", "some_tool", serde_json::json!({"x": 1})),
2441        ];
2442        let hm = make_history(&store, initial).await;
2443
2444        let result = prepare_input(
2445            user_text_msg("thread-1", "actually, never mind"),
2446            "msg-2".to_owned(),
2447            &hm,
2448            &store,
2449            &StubSender,
2450        )
2451        .await
2452        .expect("prepare input");
2453
2454        assert_eq!(result, PrepareResult::Ready);
2455        // Should have: original user, tool call, synthetic interrupted result, new user msg
2456        insta::assert_json_snapshot!(hm.history.into_inner());
2457    }
2458
2459    #[tokio::test]
2460    async fn tool_result_appended_to_history() {
2461        let store = InMemoryConversationStore::new();
2462        let initial = vec![
2463            Message::User {
2464                content: vec![UserContent::text("do something")],
2465            },
2466            tool_call_msg("tc-1", "some_tool", serde_json::json!({"x": 1})),
2467        ];
2468        let hm = make_history(&store, initial).await;
2469
2470        let input = tool_result_input("thread-1", "tc-1", "tool output", None);
2471
2472        let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2473            .await
2474            .expect("prepare input");
2475
2476        assert_eq!(result, PrepareResult::Ready);
2477        insta::assert_json_snapshot!(hm.history.into_inner());
2478    }
2479
2480    #[tokio::test]
2481    async fn thread_report_synthetic_event() {
2482        let store = InMemoryConversationStore::new();
2483        // Tool call already completed before the thread report arrives
2484        let initial = vec![
2485            Message::User {
2486                content: vec![UserContent::text("subscribe")],
2487            },
2488            tool_call_msg(
2489                "tc-sub",
2490                "subscribe_tool",
2491                serde_json::json!({"topic": "events"}),
2492            ),
2493            Message::User {
2494                content: vec![UserContent::ToolResult(ToolResult {
2495                    id: "tc-sub".to_owned(),
2496                    call_id: None,
2497                    content: vec![ToolResultContent::Text(
2498                        infinity_provider_protocol::message::Text {
2499                            text: "subscribed successfully".to_owned(),
2500                        },
2501                    )],
2502                })],
2503            },
2504        ];
2505        let hm = make_history(&store, initial).await;
2506
2507        let input = tool_result_input(
2508            "thread-1",
2509            "tc-sub",
2510            "thread report data",
2511            Some(SyntheticKind::Tagged(TaggedSyntheticKind::ThreadReport {
2512                tool_call_id: "tc-sub".into(),
2513                child_thread_id: "thread-1".into(),
2514            })),
2515        );
2516
2517        let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2518            .await
2519            .expect("prepare input");
2520
2521        assert_eq!(result, PrepareResult::Ready);
2522        // Should have: original user, original tool call, original result, subscription event (with embedded invocation)
2523        insta::assert_json_snapshot!(
2524            hm.history.into_inner(),
2525            { "[3].result.id" => "[uuid]", "[3].invocation.id" => "[uuid]" }
2526        );
2527    }
2528
2529    #[tokio::test]
2530    async fn thread_report_tool_interruption() {
2531        let store = InMemoryConversationStore::new();
2532        // Tool call is still pending when the thread report arrives
2533        let initial = vec![
2534            Message::User {
2535                content: vec![UserContent::text("subscribe")],
2536            },
2537            tool_call_msg(
2538                "tc-sub",
2539                "subscribe_tool",
2540                serde_json::json!({"topic": "events"}),
2541            ),
2542        ];
2543        let hm = make_history(&store, initial).await;
2544
2545        let input = tool_result_input(
2546            "thread-1",
2547            "tc-sub",
2548            "thread report data",
2549            Some(SyntheticKind::Tagged(TaggedSyntheticKind::ThreadReport {
2550                tool_call_id: "tc-sub".into(),
2551                child_thread_id: "thread-1".into(),
2552            })),
2553        );
2554
2555        let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2556            .await
2557            .expect("prepare input");
2558
2559        assert_eq!(result, PrepareResult::Ready);
2560        insta::assert_json_snapshot!(
2561            hm.history.into_inner(),
2562            { "[3].result.id" => "[uuid]", "[3].invocation.id" => "[uuid]" }
2563        );
2564    }
2565
2566    #[tokio::test]
2567    async fn subscription_event_spawned_thread() {
2568        let store = InMemoryConversationStore::new();
2569        // Tool call already completed with a result before the event arrives
2570        let initial = vec![
2571            Message::User {
2572                content: vec![UserContent::text("subscribe")],
2573            },
2574            tool_call_msg(
2575                "tc-sub",
2576                "subscribe_tool",
2577                serde_json::json!({"topic": "events"}),
2578            ),
2579            Message::User {
2580                content: vec![UserContent::ToolResult(ToolResult {
2581                    id: "tc-sub".to_owned(),
2582                    call_id: None,
2583                    content: vec![ToolResultContent::Text(
2584                        infinity_provider_protocol::message::Text {
2585                            text: "subscribed successfully".to_owned(),
2586                        },
2587                    )],
2588                })],
2589            },
2590        ];
2591        let hm = make_history(&store, initial).await;
2592
2593        let input = tool_result_input(
2594            "thread-1",
2595            "tc-sub",
2596            "event payload",
2597            Some(SyntheticKind::Tagged(
2598                TaggedSyntheticKind::SubscriptionEvent {
2599                    tool_call_id: "tc-sub".into(),
2600                    associative: false,
2601                    r#final: false,
2602                },
2603            )),
2604        );
2605
2606        let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2607            .await
2608            .expect("prepare input");
2609
2610        assert_eq!(result, PrepareResult::Handled);
2611        assert_eq!(hm.thread_id.as_str(), "thread-1");
2612    }
2613
2614    #[tokio::test]
2615    async fn subscription_event_tool_interruption() {
2616        let store = InMemoryConversationStore::new();
2617        // Tool call is still pending (no result yet) when the event arrives
2618        let initial = vec![
2619            Message::User {
2620                content: vec![UserContent::text("subscribe")],
2621            },
2622            tool_call_msg(
2623                "tc-sub",
2624                "subscribe_tool",
2625                serde_json::json!({"topic": "events"}),
2626            ),
2627        ];
2628        let hm = make_history(&store, initial).await;
2629
2630        let input = tool_result_input(
2631            "thread-1",
2632            "tc-sub",
2633            "event payload",
2634            Some(SyntheticKind::Tagged(
2635                TaggedSyntheticKind::SubscriptionEvent {
2636                    tool_call_id: "tc-sub".into(),
2637                    associative: false,
2638                    r#final: false,
2639                },
2640            )),
2641        );
2642
2643        let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2644            .await
2645            .expect("prepare input");
2646
2647        assert_eq!(result, PrepareResult::Handled);
2648        assert_eq!(hm.thread_id.as_str(), "thread-1");
2649    }
2650
2651    #[tokio::test]
2652    async fn synthetic_with_missing_tool_call_returns_handled() {
2653        let store = InMemoryConversationStore::new();
2654        // Empty history — no tool call to match
2655        let hm = make_history(&store, vec![]).await;
2656
2657        let input = tool_result_input(
2658            "thread-1",
2659            "nonexistent-tc",
2660            "some data",
2661            Some(SyntheticKind::Tagged(
2662                TaggedSyntheticKind::SubscriptionEvent {
2663                    tool_call_id: "nonexistent-tc".into(),
2664                    associative: false,
2665                    r#final: false,
2666                },
2667            )),
2668        );
2669
2670        let result = prepare_input(input, "msg-1".to_owned(), &hm, &store, &StubSender)
2671            .await
2672            .expect("prepare input");
2673
2674        assert_eq!(result, PrepareResult::Handled);
2675        assert!(hm.history.into_inner().is_empty());
2676    }
2677
2678    #[tokio::test]
2679    async fn metadata_is_updated_before_processing() {
2680        let store = InMemoryConversationStore::new();
2681        let hm = make_history(&store, vec![]).await;
2682        assert!(hm.get_metadata().is_none());
2683
2684        let input = InputMessage {
2685            content: InputMessageContent::User(UserContent::text("hi")),
2686            group_id: "thread-1".into(),
2687            metadata: Some(serde_json::json!({"user_id": "u-123"})),
2688            synthetic: None,
2689            display_as: None,
2690            subscription: false,
2691        };
2692
2693        let _ = prepare_input(input, "msg-1".to_owned(), &hm, &store, &StubSender)
2694            .await
2695            .expect("prepare input");
2696
2697        insta::assert_json_snapshot!(hm.get_metadata());
2698    }
2699
2700    #[tokio::test]
2701    async fn associative_subscription_event_inlined() {
2702        let store = InMemoryConversationStore::new();
2703        // Tool call already completed with a result before the associative event arrives
2704        let initial = vec![
2705            Message::User {
2706                content: vec![UserContent::text("run command")],
2707            },
2708            tool_call_msg(
2709                "tc-cmd",
2710                "execute_command",
2711                serde_json::json!({"command": "make build"}),
2712            ),
2713            Message::User {
2714                content: vec![UserContent::ToolResult(ToolResult {
2715                    id: "tc-cmd".to_owned(),
2716                    call_id: None,
2717                    content: vec![ToolResultContent::Text(infinity_provider_protocol::message::Text {
2718                        text: "Command is still running. Output will be streamed via subscription events.".to_owned(),
2719                    })],
2720                })],
2721            },
2722        ];
2723        let hm = make_history(&store, initial).await;
2724
2725        let input = tool_result_input(
2726            "thread-1",
2727            "tc-cmd",
2728            "build output chunk\n[exit code: 0]",
2729            Some(SyntheticKind::Tagged(
2730                TaggedSyntheticKind::SubscriptionEvent {
2731                    tool_call_id: "tc-cmd".into(),
2732                    associative: true,
2733                    r#final: false,
2734                },
2735            )),
2736        );
2737
2738        let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2739            .await
2740            .expect("prepare input");
2741
2742        assert_eq!(result, PrepareResult::Ready);
2743        // Should NOT spawn a subthread — stays in the same thread
2744        assert_eq!(hm.thread_id.as_str(), "thread-1");
2745        // Should have: original user, tool call, original result, subscription event (with embedded invocation)
2746        insta::assert_json_snapshot!(
2747            hm.history.into_inner(),
2748            { "[3].result.id" => "[uuid]", "[3].invocation.id" => "[uuid]" }
2749        );
2750    }
2751
2752    #[tokio::test]
2753    async fn associative_subscription_event_tool_interruption() {
2754        let store = InMemoryConversationStore::new();
2755        // Tool call is still pending (no result yet) when the associative event arrives
2756        let initial = vec![
2757            Message::User {
2758                content: vec![UserContent::text("run command")],
2759            },
2760            tool_call_msg(
2761                "tc-cmd",
2762                "execute_command",
2763                serde_json::json!({"command": "make build"}),
2764            ),
2765        ];
2766        let hm = make_history(&store, initial).await;
2767
2768        let input = tool_result_input(
2769            "thread-1",
2770            "tc-cmd",
2771            "build output chunk\n[exit code: 0]",
2772            Some(SyntheticKind::Tagged(
2773                TaggedSyntheticKind::SubscriptionEvent {
2774                    tool_call_id: "tc-cmd".into(),
2775                    associative: true,
2776                    r#final: false,
2777                },
2778            )),
2779        );
2780
2781        let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2782            .await
2783            .expect("prepare input");
2784
2785        assert_eq!(result, PrepareResult::Ready);
2786        // Should NOT spawn a subthread — stays in the same thread
2787        assert_eq!(hm.thread_id.as_str(), "thread-1");
2788        insta::assert_json_snapshot!(
2789            hm.history.into_inner(),
2790            { "[3].result.id" => "[uuid]", "[3].invocation.id" => "[uuid]" }
2791        );
2792    }
2793
2794    // `run_completion` tests
2795    use std::collections::HashMap;
2796
2797    use super::{CompletionAction, CompletionEvent, HistoryManager};
2798    use crate::test_helpers::{mock_provider, mock_provider_with_image_support};
2799    use crate::tools::{Tool, ToolContext};
2800    use futures_util::StreamExt;
2801    use infinity_provider_protocol::ToolDefinition;
2802    use rap_protocol::ThreadId;
2803
2804    fn tool_context() -> ToolContext<StubSender> {
2805        ToolContext {
2806            message_sender: StubSender,
2807            group_id: "thread-1".into(),
2808            callback_url: String::new(),
2809            user_id: None,
2810            thread_stack: vec!["thread-1".into()],
2811            safe_spawn_point: None,
2812        }
2813    }
2814
2815    fn no_tools() -> (
2816        HashSet<String>,
2817        Vec<ToolDefinition>,
2818        HashMap<String, &'static dyn Tool<StubSender>>,
2819    ) {
2820        (HashSet::new(), vec![], HashMap::new())
2821    }
2822
2823    // ── Tests ──
2824
2825    #[tokio::test(flavor = "current_thread")]
2826    async fn basic_text_completion() {
2827        let local = tokio::task::LocalSet::new();
2828        local
2829            .run_until(async {
2830                let (provider, mut ctrl) = mock_provider();
2831                let convo_store = InMemoryConversationStore::new();
2832                let hm = make_history(
2833                    &convo_store,
2834                    vec![Message::User {
2835                        content: vec![UserContent::text("hello")],
2836                    }],
2837                )
2838                .await;
2839                let (tool_names, tool_defs, tool_registry) = no_tools();
2840                let ctx = tool_context();
2841                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
2842
2843                // Spawn the stream consumer
2844                let handle = tokio::task::spawn_local(async move {
2845                    let thread_id = ThreadId::from("thread-1");
2846                    let stream = run_completion(
2847                        &provider,
2848                        "mock",
2849                        false,
2850                        &hm,
2851                        &tool_names,
2852                        &tool_defs,
2853                        &tool_registry,
2854                        &ctx,
2855                        &thread_id,
2856                        "msg-1",
2857                        None,
2858                        cancel_rx,
2859                    );
2860                    tokio::pin!(stream);
2861                    let mut texts = Vec::new();
2862                    let mut got_done = false;
2863                    while let Some(ev) = stream.next().await {
2864                        match ev.expect("receive stream event") {
2865                            CompletionEvent::TextChunk(t) => texts.push(t),
2866                            CompletionEvent::Action(CompletionAction::Done(_)) => {
2867                                got_done = true;
2868                            }
2869                            _ => {}
2870                        }
2871                    }
2872                    (texts, got_done)
2873                });
2874
2875                // Feed the model
2876                let _req = ctrl.next_request().await;
2877                ctrl.send_text("Hello ");
2878                ctrl.send_text("world!");
2879                ctrl.finish();
2880
2881                let (texts, got_done) = handle.await.expect("await task handle");
2882                assert_eq!(texts, vec!["Hello ", "world!"]);
2883                assert!(got_done);
2884            })
2885            .await;
2886    }
2887
2888    #[tokio::test(flavor = "current_thread")]
2889    async fn cancellation_mid_stream() {
2890        let local = tokio::task::LocalSet::new();
2891        local
2892            .run_until(async {
2893                let (provider, mut ctrl) = mock_provider();
2894                let convo_store = InMemoryConversationStore::new();
2895                let hm = make_history(
2896                    &convo_store,
2897                    vec![Message::User {
2898                        content: vec![UserContent::text("hello")],
2899                    }],
2900                )
2901                .await;
2902                let (tool_names, tool_defs, tool_registry) = no_tools();
2903                let ctx = tool_context();
2904                let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
2905
2906                let handle = tokio::task::spawn_local(async move {
2907                    let thread_id = ThreadId::from("thread-1");
2908                    let stream = run_completion(
2909                        &provider,
2910                        "mock",
2911                        false,
2912                        &hm,
2913                        &tool_names,
2914                        &tool_defs,
2915                        &tool_registry,
2916                        &ctx,
2917                        &thread_id,
2918                        "msg-1",
2919                        None,
2920                        cancel_rx,
2921                    );
2922                    tokio::pin!(stream);
2923                    let mut texts = Vec::new();
2924                    let mut got_done = false;
2925                    while let Some(ev) = stream.next().await {
2926                        match ev.expect("receive stream event") {
2927                            CompletionEvent::TextChunk(t) => texts.push(t),
2928                            CompletionEvent::Action(CompletionAction::Done(_)) => {
2929                                got_done = true;
2930                            }
2931                            _ => {}
2932                        }
2933                    }
2934                    (texts, got_done)
2935                });
2936
2937                let _req = ctrl.next_request().await;
2938                ctrl.send_text("partial");
2939                // Give the stream a moment to process the chunk
2940                tokio::task::yield_now().await;
2941                tokio::task::yield_now().await;
2942                // Cancel before finishing
2943                cancel_tx.send(()).expect("send cancel signal");
2944
2945                let (texts, got_done) = handle.await.expect("await task handle");
2946                assert_eq!(texts, vec!["partial"]);
2947                // Should NOT get Done — stream was cancelled
2948                assert!(!got_done);
2949            })
2950            .await;
2951    }
2952
2953    #[tokio::test(flavor = "current_thread")]
2954    async fn unknown_tool_injects_error_and_retries() {
2955        let local = tokio::task::LocalSet::new();
2956        local
2957            .run_until(async {
2958                let (provider, mut ctrl) = mock_provider();
2959                let convo_store = InMemoryConversationStore::new();
2960                let hm = make_history(
2961                    &convo_store,
2962                    vec![Message::User {
2963                        content: vec![UserContent::text("do it")],
2964                    }],
2965                )
2966                .await;
2967                let (tool_names, tool_defs, tool_registry) = no_tools();
2968                let ctx = tool_context();
2969                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
2970
2971                let handle = tokio::task::spawn_local(async move {
2972                    let thread_id = ThreadId::from("thread-1");
2973                    let stream = run_completion(
2974                        &provider,
2975                        "mock",
2976                        false,
2977                        &hm,
2978                        &tool_names,
2979                        &tool_defs,
2980                        &tool_registry,
2981                        &ctx,
2982                        &thread_id,
2983                        "msg-1",
2984                        None,
2985                        cancel_rx,
2986                    );
2987                    tokio::pin!(stream);
2988                    let mut texts = Vec::new();
2989                    let mut got_done = false;
2990                    while let Some(ev) = stream.next().await {
2991                        match ev.expect("receive stream event") {
2992                            CompletionEvent::TextChunk(t) => texts.push(t),
2993                            CompletionEvent::Action(CompletionAction::Done(_)) => {
2994                                got_done = true;
2995                            }
2996                            _ => {}
2997                        }
2998                    }
2999                    (texts, got_done)
3000                });
3001
3002                // Round 1: model calls unknown tool
3003                let _req = ctrl.next_request().await;
3004                ctrl.send_tool_call("tc-1", "nonexistent_tool", serde_json::json!({}));
3005                ctrl.finish();
3006
3007                // Round 2: after error injection, model retries and returns text
3008                let req2 = ctrl.next_request().await;
3009                // The history should now contain the error tool result
3010                let last_msg = req2
3011                    .chat_history
3012                    .into_iter()
3013                    .last()
3014                    .expect("bug: chat history is empty");
3015                if let Message::User { content } = &last_msg {
3016                    if let Some(UserContent::ToolResult(res)) = content.first() {
3017                        if let Some(infinity_provider_protocol::message::ToolResultContent::Text(
3018                            t,
3019                        )) = res.content.first()
3020                        {
3021                            assert!(
3022                                t.text.contains("does not exist"),
3023                                "Expected error about nonexistent tool, got: {}",
3024                                t.text
3025                            );
3026                        }
3027                    }
3028                }
3029                ctrl.send_text("ok, done");
3030                ctrl.finish();
3031
3032                let (texts, got_done) = handle.await.expect("await task handle");
3033                assert_eq!(texts, vec!["ok, done"]);
3034                assert!(got_done);
3035            })
3036            .await;
3037    }
3038
3039    #[tokio::test(flavor = "current_thread")]
3040    async fn receive_event_injected_tool_rejected() {
3041        let local = tokio::task::LocalSet::new();
3042        local
3043            .run_until(async {
3044                let (provider, mut ctrl) = mock_provider();
3045                let convo_store = InMemoryConversationStore::new();
3046                let hm = make_history(
3047                    &convo_store,
3048                    vec![Message::User {
3049                        content: vec![UserContent::text("do it")],
3050                    }],
3051                )
3052                .await;
3053                let (tool_names, tool_defs, tool_registry) = no_tools();
3054                let ctx = tool_context();
3055                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3056
3057                let handle = tokio::task::spawn_local(async move {
3058                    let thread_id = ThreadId::from("thread-1");
3059                    let stream = run_completion(
3060                        &provider,
3061                        "mock",
3062                        false,
3063                        &hm,
3064                        &tool_names,
3065                        &tool_defs,
3066                        &tool_registry,
3067                        &ctx,
3068                        &thread_id,
3069                        "msg-1",
3070                        None,
3071                        cancel_rx,
3072                    );
3073                    tokio::pin!(stream);
3074                    let mut got_done = false;
3075                    while let Some(ev) = stream.next().await {
3076                        if let Ok(CompletionEvent::Action(CompletionAction::Done(_))) = ev {
3077                            got_done = true;
3078                        }
3079                    }
3080                    got_done
3081                });
3082
3083                // Round 1: model tries to call the injected-only tool
3084                let _req = ctrl.next_request().await;
3085                ctrl.send_tool_call("tc-1", "receive_event__injected", serde_json::json!({}));
3086                ctrl.finish();
3087
3088                // Round 2: model should get error and retry
3089                let _req2 = ctrl.next_request().await;
3090                ctrl.send_text("understood");
3091                ctrl.finish();
3092
3093                let got_done = handle.await.expect("await task handle");
3094                assert!(got_done);
3095            })
3096            .await;
3097    }
3098
3099    // ── Sync tool for testing ──
3100
3101    struct EchoSyncTool;
3102
3103    #[async_trait]
3104    impl Tool<StubSender> for EchoSyncTool {
3105        fn name(&self) -> &str {
3106            "echo_sync"
3107        }
3108        fn description(&self) -> &str {
3109            "echoes args"
3110        }
3111        fn parameters(&self) -> serde_json::Value {
3112            serde_json::json!({"type": "object", "properties": {"text": {"type": "string"}}, "required": ["text"]})
3113        }
3114        async fn execute(
3115            &self,
3116            _: serde_json::Value,
3117            _: ToolCallId,
3118            _: Option<ProviderCallId>,
3119            _: &ToolContext<StubSender>,
3120        ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
3121            Ok(())
3122        }
3123        fn supports_sync(&self) -> bool {
3124            true
3125        }
3126        async fn execute_synchronous(
3127            &self,
3128            args: &serde_json::Value,
3129            id: &ToolCallId<str>,
3130            call_id: Option<&ProviderCallId<str>>,
3131            _ctx: &ToolContext<StubSender>,
3132        ) -> Option<ToolResult> {
3133            let text = args["text"].as_str().unwrap_or("?");
3134            Some(ToolResult {
3135                id: id.as_str().to_owned(),
3136                call_id: call_id.map(|c| c.as_str().to_owned()),
3137                content: vec![ToolResultContent::Text(
3138                    infinity_provider_protocol::message::Text {
3139                        text: format!("echo: {}", text),
3140                    },
3141                )],
3142            })
3143        }
3144    }
3145
3146    static ECHO_TOOL: EchoSyncTool = EchoSyncTool;
3147
3148    #[tokio::test(flavor = "current_thread")]
3149    async fn sync_tool_loops_back_without_new_stream() {
3150        let local = tokio::task::LocalSet::new();
3151        local
3152            .run_until(async {
3153                let (provider, mut ctrl) = mock_provider();
3154                let convo_store = InMemoryConversationStore::new();
3155                let hm = make_history(
3156                    &convo_store,
3157                    vec![Message::User {
3158                        content: vec![UserContent::text("echo something")],
3159                    }],
3160                )
3161                .await;
3162
3163                let mut tool_names = HashSet::new();
3164                tool_names.insert("echo_sync".to_owned());
3165                let tool_defs = vec![ToolDefinition {
3166                    name: "echo_sync".into(),
3167                    description: "echoes".into(),
3168                    parameters: serde_json::json!({"type": "object", "properties": {"text": {"type": "string"}}}),
3169                }];
3170                let mut tool_registry: HashMap<String, &dyn Tool<StubSender>> = HashMap::new();
3171                tool_registry.insert("echo_sync".into(), &ECHO_TOOL);
3172                let ctx = tool_context();
3173                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3174
3175                let handle = tokio::task::spawn_local(async move {
3176                    let thread_id = ThreadId::from("thread-1");
3177                    let stream = run_completion(
3178                        &provider,
3179                        "mock",
3180                        false,
3181                        &hm,
3182                        &tool_names,
3183                        &tool_defs,
3184                        &tool_registry,
3185                        &ctx,
3186                        &thread_id,
3187                        "msg-1",
3188                        None,
3189                        cancel_rx,
3190                    );
3191                    tokio::pin!(stream);
3192                    let mut sync_calls = Vec::new();
3193                    let mut sync_results = Vec::new();
3194                    let mut texts = Vec::new();
3195                    while let Some(ev) = stream.next().await {
3196                        match ev.expect("receive stream event") {
3197                            CompletionEvent::SyncToolCall { tool_name, .. } => sync_calls.push(tool_name),
3198                            CompletionEvent::SyncToolResult(res) => {
3199                                if let Some(ToolResultContent::Text(t)) = res.content.first() {
3200                                    sync_results.push(t.text.clone());
3201                                }
3202                            }
3203                            CompletionEvent::TextChunk(t) => texts.push(t),
3204                            _ => {}
3205                        }
3206                    }
3207                    (sync_calls, sync_results, texts)
3208                });
3209
3210                // Round 1: model calls sync tool
3211                let _req = ctrl.next_request().await;
3212                ctrl.send_tool_call("tc-1", "echo_sync", serde_json::json!({"text": "hi"}));
3213                ctrl.finish();
3214
3215                // Round 2: model sees the tool result in history and responds with text
3216                let req2 = ctrl.next_request().await;
3217                // Verify the tool result is in the history
3218                let has_echo = req2.chat_history.into_iter().any(|m| {
3219                    if let Message::User { content } = &m {
3220                        if let Some(UserContent::ToolResult(res)) = content.first() {
3221                            if let Some(ToolResultContent::Text(t)) = res.content.first() {
3222                                return t.text.contains("echo: hi");
3223                            }
3224                        }
3225                    }
3226                    false
3227                });
3228                assert!(has_echo, "Tool result should be in history for round 2");
3229                ctrl.send_text("done");
3230                ctrl.finish();
3231
3232                let (sync_calls, sync_results, texts) = handle.await.expect("await task handle");
3233                assert_eq!(sync_calls, vec!["echo_sync"]);
3234                assert_eq!(sync_results, vec!["echo: hi"]);
3235                assert_eq!(texts, vec!["done"]);
3236            })
3237            .await;
3238    }
3239
3240    // ── Image tool result handling ──
3241
3242    /// History ending in a tool result that carries both text and an image.
3243    fn image_tool_result_history() -> Vec<Message> {
3244        vec![
3245            Message::User {
3246                content: vec![UserContent::text("show me the logo")],
3247            },
3248            tool_call_msg(
3249                "tc-img",
3250                "read_file",
3251                serde_json::json!({"path": "logo.png"}),
3252            ),
3253            Message::User {
3254                content: vec![UserContent::ToolResult(ToolResult {
3255                    id: "tc-img".to_owned(),
3256                    call_id: None,
3257                    content: vec![
3258                        ToolResultContent::text("Read image file \"logo.png\""),
3259                        ToolResultContent::Image(infinity_provider_protocol::message::Image {
3260                            data: infinity_provider_protocol::message::ImageSource::Base64(
3261                                "aGVsbG8=".to_owned(),
3262                            ),
3263                            media_type: Some(
3264                                infinity_provider_protocol::message::ImageMediaType::PNG,
3265                            ),
3266                        }),
3267                    ],
3268                })],
3269            },
3270        ]
3271    }
3272
3273    /// Collect the tool-result content items of the `tc-img` tool result in a
3274    /// request's chat history.
3275    fn image_result_content(req: &CompletionRequest) -> Vec<ToolResultContent> {
3276        req.chat_history
3277            .iter()
3278            .find_map(|m| {
3279                if let Message::User { content } = m
3280                    && let Some(UserContent::ToolResult(res)) = content.first()
3281                    && res.id == "tc-img"
3282                {
3283                    Some(res.content.to_vec())
3284                } else {
3285                    None
3286                }
3287            })
3288            .expect("tool result tc-img should be in chat history")
3289    }
3290
3291    #[tokio::test(flavor = "current_thread")]
3292    async fn image_tool_result_sent_to_image_capable_model() {
3293        let local = tokio::task::LocalSet::new();
3294        local
3295            .run_until(async {
3296                let (provider, mut ctrl) = mock_provider_with_image_support(true);
3297                let convo_store = InMemoryConversationStore::new();
3298                let hm = make_history(&convo_store, image_tool_result_history()).await;
3299                let (tool_names, tool_defs, tool_registry) = no_tools();
3300                let ctx = tool_context();
3301                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3302
3303                let thread_id_owned = ThreadId::from("thread-1");
3304                let handle = tokio::task::spawn_local(async move {
3305                    let stream = run_completion(
3306                        &provider,
3307                        "mock",
3308                        true,
3309                        &hm,
3310                        &tool_names,
3311                        &tool_defs,
3312                        &tool_registry,
3313                        &ctx,
3314                        &thread_id_owned,
3315                        "msg-1",
3316                        None,
3317                        cancel_rx,
3318                    );
3319                    tokio::pin!(stream);
3320                    while stream.next().await.is_some() {}
3321                });
3322
3323                let req = ctrl.next_request().await;
3324                let content = image_result_content(&req);
3325                assert_eq!(content.len(), 2);
3326                assert!(
3327                    matches!(&content[1], ToolResultContent::Image(img)
3328                        if img.data == infinity_provider_protocol::message::ImageSource::Base64("aGVsbG8=".to_owned())),
3329                    "image content should be passed through unchanged, got {content:?}"
3330                );
3331
3332                ctrl.send_text("nice logo");
3333                ctrl.finish();
3334                handle.await.expect("await task handle");
3335            })
3336            .await;
3337    }
3338
3339    #[tokio::test(flavor = "current_thread")]
3340    async fn image_tool_result_stripped_for_non_image_model() {
3341        let local = tokio::task::LocalSet::new();
3342        local
3343            .run_until(async {
3344                // The default mock provider does not declare image support.
3345                let (provider, mut ctrl) = mock_provider();
3346                let convo_store = InMemoryConversationStore::new();
3347                let hm = make_history(&convo_store, image_tool_result_history()).await;
3348                let (tool_names, tool_defs, tool_registry) = no_tools();
3349                let ctx = tool_context();
3350                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3351
3352                let handle = tokio::task::spawn_local(async move {
3353                    let thread_id = ThreadId::from("thread-1");
3354                    let stream = run_completion(
3355                        &provider,
3356                        "mock",
3357                        false,
3358                        &hm,
3359                        &tool_names,
3360                        &tool_defs,
3361                        &tool_registry,
3362                        &ctx,
3363                        &thread_id,
3364                        "msg-1",
3365                        None,
3366                        cancel_rx,
3367                    );
3368                    tokio::pin!(stream);
3369                    while stream.next().await.is_some() {}
3370                });
3371
3372                let req = ctrl.next_request().await;
3373                let content = image_result_content(&req);
3374                assert_eq!(content.len(), 2);
3375                match &content[0] {
3376                    ToolResultContent::Text(t) => {
3377                        assert_eq!(t.text, "Read image file \"logo.png\"")
3378                    }
3379                    other => panic!("expected text content, got {other:?}"),
3380                }
3381                match &content[1] {
3382                    ToolResultContent::Text(t) => assert_eq!(t.text, IMAGE_OMITTED_PLACEHOLDER),
3383                    other => panic!("image should be replaced with placeholder, got {other:?}"),
3384                }
3385
3386                ctrl.send_text("cannot see images");
3387                ctrl.finish();
3388                handle.await.expect("await task handle");
3389            })
3390            .await;
3391    }
3392
3393    #[tokio::test(flavor = "current_thread")]
3394    async fn thinking_chunks_emitted() {
3395        let local = tokio::task::LocalSet::new();
3396        local
3397            .run_until(async {
3398                let (provider, mut ctrl) = mock_provider();
3399                let convo_store = InMemoryConversationStore::new();
3400                let hm = make_history(
3401                    &convo_store,
3402                    vec![Message::User {
3403                        content: vec![UserContent::text("think hard")],
3404                    }],
3405                )
3406                .await;
3407                let (tool_names, tool_defs, tool_registry) = no_tools();
3408                let ctx = tool_context();
3409                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3410
3411                let handle = tokio::task::spawn_local(async move {
3412                    let thread_id = ThreadId::from("thread-1");
3413                    let stream = run_completion(
3414                        &provider,
3415                        "mock",
3416                        false,
3417                        &hm,
3418                        &tool_names,
3419                        &tool_defs,
3420                        &tool_registry,
3421                        &ctx,
3422                        &thread_id,
3423                        "msg-1",
3424                        None,
3425                        cancel_rx,
3426                    );
3427                    tokio::pin!(stream);
3428                    let mut events = Vec::new();
3429                    while let Some(ev) = stream.next().await {
3430                        match ev.expect("receive stream event") {
3431                            CompletionEvent::ThinkingStart => events.push("start".to_owned()),
3432                            CompletionEvent::ThinkingEnd => events.push("end".to_owned()),
3433                            CompletionEvent::ThinkingChunk(c) => {
3434                                events.push(format!("think:{}", c))
3435                            }
3436                            CompletionEvent::TextChunk(t) => events.push(format!("text:{}", t)),
3437                            _ => {}
3438                        }
3439                    }
3440                    events
3441                });
3442
3443                let _req = ctrl.next_request().await;
3444                ctrl.send_chunk(StreamChunk::ReasoningDelta {
3445                    id: None,
3446                    text: "hmm".into(),
3447                });
3448                ctrl.send_chunk(StreamChunk::ReasoningDelta {
3449                    id: None,
3450                    text: "...".into(),
3451                });
3452                ctrl.send_text("answer");
3453                ctrl.finish();
3454
3455                let events = handle.await.expect("await task handle");
3456                assert_eq!(
3457                    events,
3458                    vec!["start", "think:hmm", "think:...", "end", "text:answer"]
3459                );
3460            })
3461            .await;
3462    }
3463
3464    #[tokio::test(flavor = "current_thread")]
3465    async fn async_tool_call_yields_execute_action() {
3466        let local = tokio::task::LocalSet::new();
3467        local
3468            .run_until(async {
3469                let (provider, mut ctrl) = mock_provider();
3470                let convo_store = InMemoryConversationStore::new();
3471                let hm = make_history(
3472                    &convo_store,
3473                    vec![Message::User {
3474                        content: vec![UserContent::text("run tool")],
3475                    }],
3476                )
3477                .await;
3478
3479                struct AsyncTool;
3480                #[async_trait]
3481                impl Tool<StubSender> for AsyncTool {
3482                    fn name(&self) -> &str {
3483                        "async_tool"
3484                    }
3485                    fn description(&self) -> &str {
3486                        "async"
3487                    }
3488                    fn parameters(&self) -> serde_json::Value {
3489                        serde_json::json!({"type": "object", "properties": {}})
3490                    }
3491                    async fn execute(
3492                        &self,
3493                        _: serde_json::Value,
3494                        _: ToolCallId,
3495                        _: Option<ProviderCallId>,
3496                        _: &ToolContext<StubSender>,
3497                    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
3498                        Ok(())
3499                    }
3500                }
3501                static ASYNC_TOOL: AsyncTool = AsyncTool;
3502
3503                let mut tool_names = HashSet::new();
3504                tool_names.insert("async_tool".to_owned());
3505                let tool_defs = vec![ToolDefinition {
3506                    name: "async_tool".into(),
3507                    description: "async".into(),
3508                    parameters: serde_json::json!({"type": "object"}),
3509                }];
3510                let mut tool_registry: HashMap<String, &dyn Tool<StubSender>> = HashMap::new();
3511                tool_registry.insert("async_tool".into(), &ASYNC_TOOL);
3512                let ctx = tool_context();
3513                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3514
3515                let handle = tokio::task::spawn_local(async move {
3516                    let thread_id = ThreadId::from("thread-1");
3517                    let stream = run_completion(
3518                        &provider,
3519                        "mock",
3520                        false,
3521                        &hm,
3522                        &tool_names,
3523                        &tool_defs,
3524                        &tool_registry,
3525                        &ctx,
3526                        &thread_id,
3527                        "msg-1",
3528                        None,
3529                        cancel_rx,
3530                    );
3531                    tokio::pin!(stream);
3532                    let mut tool_call_name = None;
3533                    while let Some(ev) = stream.next().await {
3534                        if let Ok(CompletionEvent::Action(CompletionAction::ExecuteToolCall {
3535                            tool_name,
3536                            ..
3537                        })) = ev
3538                        {
3539                            tool_call_name = Some(tool_name);
3540                        }
3541                    }
3542                    tool_call_name
3543                });
3544
3545                let _req = ctrl.next_request().await;
3546                ctrl.send_tool_call("tc-1", "async_tool", serde_json::json!({"x": 1}));
3547                ctrl.finish();
3548
3549                let name = handle.await.expect("await task handle");
3550                assert_eq!(name, Some("async_tool".to_owned()));
3551            })
3552            .await;
3553    }
3554
3555    /// Regression test: Bedrock streams *concurrent* tool calls in a single
3556    /// assistant message (each closed content block yields another
3557    /// `ToolCall`), with interleaved reasoning between them. Only the first
3558    /// call is executed — its result arrives in a later round — so everything
3559    /// streamed after it must be suppressed. Previously the ignored second
3560    /// call's name/argument deltas and the interleaved reasoning were
3561    /// forwarded as `ThinkingChunk`s, so clients saw "thinking" streaming
3562    /// *before* the executed call's `ToolResult`. Worse, the trailing
3563    /// reasoning block was committed to history after the tool call, which
3564    /// broke the `history.last()` match when the result arrived (the result
3565    /// was dropped and the call stranded as unanswered).
3566    #[tokio::test(flavor = "current_thread")]
3567    async fn concurrent_tool_calls_suppress_trailing_stream_content() {
3568        use infinity_provider_protocol::StreamChunk;
3569        let local = tokio::task::LocalSet::new();
3570        local
3571            .run_until(async {
3572                let (provider, mut ctrl) = mock_provider();
3573                let convo_store = InMemoryConversationStore::new();
3574                let hm = make_history(
3575                    &convo_store,
3576                    vec![Message::User {
3577                        content: vec![UserContent::text("run tools")],
3578                    }],
3579                )
3580                .await;
3581
3582                struct AsyncTool;
3583                #[async_trait]
3584                impl Tool<StubSender> for AsyncTool {
3585                    fn name(&self) -> &str {
3586                        "async_tool"
3587                    }
3588                    fn description(&self) -> &str {
3589                        "async"
3590                    }
3591                    fn parameters(&self) -> serde_json::Value {
3592                        serde_json::json!({"type": "object", "properties": {}})
3593                    }
3594                    async fn execute(
3595                        &self,
3596                        _: serde_json::Value,
3597                        _: ToolCallId,
3598                        _: Option<ProviderCallId>,
3599                        _: &ToolContext<StubSender>,
3600                    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
3601                        Ok(())
3602                    }
3603                }
3604                static ASYNC_TOOL2: AsyncTool = AsyncTool;
3605
3606                let mut tool_names = HashSet::new();
3607                tool_names.insert("async_tool".to_owned());
3608                let tool_defs = vec![ToolDefinition {
3609                    name: "async_tool".into(),
3610                    description: "async".into(),
3611                    parameters: serde_json::json!({"type": "object"}),
3612                }];
3613                let mut tool_registry: HashMap<String, &dyn Tool<StubSender>> = HashMap::new();
3614                tool_registry.insert("async_tool".into(), &ASYNC_TOOL2);
3615                let ctx = tool_context();
3616                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3617
3618                let handle = tokio::task::spawn_local(async move {
3619                    let thread_id = ThreadId::from("thread-1");
3620                    let stream = run_completion(
3621                        &provider,
3622                        "mock",
3623                        false,
3624                        &hm,
3625                        &tool_names,
3626                        &tool_defs,
3627                        &tool_registry,
3628                        &ctx,
3629                        &thread_id,
3630                        "msg-1",
3631                        None,
3632                        cancel_rx,
3633                    );
3634                    tokio::pin!(stream);
3635                    let mut tags: Vec<&'static str> = Vec::new();
3636                    while let Some(ev) = stream.next().await {
3637                        tags.push(match ev.expect("receive stream event") {
3638                            CompletionEvent::Info(_) => "Info",
3639                            CompletionEvent::TextChunk(_) => "Text",
3640                            CompletionEvent::ThinkingStart => "ThinkingStart",
3641                            CompletionEvent::ThinkingEnd => "ThinkingEnd",
3642                            CompletionEvent::ThinkingChunk(_) => "ThinkingChunk",
3643                            CompletionEvent::SyncToolCall { .. } => "SyncToolCall",
3644                            CompletionEvent::SyncToolResult(_) => "SyncToolResult",
3645                            CompletionEvent::Action(CompletionAction::ExecuteToolCall {
3646                                ..
3647                            }) => "ExecuteToolCall",
3648                            CompletionEvent::Action(CompletionAction::Done(_)) => "Done",
3649                        });
3650                    }
3651                    // Return the final history so the test can check that the
3652                    // executed tool call stayed the last committed entry.
3653                    let last = hm.history.borrow().last().cloned();
3654                    (tags, last)
3655                });
3656
3657                let _req = ctrl.next_request().await;
3658                // Bedrock-style single assistant message with two concurrent
3659                // tool calls and interleaved reasoning between them.
3660                ctrl.send_chunk(StreamChunk::ReasoningDelta {
3661                    id: None,
3662                    text: "planning the calls".into(),
3663                });
3664                ctrl.send_chunk(StreamChunk::Reasoning(
3665                    infinity_provider_protocol::message::Reasoning::new_with_signature(
3666                        "planning the calls",
3667                        Some("sig-1".to_owned()),
3668                    ),
3669                ));
3670                ctrl.send_chunk(StreamChunk::ToolCallDelta {
3671                    id: "tc-1".into(),
3672                    content: ToolCallDeltaContent::Name("async_tool".into()),
3673                });
3674                ctrl.send_tool_call("tc-1", "async_tool", serde_json::json!({"x": 1}));
3675                // Second concurrent call: its deltas must not leak out as
3676                // thinking, and it must not be executed.
3677                ctrl.send_chunk(StreamChunk::ToolCallDelta {
3678                    id: "tc-2".into(),
3679                    content: ToolCallDeltaContent::Name("async_tool".into()),
3680                });
3681                ctrl.send_chunk(StreamChunk::ToolCallDelta {
3682                    id: "tc-2".into(),
3683                    content: ToolCallDeltaContent::Delta("{\"x\":2}".into()),
3684                });
3685                // Interleaved reasoning after the first call.
3686                ctrl.send_chunk(StreamChunk::ReasoningDelta {
3687                    id: None,
3688                    text: "now the second call".into(),
3689                });
3690                ctrl.send_chunk(StreamChunk::Reasoning(
3691                    infinity_provider_protocol::message::Reasoning::new_with_signature(
3692                        "now the second call",
3693                        Some("sig-2".to_owned()),
3694                    ),
3695                ));
3696                ctrl.send_tool_call("tc-2", "async_tool", serde_json::json!({"x": 2}));
3697                ctrl.finish();
3698
3699                let (tags, last_history) = handle.await.expect("await task handle");
3700
3701                let action_idx = tags
3702                    .iter()
3703                    .position(|t| *t == "ExecuteToolCall")
3704                    .expect("first tool call should be executed");
3705                assert_eq!(
3706                    tags[action_idx + 1..]
3707                        .iter()
3708                        .filter(|t| **t != "Done")
3709                        .count(),
3710                    0,
3711                    "no display events may be emitted after the executed tool call \
3712                     (they would stream as thinking before the tool result); got {tags:?}"
3713                );
3714
3715                // The executed call must remain the last committed history
3716                // entry so its result matches when it arrives.
3717                match last_history {
3718                    Some(InfinityMessage::ToolCall { call, .. }) => {
3719                        assert_eq!(call.id, "tc-1");
3720                    }
3721                    other => panic!("history must end with the executed tool call, got {other:?}"),
3722                }
3723            })
3724            .await;
3725    }
3726
3727    #[tokio::test(flavor = "current_thread")]
3728    async fn stream_drop_triggers_retry() {
3729        let local = tokio::task::LocalSet::new();
3730        local
3731            .run_until(async {
3732                // When the model stream ends unexpectedly (None from next()), the loop retries.
3733                let (provider, mut ctrl) = mock_provider();
3734                let convo_store = InMemoryConversationStore::new();
3735                let hm = make_history(
3736                    &convo_store,
3737                    vec![Message::User {
3738                        content: vec![UserContent::text("go")],
3739                    }],
3740                )
3741                .await;
3742                let (tool_names, tool_defs, tool_registry) = no_tools();
3743                let ctx = tool_context();
3744                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3745
3746                let handle = tokio::task::spawn_local(async move {
3747                    let thread_id = ThreadId::from("thread-1");
3748                    let stream = run_completion(
3749                        &provider,
3750                        "mock",
3751                        false,
3752                        &hm,
3753                        &tool_names,
3754                        &tool_defs,
3755                        &tool_registry,
3756                        &ctx,
3757                        &thread_id,
3758                        "msg-1",
3759                        None,
3760                        cancel_rx,
3761                    );
3762                    tokio::pin!(stream);
3763                    let mut texts = Vec::new();
3764                    let mut info_count = 0;
3765                    while let Some(ev) = stream.next().await {
3766                        match ev.expect("receive stream event") {
3767                            CompletionEvent::TextChunk(t) => texts.push(t),
3768                            CompletionEvent::Info(_) => info_count += 1,
3769                            _ => {}
3770                        }
3771                    }
3772                    (texts, info_count)
3773                });
3774
3775                // Round 1: drop the stream without sending Final (simulates unexpected end)
3776                let _req = ctrl.next_request().await;
3777                ctrl.drop_stream();
3778
3779                // Round 2: retry should happen, model responds normally
3780                let _req2 = ctrl.next_request().await;
3781                ctrl.send_text("recovered");
3782                ctrl.finish();
3783
3784                let (texts, info_count) = handle.await.expect("await task handle");
3785                assert_eq!(texts, vec!["recovered"]);
3786                assert!(
3787                    info_count >= 1,
3788                    "Should have emitted at least one Info about retry"
3789                );
3790            })
3791            .await;
3792    }
3793
3794    #[tokio::test(flavor = "current_thread")]
3795    async fn cancellation_during_thinking_emits_thinking_end() {
3796        let local = tokio::task::LocalSet::new();
3797        local
3798            .run_until(async {
3799                let (provider, mut ctrl) = mock_provider();
3800                let convo_store = InMemoryConversationStore::new();
3801                let hm = make_history(
3802                    &convo_store,
3803                    vec![Message::User {
3804                        content: vec![UserContent::text("think")],
3805                    }],
3806                )
3807                .await;
3808                let (tool_names, tool_defs, tool_registry) = no_tools();
3809                let ctx = tool_context();
3810                let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3811
3812                let handle = tokio::task::spawn_local(async move {
3813                    let thread_id = ThreadId::from("thread-1");
3814                    let stream = run_completion(
3815                        &provider,
3816                        "mock",
3817                        false,
3818                        &hm,
3819                        &tool_names,
3820                        &tool_defs,
3821                        &tool_registry,
3822                        &ctx,
3823                        &thread_id,
3824                        "msg-1",
3825                        None,
3826                        cancel_rx,
3827                    );
3828                    tokio::pin!(stream);
3829                    let mut events = Vec::new();
3830                    while let Some(ev) = stream.next().await {
3831                        match ev.expect("receive stream event") {
3832                            CompletionEvent::ThinkingStart => events.push("start"),
3833                            CompletionEvent::ThinkingEnd => events.push("end"),
3834                            CompletionEvent::ThinkingChunk(_) => events.push("chunk"),
3835                            _ => {}
3836                        }
3837                    }
3838                    events
3839                });
3840
3841                let _req = ctrl.next_request().await;
3842                ctrl.send_chunk(StreamChunk::ReasoningDelta {
3843                    id: None,
3844                    text: "deep thought".into(),
3845                });
3846                tokio::task::yield_now().await;
3847                tokio::task::yield_now().await;
3848                cancel_tx.send(()).expect("send cancel signal");
3849
3850                let events = handle.await.expect("await task handle");
3851                // Should have: start, chunk, end (end emitted on cancellation)
3852                assert!(events.contains(&"start"));
3853                assert!(
3854                    events.last() == Some(&"end"),
3855                    "ThinkingEnd should be emitted on cancel, got: {:?}",
3856                    events
3857                );
3858            })
3859            .await;
3860    }
3861
3862    #[tokio::test(flavor = "current_thread")]
3863    async fn cancellation_after_reasoning_does_not_commit_trailing_reasoning() {
3864        // A user interrupts the model after it streamed some visible text and a
3865        // complete reasoning block. On cancel we keep the text but must trim the
3866        // trailing reasoning: the next input is a user turn, and a user message
3867        // immediately following a reasoning block is rejected by some providers.
3868        let local = tokio::task::LocalSet::new();
3869        local
3870            .run_until(async {
3871                let (provider, mut ctrl) = mock_provider();
3872                let convo_store = InMemoryConversationStore::new();
3873                let hm = std::rc::Rc::new(
3874                    make_history(
3875                        &convo_store,
3876                        vec![Message::User {
3877                            content: vec![UserContent::text("do the thing")],
3878                        }],
3879                    )
3880                    .await,
3881                );
3882                let (tool_names, tool_defs, tool_registry) = no_tools();
3883                let ctx = tool_context();
3884                let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3885
3886                let hm_task = hm.clone();
3887                let thread_id_owned = ThreadId::from("thread-1");
3888                let handle = tokio::task::spawn_local(async move {
3889                    let stream = run_completion(
3890                        &provider,
3891                        "mock",
3892                        false,
3893                        &hm_task,
3894                        &tool_names,
3895                        &tool_defs,
3896                        &tool_registry,
3897                        &ctx,
3898                        &thread_id_owned,
3899                        "msg-1",
3900                        None,
3901                        cancel_rx,
3902                    );
3903                    tokio::pin!(stream);
3904                    while stream.next().await.is_some() {}
3905                });
3906
3907                let _req = ctrl.next_request().await;
3908                ctrl.send_text("here is the answer");
3909                // A complete reasoning block (has a signature, so it is buffered).
3910                ctrl.send_chunk(StreamChunk::Reasoning(
3911                    infinity_provider_protocol::message::Reasoning::new_with_signature(
3912                        "still thinking",
3913                        Some("sig".to_owned()),
3914                    ),
3915                ));
3916                tokio::task::yield_now().await;
3917                tokio::task::yield_now().await;
3918                cancel_tx.send(()).expect("send cancel signal");
3919
3920                handle.await.expect("await task handle");
3921
3922                // Committed history: user + assistant text, with the trailing
3923                // reasoning trimmed off.
3924                let history = hm.history.borrow();
3925                assert!(
3926                    !matches!(
3927                        history.last(),
3928                        Some(InfinityMessage::Assistant {
3929                            content: AssistantContent::Reasoning(_),
3930                        })
3931                    ),
3932                    "history must not end on a reasoning block, got: {:?}",
3933                    history.last()
3934                );
3935                assert!(matches!(
3936                    history.last(),
3937                    Some(InfinityMessage::Assistant {
3938                        content: AssistantContent::Text(t),
3939                    }) if t.text == "here is the answer"
3940                ));
3941            })
3942            .await;
3943    }
3944
3945    #[tokio::test(flavor = "current_thread")]
3946    async fn multiple_sync_tool_calls_chain() {
3947        let local = tokio::task::LocalSet::new();
3948        local
3949            .run_until(async {
3950                // Model calls sync tool twice in sequence (two completion rounds), then responds.
3951                let (provider, mut ctrl) = mock_provider();
3952                let convo_store = InMemoryConversationStore::new();
3953                let hm = make_history(
3954                    &convo_store,
3955                    vec![Message::User {
3956                        content: vec![UserContent::text("echo twice")],
3957                    }],
3958                )
3959                .await;
3960
3961                let mut tool_names = HashSet::new();
3962                tool_names.insert("echo_sync".to_owned());
3963                let tool_defs = vec![ToolDefinition {
3964                    name: "echo_sync".into(),
3965                    description: "echoes".into(),
3966                    parameters: serde_json::json!({"type": "object", "properties": {"text": {"type": "string"}}}),
3967                }];
3968                let mut tool_registry: HashMap<String, &dyn Tool<StubSender>> = HashMap::new();
3969                tool_registry.insert("echo_sync".into(), &ECHO_TOOL);
3970                let ctx = tool_context();
3971                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3972
3973                let handle = tokio::task::spawn_local(async move {
3974                    let thread_id = ThreadId::from("thread-1");
3975                    let stream = run_completion(
3976                        &provider,
3977                        "mock",
3978                        false,
3979                        &hm,
3980                        &tool_names,
3981                        &tool_defs,
3982                        &tool_registry,
3983                        &ctx,
3984                        &thread_id,
3985                        "msg-1",
3986                        None,
3987                        cancel_rx,
3988                    );
3989                    tokio::pin!(stream);
3990                    let mut sync_calls = 0;
3991                    let mut texts = Vec::new();
3992                    while let Some(ev) = stream.next().await {
3993                        match ev.expect("receive stream event") {
3994                            CompletionEvent::SyncToolCall { .. } => sync_calls += 1,
3995                            CompletionEvent::TextChunk(t) => texts.push(t),
3996                            _ => {}
3997                        }
3998                    }
3999                    (sync_calls, texts)
4000                });
4001
4002                // Round 1: first sync tool call
4003                let _req = ctrl.next_request().await;
4004                ctrl.send_tool_call("tc-1", "echo_sync", serde_json::json!({"text": "first"}));
4005                ctrl.finish();
4006
4007                // Round 2: second sync tool call
4008                let _req2 = ctrl.next_request().await;
4009                ctrl.send_tool_call("tc-2", "echo_sync", serde_json::json!({"text": "second"}));
4010                ctrl.finish();
4011
4012                // Round 3: final text response
4013                let _req3 = ctrl.next_request().await;
4014                ctrl.send_text("all done");
4015                ctrl.finish();
4016
4017                let (sync_calls, texts) = handle.await.expect("await task handle");
4018                assert_eq!(sync_calls, 2);
4019                assert_eq!(texts, vec!["all done"]);
4020            })
4021            .await;
4022    }
4023
4024    #[tokio::test(flavor = "current_thread")]
4025    async fn broken_stream_discards_partial_turn_before_retry() {
4026        // End-to-end guard for the original bug: the model streams visible text,
4027        // then the stream breaks. The retry must rebuild the request from clean
4028        // committed history — ending on the user message, not the partial
4029        // assistant text (which Bedrock thinking models reject as "assistant
4030        // message prefill"). Under the buffering model this holds structurally:
4031        // the partial turn was never committed, so the discard is implicit.
4032        let local = tokio::task::LocalSet::new();
4033        local
4034            .run_until(async {
4035                let (provider, mut ctrl) = mock_provider();
4036                let convo_store = InMemoryConversationStore::new();
4037                let hm = make_history(
4038                    &convo_store,
4039                    vec![Message::User {
4040                        content: vec![UserContent::text("do the thing")],
4041                    }],
4042                )
4043                .await;
4044                let (tool_names, tool_defs, tool_registry) = no_tools();
4045                let ctx = tool_context();
4046                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
4047
4048                let handle = tokio::task::spawn_local(async move {
4049                    let thread_id = ThreadId::from("thread-1");
4050                    let stream = run_completion(
4051                        &provider,
4052                        "mock",
4053                        false,
4054                        &hm,
4055                        &tool_names,
4056                        &tool_defs,
4057                        &tool_registry,
4058                        &ctx,
4059                        &thread_id,
4060                        "msg-1",
4061                        None,
4062                        cancel_rx,
4063                    );
4064                    tokio::pin!(stream);
4065                    let mut texts = Vec::new();
4066                    let mut got_done = false;
4067                    while let Some(ev) = stream.next().await {
4068                        match ev.expect("receive stream event") {
4069                            CompletionEvent::TextChunk(t) => texts.push(t),
4070                            CompletionEvent::Action(CompletionAction::Done(_)) => got_done = true,
4071                            _ => {}
4072                        }
4073                    }
4074                    (texts, got_done)
4075                });
4076
4077                // Round 1: stream text, then break the stream without a Final.
4078                let _req = ctrl.next_request().await;
4079                ctrl.send_text("I'll take a look");
4080                tokio::task::yield_now().await;
4081                tokio::task::yield_now().await;
4082                ctrl.drop_stream();
4083
4084                // Round 2: the retry request must end on the user message.
4085                let req2 = ctrl.next_request().await;
4086                let last_msg = req2
4087                    .chat_history
4088                    .into_iter()
4089                    .last()
4090                    .expect("bug: chat history is empty");
4091                assert!(
4092                    matches!(last_msg, Message::User { .. }),
4093                    "retry request must not end on an assistant message, got: {last_msg:?}"
4094                );
4095
4096                ctrl.send_text("done");
4097                ctrl.finish();
4098
4099                let (texts, got_done) = handle.await.expect("await task handle");
4100                assert!(got_done, "completion should finish after a clean retry");
4101                assert_eq!(texts, vec!["I'll take a look", "done"]);
4102            })
4103            .await;
4104    }
4105
4106    #[tokio::test(flavor = "current_thread")]
4107    async fn async_tool_call_flushes_tool_call_before_turn_ends() {
4108        // An async tool call ends the turn; its result arrives on a later,
4109        // separate turn. The assistant tool-call message (and any preceding
4110        // text) must be committed to history before the turn exits so the
4111        // caller's sync() persists it and the returning result matches.
4112        let local = tokio::task::LocalSet::new();
4113        local
4114            .run_until(async {
4115                let (provider, mut ctrl) = mock_provider();
4116                let convo_store = InMemoryConversationStore::new();
4117                let hm = std::rc::Rc::new(
4118                    make_history(
4119                        &convo_store,
4120                        vec![Message::User {
4121                            content: vec![UserContent::text("use the tool")],
4122                        }],
4123                    )
4124                    .await,
4125                );
4126
4127                struct AsyncTool;
4128                #[async_trait]
4129                impl Tool<StubSender> for AsyncTool {
4130                    fn name(&self) -> &str {
4131                        "do_async"
4132                    }
4133                    fn description(&self) -> &str {
4134                        "async"
4135                    }
4136                    fn parameters(&self) -> serde_json::Value {
4137                        serde_json::json!({"type": "object"})
4138                    }
4139                    async fn execute(
4140                        &self,
4141                        _: serde_json::Value,
4142                        _: ToolCallId,
4143                        _: Option<ProviderCallId>,
4144                        _: &ToolContext<StubSender>,
4145                    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
4146                        Ok(())
4147                    }
4148                }
4149                static ASYNC_TOOL: AsyncTool = AsyncTool;
4150
4151                let mut tool_names = HashSet::new();
4152                tool_names.insert("do_async".to_owned());
4153                let tool_defs = vec![ToolDefinition {
4154                    name: "do_async".into(),
4155                    description: "async".into(),
4156                    parameters: serde_json::json!({"type": "object"}),
4157                }];
4158                let mut tool_registry: HashMap<String, &dyn Tool<StubSender>> = HashMap::new();
4159                tool_registry.insert("do_async".into(), &ASYNC_TOOL);
4160                let ctx = tool_context();
4161                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
4162
4163                let hm_task = hm.clone();
4164                let thread_id_owned = ThreadId::from("thread-1");
4165                let handle = tokio::task::spawn_local(async move {
4166                    let stream = run_completion(
4167                        &provider,
4168                        "mock",
4169                        false,
4170                        &hm_task,
4171                        &tool_names,
4172                        &tool_defs,
4173                        &tool_registry,
4174                        &ctx,
4175                        &thread_id_owned,
4176                        "msg-1",
4177                        None,
4178                        cancel_rx,
4179                    );
4180                    tokio::pin!(stream);
4181                    let mut got_action = false;
4182                    while let Some(ev) = stream.next().await {
4183                        if let CompletionEvent::Action(CompletionAction::ExecuteToolCall {
4184                            ..
4185                        }) = ev.expect("receive stream event")
4186                        {
4187                            got_action = true;
4188                        }
4189                    }
4190                    got_action
4191                });
4192
4193                let _req = ctrl.next_request().await;
4194                ctrl.send_text("let me check");
4195                ctrl.send_tool_call("tc-1", "do_async", serde_json::json!({}));
4196                ctrl.finish();
4197
4198                let got_action = handle.await.expect("await task handle");
4199                assert!(got_action, "async tool call should yield ExecuteToolCall");
4200
4201                // The text and the tool call are committed to history (and
4202                // pending_items), so a subsequent tool-result turn will match.
4203                let history = hm.history.borrow();
4204                assert!(matches!(
4205                    history.last(),
4206                    Some(InfinityMessage::ToolCall { .. })
4207                ));
4208                assert!(
4209                    history
4210                        .iter()
4211                        .any(|m| matches!(m, InfinityMessage::Assistant { .. })),
4212                    "preceding assistant text should be committed too"
4213                );
4214                assert!(
4215                    hm.turn_buffer.borrow().is_empty(),
4216                    "buffer must be flushed when the turn ends"
4217                );
4218            })
4219            .await;
4220    }
4221
4222    #[tokio::test(flavor = "current_thread")]
4223    async fn sync_tool_loop_back_includes_flushed_tool_call() {
4224        // When a sync tool loops back within the same run_completion, the
4225        // re-sent request must include the assistant tool call and the injected
4226        // tool result — i.e. the buffer was flushed before the loop-back.
4227        let local = tokio::task::LocalSet::new();
4228        local
4229            .run_until(async {
4230                let (provider, mut ctrl) = mock_provider();
4231                let convo_store = InMemoryConversationStore::new();
4232                let hm = make_history(
4233                    &convo_store,
4234                    vec![Message::User {
4235                        content: vec![UserContent::text("echo something")],
4236                    }],
4237                )
4238                .await;
4239
4240                let mut tool_names = HashSet::new();
4241                tool_names.insert("echo_sync".to_owned());
4242                let tool_defs = vec![ToolDefinition {
4243                    name: "echo_sync".into(),
4244                    description: "echoes".into(),
4245                    parameters: serde_json::json!({"type": "object", "properties": {"text": {"type": "string"}}}),
4246                }];
4247                let mut tool_registry: HashMap<String, &dyn Tool<StubSender>> = HashMap::new();
4248                tool_registry.insert("echo_sync".into(), &ECHO_TOOL);
4249                let ctx = tool_context();
4250                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
4251
4252                let handle = tokio::task::spawn_local(async move {
4253                    let thread_id = ThreadId::from("thread-1");
4254                    let stream = run_completion(
4255                        &provider, "mock", false, &hm, &tool_names, &tool_defs, &tool_registry,
4256                        &ctx, &thread_id, "msg-1", None, cancel_rx,
4257                    );
4258                    tokio::pin!(stream);
4259                    let mut got_done = false;
4260                    while let Some(ev) = stream.next().await {
4261                        if let CompletionEvent::Action(CompletionAction::Done(_)) =
4262                            ev.expect("receive stream event")
4263                        {
4264                            got_done = true;
4265                        }
4266                    }
4267                    got_done
4268                });
4269
4270                // Round 1: sync tool call.
4271                let _req = ctrl.next_request().await;
4272                ctrl.send_tool_call("tc-1", "echo_sync", serde_json::json!({"text": "hi"}));
4273                ctrl.finish();
4274
4275                // Round 2 (loop-back): request must contain the assistant tool
4276                // call and the injected tool result.
4277                let req2 = ctrl.next_request().await;
4278                let msgs: Vec<Message> = req2.chat_history.into_iter().collect();
4279                let has_tool_call = msgs.iter().any(|m| matches!(
4280                    m,
4281                    Message::Assistant { content, .. }
4282                        if matches!(content.first(), Some(AssistantContent::ToolCall(_)))
4283                ));
4284                let has_tool_result = msgs.iter().any(|m| matches!(
4285                    m,
4286                    Message::User { content }
4287                        if matches!(content.first(), Some(UserContent::ToolResult(_)))
4288                ));
4289                assert!(has_tool_call, "loop-back must include the assistant tool call");
4290                assert!(has_tool_result, "loop-back must include the injected tool result");
4291
4292                ctrl.send_text("all done");
4293                ctrl.finish();
4294
4295                let got_done = handle.await.expect("await task handle");
4296                assert!(got_done);
4297            })
4298            .await;
4299    }
4300
4301    // ── Tool dispatch failure fallbacks (ported from the removed
4302    //    batch_processor, regression coverage for #88) ──
4303
4304    /// An `InputSender` that hands sent messages to the test.
4305    #[derive(Clone)]
4306    struct CapturingSender(tokio::sync::mpsc::UnboundedSender<InputMessage>);
4307
4308    #[async_trait]
4309    impl InputSender for CapturingSender {
4310        type Error = std::io::Error;
4311        async fn send_to_input_queue(
4312            &self,
4313            message: InputMessage,
4314            _dedup_id: &str,
4315        ) -> Result<(), std::io::Error> {
4316            self.0.send(message).expect("testing");
4317            Ok(())
4318        }
4319    }
4320
4321    #[derive(Clone)]
4322    struct StubHttp;
4323
4324    #[async_trait]
4325    impl rap_client::http::HttpClient for StubHttp {
4326        type Error = std::io::Error;
4327        async fn post(&self, _: &str, _: &str) -> Result<u16, std::io::Error> {
4328            Ok(200)
4329        }
4330        async fn get(&self, _: &str) -> Result<(u16, Vec<u8>), std::io::Error> {
4331            Ok((200, Vec::new()))
4332        }
4333    }
4334
4335    /// A tool whose dispatch always fails.
4336    struct FailingTool;
4337
4338    #[async_trait]
4339    impl Tool<CapturingSender> for FailingTool {
4340        fn name(&self) -> &str {
4341            "failing_tool"
4342        }
4343        fn description(&self) -> &str {
4344            "always fails"
4345        }
4346        fn parameters(&self) -> serde_json::Value {
4347            serde_json::json!({"type":"object","properties":{}})
4348        }
4349        async fn execute(
4350            &self,
4351            _: serde_json::Value,
4352            _: ToolCallId,
4353            _: Option<ProviderCallId>,
4354            _: &ToolContext<CapturingSender>,
4355        ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
4356            Err("stub".into())
4357        }
4358    }
4359
4360    fn capturing_ctx(
4361        group_id: &str,
4362        thread_stack: Vec<ThreadId>,
4363    ) -> (
4364        ToolContext<CapturingSender>,
4365        tokio::sync::mpsc::UnboundedReceiver<InputMessage>,
4366    ) {
4367        let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
4368        (
4369            ToolContext {
4370                message_sender: CapturingSender(tx),
4371                group_id: group_id.into(),
4372                callback_url: String::new(),
4373                user_id: None,
4374                thread_stack,
4375                safe_spawn_point: None,
4376            },
4377            rx,
4378        )
4379    }
4380
4381    /// Extract the single text tool-result the context's sender captured.
4382    fn captured_tool_result(
4383        rx: &mut tokio::sync::mpsc::UnboundedReceiver<InputMessage>,
4384    ) -> (String, Option<String>, String) {
4385        let message = rx.try_recv().expect("a tool result should be enqueued");
4386        let InputMessageContent::User(UserContent::ToolResult(result)) = message.content else {
4387            panic!("expected a tool result");
4388        };
4389        let Some(ToolResultContent::Text(text)) = result.content.first() else {
4390            panic!("expected text content");
4391        };
4392        (result.id.clone(), result.call_id.clone(), text.text.clone())
4393    }
4394
4395    /// A tool's own argument validation failure comes back to the agent as an
4396    /// error tool result (not a propagated error).
4397    #[tokio::test]
4398    async fn close_thread_missing_id_enqueues_error_result() {
4399        let (context, mut rx) = capturing_ctx("child", vec!["root".into(), "child".into()]);
4400        let tool = crate::tools::thread::CloseThreadTool::<_, StubHttp> {
4401            conversation_store: InMemoryConversationStore::new(),
4402            rap_notifier: None,
4403        };
4404
4405        tool.execute(
4406            serde_json::json!({}),
4407            "tc-close".into(),
4408            Some("call-close".into()),
4409            &context,
4410        )
4411        .await
4412        .expect("validation error should be returned as a tool result");
4413
4414        let (id, call_id, text) = captured_tool_result(&mut rx);
4415        assert_eq!(id, "tc-close");
4416        assert_eq!(call_id.as_deref(), Some("call-close"));
4417        assert_eq!(text, "Error: thread_id is required");
4418    }
4419
4420    /// A tool whose dispatch fails still gets a generic error result enqueued
4421    /// so the agent can recover, and the original error is surfaced.
4422    #[tokio::test]
4423    async fn failed_tool_execution_enqueues_fallback_result() {
4424        let (context, mut rx) = capturing_ctx("t1", vec!["t1".into()]);
4425        let tool = FailingTool;
4426        let tools: HashMap<String, &dyn Tool<CapturingSender>> =
4427            HashMap::from([("failing_tool".into(), &tool as &dyn Tool<CapturingSender>)]);
4428        let action = CompletionAction::ExecuteToolCall {
4429            tool_name: "failing_tool".into(),
4430            tool_args: serde_json::json!({}),
4431            tool_call_id: "tc-1".into(),
4432            call_id: Some("call-1".into()),
4433            display_as: None,
4434        };
4435
4436        let error = execute_action_with_error_result(action, &tools, &context)
4437            .await
4438            .expect_err("tool execution should fail");
4439        assert_eq!(error.to_string(), "stub");
4440
4441        let (id, call_id, text) = captured_tool_result(&mut rx);
4442        assert_eq!(id, "tc-1");
4443        assert_eq!(call_id.as_deref(), Some("call-1"));
4444        assert_eq!(text, "Error: Tool call failed");
4445    }
4446
4447    /// A user-choice callback is surfaced out-of-band instead of entering
4448    /// history.
4449    #[tokio::test]
4450    async fn user_choice_input_surfaces_prompt() {
4451        let store = InMemoryConversationStore::new();
4452        let hm = make_history(&store, vec![]).await;
4453
4454        let input = InputMessage {
4455            content: InputMessageContent::UserChoice(crate::message::UserChoiceRequired {
4456                content_type: "user_choice_required".to_owned(),
4457                id: "choice-1".into(),
4458                call_id: None,
4459                prompt: "pick one".to_owned(),
4460                choices: vec!["a".to_owned(), "b".to_owned()],
4461                default: 0,
4462                response_url: "https://example.com/choice".to_owned(),
4463            }),
4464            group_id: "thread-1".into(),
4465            metadata: None,
4466            synthetic: None,
4467            display_as: None,
4468            subscription: false,
4469        };
4470
4471        let result = prepare_input(input, "msg-1".to_owned(), &hm, &store, &StubSender)
4472            .await
4473            .expect("prepare input");
4474
4475        let PrepareResult::UserChoiceRequired {
4476            id,
4477            prompt,
4478            choices,
4479            default,
4480            response_url,
4481        } = result
4482        else {
4483            panic!("expected UserChoiceRequired, got {result:?}");
4484        };
4485        assert_eq!(id.as_str(), "choice-1");
4486        assert_eq!(prompt, "pick one");
4487        assert_eq!(choices, vec!["a".to_owned(), "b".to_owned()]);
4488        assert_eq!(default, 0);
4489        assert_eq!(response_url, "https://example.com/choice");
4490        assert!(hm.history.into_inner().is_empty());
4491    }
4492
4493    // ═══════════════════════════════════════════════════════════════════
4494    // Context overflow & input-persistence safety
4495    //
4496    // Inputs must not be persisted to the conversation store until the
4497    // model has produced output for them (which proves the context did not
4498    // overflow). Provider errors carry an `ErrorClass`; core reacts to the
4499    // classification instead of parsing message strings, and never applies
4500    // its own timeouts to model requests.
4501    // ═══════════════════════════════════════════════════════════════════
4502
4503    use infinity_provider_protocol::{CompletionError, ErrorClass, ModelProvider};
4504
4505    /// Feed a user text input through the same path a step uses
4506    /// (`handle_content`), as opposed to `make_history` which fabricates
4507    /// already-committed history.
4508    fn add_user_input(
4509        hm: &HistoryManager<InMemoryConversationStore, InMemoryStateStore>,
4510        text: &str,
4511        message_id: &str,
4512    ) {
4513        let accepted = hm
4514            .handle_content(
4515                InfinityMessage::User {
4516                    content: UserContent::text(text),
4517                },
4518                message_id.to_owned(),
4519            )
4520            .expect("handle user input");
4521        assert!(accepted, "input should be accepted");
4522    }
4523
4524    /// Feed a tool-result input through the same path a step uses.
4525    fn add_tool_result_input(
4526        hm: &HistoryManager<InMemoryConversationStore, InMemoryStateStore>,
4527        tool_call_id: &str,
4528        text: &str,
4529        message_id: &str,
4530    ) {
4531        let accepted = hm
4532            .handle_content(
4533                InfinityMessage::ToolResult {
4534                    result: ToolResult {
4535                        id: tool_call_id.to_owned(),
4536                        call_id: None,
4537                        content: vec![ToolResultContent::Text(
4538                            infinity_provider_protocol::message::Text {
4539                                text: text.to_owned(),
4540                            },
4541                        )],
4542                    },
4543                    display_segments: None,
4544                },
4545                message_id.to_owned(),
4546            )
4547            .expect("handle tool result input");
4548        assert!(accepted, "tool result should be accepted");
4549    }
4550
4551    /// Run one completion to termination, summarizing the yielded events as
4552    /// strings (`text:`, `info:`, `error:`, `done`).
4553    async fn collect_completion_events<P: ModelProvider>(
4554        provider: &P,
4555        hm: &HistoryManager<InMemoryConversationStore, InMemoryStateStore>,
4556        cancel_rx: tokio::sync::oneshot::Receiver<()>,
4557    ) -> Vec<String> {
4558        let (tool_names, tool_defs, tool_registry) = no_tools();
4559        let ctx = tool_context();
4560        let thread_id = ThreadId::from("thread-1");
4561        let stream = run_completion(
4562            provider,
4563            "mock",
4564            false,
4565            hm,
4566            &tool_names,
4567            &tool_defs,
4568            &tool_registry,
4569            &ctx,
4570            &thread_id,
4571            "msg-1",
4572            None,
4573            cancel_rx,
4574        );
4575        tokio::pin!(stream);
4576        let mut events = Vec::new();
4577        while let Some(ev) = stream.next().await {
4578            match ev {
4579                Ok(CompletionEvent::TextChunk(t)) => events.push(format!("text:{t}")),
4580                Ok(CompletionEvent::Info(t)) => events.push(format!("info:{t}")),
4581                Ok(CompletionEvent::Action(CompletionAction::Done(_))) => {
4582                    events.push("done".to_owned());
4583                }
4584                Ok(_) => {}
4585                Err(e) => events.push(format!("error:{e}")),
4586            }
4587        }
4588        events
4589    }
4590
4591    /// The messages persisted for `thread-1`, debug-formatted for asserts.
4592    fn persisted(store: &InMemoryConversationStore) -> String {
4593        format!(
4594            "{:?}",
4595            store
4596                .thread_messages(&ThreadId::from("thread-1"))
4597                .unwrap_or_default()
4598        )
4599    }
4600
4601    fn overflow_error() -> CompletionError {
4602        CompletionError::provider(
4603            ErrorClass::ContextOverflow,
4604            "input is too long for the model",
4605        )
4606    }
4607
4608    /// An oversized *user input* cannot be shrunk: on a context-overflow
4609    /// error it must be dropped from the in-memory history and never
4610    /// persisted, so the thread does not permanently hang on a poison
4611    /// message.
4612    #[tokio::test(flavor = "current_thread", start_paused = true)]
4613    async fn oversized_user_input_is_dropped_and_not_persisted() {
4614        let local = tokio::task::LocalSet::new();
4615        local
4616            .run_until(async {
4617                let (provider, mut ctrl) = mock_provider();
4618                let convo_store = InMemoryConversationStore::new();
4619                let hm = make_history(&convo_store, vec![]).await;
4620                add_user_input(&hm, "HUGE INPUT", "msg-huge");
4621                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
4622
4623                let handle = tokio::task::spawn_local(async move {
4624                    let events = collect_completion_events(&provider, &hm, cancel_rx).await;
4625                    (hm, events)
4626                });
4627
4628                let _req = ctrl.next_request().await;
4629                ctrl.send_error(overflow_error());
4630                ctrl.drop_stream();
4631
4632                let (hm, events) = handle.await.expect("join completion task");
4633                assert!(
4634                    events.iter().any(|e| e.starts_with("error:")),
4635                    "the overflow must surface as a terminal error, got {events:?}"
4636                );
4637                // The poison input is gone from the in-memory history...
4638                assert!(
4639                    !format!("{:?}", hm.history.borrow()).contains("HUGE INPUT"),
4640                    "oversized input must be dropped from in-memory history"
4641                );
4642                // ...and the commit that ends the step persists nothing.
4643                hm.sync().await.expect("sync");
4644                assert!(
4645                    !persisted(&convo_store).contains("HUGE INPUT"),
4646                    "oversized input must not be persisted"
4647                );
4648            })
4649            .await;
4650    }
4651
4652    /// An oversized *tool result* can be shrunk: on a context-overflow
4653    /// error it is replaced with a fixed placeholder (the tool call must
4654    /// still be answered) and the completion is retried.
4655    #[tokio::test(flavor = "current_thread", start_paused = true)]
4656    async fn oversized_tool_result_is_replaced_with_placeholder_and_retried() {
4657        let local = tokio::task::LocalSet::new();
4658        local
4659            .run_until(async {
4660                let (provider, mut ctrl) = mock_provider();
4661                let convo_store = InMemoryConversationStore::new();
4662                let hm = make_history(
4663                    &convo_store,
4664                    vec![
4665                        Message::User {
4666                            content: vec![UserContent::text("run it")],
4667                        },
4668                        tool_call_msg("tc-1", "some_tool", serde_json::json!({})),
4669                    ],
4670                )
4671                .await;
4672                add_tool_result_input(&hm, "tc-1", "HUGE RESULT", "msg-tr");
4673                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
4674
4675                let handle = tokio::task::spawn_local(async move {
4676                    let events = collect_completion_events(&provider, &hm, cancel_rx).await;
4677                    (hm, events)
4678                });
4679
4680                let _req1 = ctrl.next_request().await;
4681                ctrl.send_error(overflow_error());
4682                ctrl.drop_stream();
4683
4684                // The retry must present the placeholder instead of the
4685                // oversized result.
4686                let req2 = tokio::time::timeout(Duration::from_secs(300), ctrl.next_request())
4687                    .await
4688                    .expect("core should retry after replacing the oversized tool result");
4689                let history_debug = format!("{:?}", req2.chat_history);
4690                assert!(
4691                    !history_debug.contains("HUGE RESULT"),
4692                    "retry must not include the oversized tool result"
4693                );
4694                assert!(
4695                    history_debug.contains(TOOL_RESULT_TOO_LARGE_PLACEHOLDER),
4696                    "retry must include the placeholder tool result"
4697                );
4698                ctrl.send_text("recovered");
4699                ctrl.finish();
4700
4701                let (hm, events) = handle.await.expect("join completion task");
4702                assert!(events.contains(&"done".to_owned()), "events: {events:?}");
4703                hm.sync().await.expect("sync");
4704                let stored = persisted(&convo_store);
4705                assert!(
4706                    stored.contains(TOOL_RESULT_TOO_LARGE_PLACEHOLDER),
4707                    "the placeholder result must be persisted"
4708                );
4709                assert!(
4710                    !stored.contains("HUGE RESULT"),
4711                    "the oversized result must not be persisted"
4712                );
4713            })
4714            .await;
4715    }
4716
4717    /// An oversized *subscription event* is also replaced with the
4718    /// placeholder (its body is a tool result) rather than dropped: the
4719    /// agent should still learn that an event arrived.
4720    #[tokio::test(flavor = "current_thread", start_paused = true)]
4721    async fn oversized_subscription_event_is_replaced_with_placeholder_and_retried() {
4722        let local = tokio::task::LocalSet::new();
4723        local
4724            .run_until(async {
4725                let (provider, mut ctrl) = mock_provider();
4726                let convo_store = InMemoryConversationStore::new();
4727                let hm = make_history(
4728                    &convo_store,
4729                    vec![Message::User {
4730                        content: vec![UserContent::text("subscribe to things")],
4731                    }],
4732                )
4733                .await;
4734                let accepted = hm
4735                    .handle_content(
4736                        InfinityMessage::SubscriptionEvent {
4737                            result: Box::new(ToolResult {
4738                                id: "evt-1".to_owned(),
4739                                call_id: None,
4740                                content: vec![ToolResultContent::Text(
4741                                    infinity_provider_protocol::message::Text {
4742                                        text: "HUGE EVENT BODY".to_owned(),
4743                                    },
4744                                )],
4745                            }),
4746                            tool_call_id: "sub-1".into(),
4747                            child_thread_id: None,
4748                            invocation: Some(Box::new(ToolCall::new(
4749                                "evt-1",
4750                                "receive_event__injected",
4751                                serde_json::json!({}),
4752                            ))),
4753                        },
4754                        "msg-evt".to_owned(),
4755                    )
4756                    .expect("handle subscription event");
4757                assert!(accepted);
4758                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
4759
4760                let handle = tokio::task::spawn_local(async move {
4761                    let events = collect_completion_events(&provider, &hm, cancel_rx).await;
4762                    (hm, events)
4763                });
4764
4765                let _req1 = ctrl.next_request().await;
4766                ctrl.send_error(overflow_error());
4767                ctrl.drop_stream();
4768
4769                let req2 = tokio::time::timeout(Duration::from_secs(300), ctrl.next_request())
4770                    .await
4771                    .expect("core should retry after replacing the oversized event body");
4772                let history_debug = format!("{:?}", req2.chat_history);
4773                assert!(
4774                    !history_debug.contains("HUGE EVENT BODY"),
4775                    "retry must not include the oversized event body"
4776                );
4777                assert!(
4778                    history_debug.contains(TOOL_RESULT_TOO_LARGE_PLACEHOLDER),
4779                    "retry must include the placeholder event body"
4780                );
4781                ctrl.send_text("noted");
4782                ctrl.finish();
4783
4784                let (hm, events) = handle.await.expect("join completion task");
4785                assert!(events.contains(&"done".to_owned()), "events: {events:?}");
4786                hm.sync().await.expect("sync");
4787                let stored = persisted(&convo_store);
4788                assert!(
4789                    stored.contains(TOOL_RESULT_TOO_LARGE_PLACEHOLDER)
4790                        && !stored.contains("HUGE EVENT BODY"),
4791                    "the placeholder event must be persisted, not the oversized body; store: {stored}"
4792                );
4793            })
4794            .await;
4795    }
4796
4797    /// If the unvalidated inputs include *user text* alongside a tool
4798    /// result, an overflow must not trigger a retry: the culprit is
4799    /// ambiguous and the user's words have to be dropped, so the tool
4800    /// result is settled with the same "interrupted by user" text a user
4801    /// interruption would inject (a "too large" note could mislead the
4802    /// model into re-running the tool) and the round stops so the user can
4803    /// re-send.
4804    #[tokio::test(flavor = "current_thread", start_paused = true)]
4805    async fn overflow_with_pending_tool_result_and_user_input_does_not_retry() {
4806        let local = tokio::task::LocalSet::new();
4807        local
4808            .run_until(async {
4809                let (provider, mut ctrl) = mock_provider();
4810                let convo_store = InMemoryConversationStore::new();
4811                let hm = make_history(
4812                    &convo_store,
4813                    vec![
4814                        Message::User {
4815                            content: vec![UserContent::text("run it")],
4816                        },
4817                        tool_call_msg("tc-1", "some_tool", serde_json::json!({})),
4818                    ],
4819                )
4820                .await;
4821                // The tool result arrives first, then a huge user input
4822                // interrupts before the model produced any output.
4823                add_tool_result_input(&hm, "tc-1", "normal tool result", "msg-tr");
4824                add_user_input(&hm, "HUGE USER INPUT", "msg-huge");
4825                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
4826
4827                let handle = tokio::task::spawn_local(async move {
4828                    let events = collect_completion_events(&provider, &hm, cancel_rx).await;
4829                    (hm, events)
4830                });
4831
4832                let _req1 = ctrl.next_request().await;
4833                ctrl.send_error(overflow_error());
4834                ctrl.drop_stream();
4835
4836                // Time-boxed so a wrong retry attempt fails fast instead of
4837                // hanging the test (the consumer would wait forever on the
4838                // retry round's mock stream).
4839                let (hm, events) = tokio::time::timeout(Duration::from_secs(600), handle)
4840                    .await
4841                    .expect("the round must stop, not retry")
4842                    .expect("join completion task");
4843                assert!(
4844                    events.iter().any(|e| e.starts_with("error:")),
4845                    "the overflow must surface as a terminal error, got {events:?}"
4846                );
4847                assert!(
4848                    ctrl.try_next_request().is_none(),
4849                    "dropping user input must stop the round, not retry it"
4850                );
4851
4852                let history = format!("{:?}", hm.history.borrow());
4853                // The user text was dropped...
4854                assert!(
4855                    !history.contains("HUGE USER INPUT"),
4856                    "oversized user input must be dropped; history: {history}"
4857                );
4858                // ...and the tool call stays answered — settled as
4859                // interrupted, not "too large" (the culprit is ambiguous).
4860                assert!(
4861                    history.contains(TOOL_CALL_INTERRUPTED_TEXT),
4862                    "the tool result must be settled as interrupted; history: {history}"
4863                );
4864                assert!(
4865                    !history.contains(TOOL_RESULT_TOO_LARGE_PLACEHOLDER),
4866                    "ambiguous overflows must not blame the tool result; history: {history}"
4867                );
4868                assert!(!history.contains("normal tool result"));
4869
4870                // Nothing from this round persists (the settled result is
4871                // still unvalidated).
4872                hm.sync().await.expect("sync");
4873                let stored = persisted(&convo_store);
4874                assert!(
4875                    !stored.contains("HUGE USER INPUT")
4876                        && !stored.contains("normal tool result")
4877                        && !stored.contains(TOOL_CALL_INTERRUPTED_TEXT),
4878                    "nothing unvalidated may persist; store: {stored}"
4879                );
4880            })
4881            .await;
4882    }
4883
4884    /// If the *placeholder* tool result still overflows, give up: surface a
4885    /// terminal error and persist nothing. Only user input is ever dropped,
4886    /// so the tool call stays answered in memory — re-settled as
4887    /// "interrupted by user", since the round is over and the next thing
4888    /// the model sees will be a fresh user message (a "too large" note
4889    /// could mislead it into re-running the tool). After a process restart
4890    /// the store ends on the unanswered call and the next input settles it
4891    /// the same way (see
4892    /// `rebooted_session_settles_persisted_unanswered_tool_call`).
4893    #[tokio::test(flavor = "current_thread", start_paused = true)]
4894    async fn second_overflow_after_placeholder_gives_up_without_persisting_result() {
4895        let local = tokio::task::LocalSet::new();
4896        local
4897            .run_until(async {
4898                let (provider, mut ctrl) = mock_provider();
4899                let convo_store = InMemoryConversationStore::new();
4900                let hm = make_history(
4901                    &convo_store,
4902                    vec![
4903                        Message::User {
4904                            content: vec![UserContent::text("run it")],
4905                        },
4906                        tool_call_msg("tc-1", "some_tool", serde_json::json!({})),
4907                    ],
4908                )
4909                .await;
4910                add_tool_result_input(&hm, "tc-1", "HUGE RESULT", "msg-tr");
4911                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
4912
4913                let handle = tokio::task::spawn_local(async move {
4914                    let events = collect_completion_events(&provider, &hm, cancel_rx).await;
4915                    (hm, events)
4916                });
4917
4918                let _req1 = ctrl.next_request().await;
4919                ctrl.send_error(overflow_error());
4920                ctrl.drop_stream();
4921                let _req2 = tokio::time::timeout(Duration::from_secs(300), ctrl.next_request())
4922                    .await
4923                    .expect("core should retry once with the placeholder result");
4924                ctrl.send_error(overflow_error());
4925                ctrl.drop_stream();
4926
4927                let (hm, events) = handle.await.expect("join completion task");
4928                assert!(
4929                    events.iter().any(|e| e.starts_with("error:")),
4930                    "the second overflow must surface as a terminal error, got {events:?}"
4931                );
4932                assert!(
4933                    ctrl.try_next_request().is_none(),
4934                    "the placeholder round must not be retried again"
4935                );
4936                // The tool call stays answered in memory — re-settled as
4937                // "interrupted" now that the round is abandoned — but
4938                // nothing about the result persists.
4939                let last = format!("{:?}", hm.history.borrow().last());
4940                assert!(
4941                    last.contains("tc-1") && last.contains(TOOL_CALL_INTERRUPTED_TEXT),
4942                    "the call must stay answered as interrupted, got {last}"
4943                );
4944                assert!(
4945                    !last.contains(TOOL_RESULT_TOO_LARGE_PLACEHOLDER),
4946                    "a 'too large' note would mislead the next round, got {last}"
4947                );
4948                hm.sync().await.expect("sync");
4949                let stored = persisted(&convo_store);
4950                assert!(!stored.contains("HUGE RESULT"), "stored: {stored}");
4951                assert!(
4952                    !stored.contains(TOOL_RESULT_TOO_LARGE_PLACEHOLDER)
4953                        && !stored.contains(TOOL_CALL_INTERRUPTED_TEXT),
4954                    "stored: {stored}"
4955                );
4956
4957                // A later user input does not need to interrupt anything —
4958                // the call is already answered in memory.
4959                add_user_input(&hm, "hello again", "msg-next");
4960                let history = format!("{:?}", hm.history.borrow());
4961                assert!(history.contains(TOOL_CALL_INTERRUPTED_TEXT));
4962                assert!(history.contains("hello again"));
4963            })
4964            .await;
4965    }
4966
4967    /// Interrupting a completion *before the model produced any output*
4968    /// must not persist the batch's inputs — the model never validated them
4969    /// (they could be the poison input) — but they stay in memory so the
4970    /// next round still sends them.
4971    #[tokio::test(flavor = "current_thread")]
4972    async fn interrupt_before_model_output_keeps_input_in_memory_unpersisted() {
4973        let local = tokio::task::LocalSet::new();
4974        local
4975            .run_until(async {
4976                let (provider, mut ctrl) = mock_provider();
4977                let convo_store = InMemoryConversationStore::new();
4978                let hm = make_history(&convo_store, vec![]).await;
4979                add_user_input(&hm, "hello there", "msg-1");
4980                let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
4981
4982                let handle = tokio::task::spawn_local(async move {
4983                    let events = collect_completion_events(&provider, &hm, cancel_rx).await;
4984                    (hm, events)
4985                });
4986
4987                // Request is in flight; no output yet. Interrupt.
4988                let _req = ctrl.next_request().await;
4989                cancel_tx.send(()).expect("send cancel");
4990
4991                let (hm, _events) = handle.await.expect("join completion task");
4992                // The input survives in memory (the next step resends it)...
4993                assert!(
4994                    format!("{:?}", hm.history.borrow()).contains("hello there"),
4995                    "interrupted input must stay in the in-memory history"
4996                );
4997                // ...but must not be persisted: the model never accepted it.
4998                hm.sync().await.expect("sync");
4999                assert!(
5000                    !persisted(&convo_store).contains("hello there"),
5001                    "input must not be persisted before the model produced output"
5002                );
5003            })
5004            .await;
5005    }
5006
5007    /// Interrupting *after* the model produced output persists both the
5008    /// input and the partial output: streamed output proves the context did
5009    /// not overflow.
5010    #[tokio::test(flavor = "current_thread")]
5011    async fn interrupt_after_model_output_persists_input_and_partial_text() {
5012        let local = tokio::task::LocalSet::new();
5013        local
5014            .run_until(async {
5015                let (provider, mut ctrl) = mock_provider();
5016                let convo_store = InMemoryConversationStore::new();
5017                let hm = make_history(&convo_store, vec![]).await;
5018                add_user_input(&hm, "hello there", "msg-1");
5019                let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
5020
5021                let handle = tokio::task::spawn_local(async move {
5022                    let events = collect_completion_events(&provider, &hm, cancel_rx).await;
5023                    (hm, events)
5024                });
5025
5026                let _req = ctrl.next_request().await;
5027                ctrl.send_text("partial answer");
5028                tokio::task::yield_now().await;
5029                tokio::task::yield_now().await;
5030                cancel_tx.send(()).expect("send cancel");
5031
5032                let (hm, events) = handle.await.expect("join completion task");
5033                assert!(events.contains(&"text:partial answer".to_owned()));
5034                hm.sync().await.expect("sync");
5035                let stored = persisted(&convo_store);
5036                assert!(
5037                    stored.contains("hello there"),
5038                    "model output validated the input: it must persist"
5039                );
5040                assert!(
5041                    stored.contains("partial answer"),
5042                    "the partial output must persist"
5043                );
5044            })
5045            .await;
5046    }
5047
5048    /// A throttled error retries after a longer backoff — driven by the
5049    /// provider's classification, not by message-string matching.
5050    #[tokio::test(flavor = "current_thread", start_paused = true)]
5051    async fn throttled_error_waits_and_retries() {
5052        let local = tokio::task::LocalSet::new();
5053        local
5054            .run_until(async {
5055                let (provider, mut ctrl) = mock_provider();
5056                let convo_store = InMemoryConversationStore::new();
5057                let hm = make_history(&convo_store, vec![]).await;
5058                add_user_input(&hm, "hi", "msg-1");
5059                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
5060
5061                let handle = tokio::task::spawn_local(async move {
5062                    collect_completion_events(&provider, &hm, cancel_rx).await
5063                });
5064
5065                let started = tokio::time::Instant::now();
5066                let _req1 = ctrl.next_request().await;
5067                ctrl.send_error(CompletionError::provider(
5068                    ErrorClass::Throttled,
5069                    "simulated throttle",
5070                ));
5071                ctrl.drop_stream();
5072
5073                let _req2 = tokio::time::timeout(Duration::from_secs(600), ctrl.next_request())
5074                    .await
5075                    .expect("core should retry after a throttled error");
5076                assert!(
5077                    started.elapsed() >= Duration::from_secs(10),
5078                    "throttled retries should back off"
5079                );
5080                ctrl.send_text("ok");
5081                ctrl.finish();
5082
5083                let events = handle.await.expect("join completion task");
5084                assert!(events.contains(&"done".to_owned()), "events: {events:?}");
5085            })
5086            .await;
5087    }
5088
5089    /// A transient error retries quickly — again from the classification,
5090    /// regardless of the message text.
5091    #[tokio::test(flavor = "current_thread", start_paused = true)]
5092    async fn transient_error_discards_turn_and_retries() {
5093        let local = tokio::task::LocalSet::new();
5094        local
5095            .run_until(async {
5096                let (provider, mut ctrl) = mock_provider();
5097                let convo_store = InMemoryConversationStore::new();
5098                let hm = make_history(&convo_store, vec![]).await;
5099                add_user_input(&hm, "hi", "msg-1");
5100                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
5101
5102                let handle = tokio::task::spawn_local(async move {
5103                    collect_completion_events(&provider, &hm, cancel_rx).await
5104                });
5105
5106                let _req1 = ctrl.next_request().await;
5107                ctrl.send_error(CompletionError::provider(
5108                    ErrorClass::Transient,
5109                    "connection reset by peer",
5110                ));
5111                ctrl.drop_stream();
5112
5113                let _req2 = tokio::time::timeout(Duration::from_secs(600), ctrl.next_request())
5114                    .await
5115                    .expect("core should retry after a transient error");
5116                ctrl.send_text("ok");
5117                ctrl.finish();
5118
5119                let events = handle.await.expect("join completion task");
5120                assert!(events.contains(&"done".to_owned()), "events: {events:?}");
5121            })
5122            .await;
5123    }
5124
5125    /// A fatal error terminates immediately: no retry request is made.
5126    #[tokio::test(flavor = "current_thread", start_paused = true)]
5127    async fn fatal_error_gives_up_immediately() {
5128        let local = tokio::task::LocalSet::new();
5129        local
5130            .run_until(async {
5131                let (provider, mut ctrl) = mock_provider();
5132                let convo_store = InMemoryConversationStore::new();
5133                let hm = make_history(&convo_store, vec![]).await;
5134                add_user_input(&hm, "hi", "msg-1");
5135                let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
5136
5137                let handle = tokio::task::spawn_local(async move {
5138                    collect_completion_events(&provider, &hm, cancel_rx).await
5139                });
5140
5141                let _req1 = ctrl.next_request().await;
5142                ctrl.send_error(CompletionError::provider(
5143                    ErrorClass::Fatal,
5144                    "the model does not exist",
5145                ));
5146                ctrl.drop_stream();
5147
5148                let events = handle.await.expect("join completion task");
5149                assert!(
5150                    events.iter().any(|e| e.starts_with("error:")),
5151                    "fatal errors must surface, got {events:?}"
5152                );
5153                assert!(
5154                    ctrl.try_next_request().is_none(),
5155                    "fatal errors must not be retried"
5156                );
5157            })
5158            .await;
5159    }
5160
5161    // ── HistoryManager three-phase pending safeguards ──
5162
5163    /// Inputs folded into history are not persisted by `sync()` until the
5164    /// model validates them.
5165    #[tokio::test]
5166    async fn sync_does_not_persist_inputs_before_model_output() {
5167        let store = InMemoryConversationStore::new();
5168        let hm = make_history(&store, vec![]).await;
5169        add_user_input(&hm, "not yet validated", "msg-1");
5170        hm.sync().await.expect("sync");
5171        assert!(
5172            !persisted(&store).contains("not yet validated"),
5173            "unvalidated inputs must not be persisted by sync()"
5174        );
5175    }
5176
5177    /// Sequentiality safeguard: committing model output while unvalidated
5178    /// inputs exist would let `sync()` persist the output *without* the
5179    /// input it answers. That is a bug and must panic.
5180    #[tokio::test]
5181    #[should_panic(expected = "unvalidated")]
5182    async fn flushing_model_output_with_unvalidated_inputs_panics() {
5183        let store = InMemoryConversationStore::new();
5184        let hm = make_history(&store, vec![]).await;
5185        add_user_input(&hm, "pending input", "msg-1");
5186        hm.handle_completion(
5187            &StreamChunk::Text("model output".to_owned()),
5188            "completion-1".to_owned(),
5189            None,
5190        );
5191        // Flushing without validating the pending input first violates
5192        // sequentiality.
5193        hm.flush_turn();
5194    }
5195
5196    /// Child threads inherit history *from the store*, so a spawn point
5197    /// must not extend past inputs that have not been persisted yet.
5198    #[tokio::test]
5199    async fn safe_spawn_point_excludes_unvalidated_inputs() {
5200        let store = InMemoryConversationStore::new();
5201        let hm = make_history(&store, vec![]).await;
5202        add_user_input(&hm, "not persisted yet", "msg-1");
5203        assert_eq!(
5204            hm.safe_spawn_point(),
5205            0,
5206            "spawn point must not cover unvalidated (unpersisted) inputs"
5207        );
5208    }
5209}