Skip to main content

zeph_context/
summarization.rs

1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Pure prompt-building, compaction helpers, and async LLM summarization for context.
5//!
6//! Stateless functions take only `Message` slices and configuration values; they contain
7//! no agent state access. The `SummarizationDeps` struct provides explicit LLM dependencies
8//! for the async summarization functions, avoiding coupling to `Agent<C>`.
9//!
10//! The orchestration layer (`Agent::compact_context`, `Agent::maybe_compact`, etc.)
11//! lives in `zeph-core` and calls these helpers.
12
13use std::fmt::Write as _;
14use std::sync::Arc;
15use std::time::Duration;
16
17use futures::StreamExt as _;
18use tracing::Instrument as _;
19use zeph_common::OVERFLOW_NOTICE_PREFIX;
20use zeph_common::memory::AnchoredSummary;
21use zeph_llm::LlmProvider as _;
22use zeph_llm::any::AnyProvider;
23use zeph_llm::provider::{Message, MessageMetadata, MessagePart, Role};
24
25/// Token counting for individual messages, used by the summarization chunker.
26///
27/// Implemented by `zeph-memory::TokenCounter` in `zeph-core`. Defined here so
28/// `zeph-context` does not need a direct dependency on `zeph-memory`.
29pub trait MessageTokenCounter: Send + Sync {
30    /// Return the token count for the given message, accounting for all parts.
31    fn count_message_tokens(&self, msg: &Message) -> usize;
32}
33
34/// Explicit LLM dependencies for async summarization, avoiding coupling to `Agent<C>`.
35///
36/// Passed to [`single_pass_summary`], [`summarize_with_llm`], and [`summarize_structured`]
37/// so these functions can be called from `zeph-context` without depending on `zeph-core`.
38#[derive(Clone)]
39pub struct SummarizationDeps {
40    /// LLM provider used for all summarization calls.
41    pub provider: AnyProvider,
42    /// Timeout applied to each individual LLM call.
43    pub llm_timeout: Duration,
44    /// Token counter for chunking message slices.
45    pub token_counter: Arc<dyn MessageTokenCounter>,
46    /// Whether to attempt structured `AnchoredSummary` output before prose.
47    pub structured_summaries: bool,
48    /// Optional callback invoked with the `AnchoredSummary` result and a `fallback` flag.
49    ///
50    /// Used by `zeph-core` to write debug dumps without the `SummarizationDeps` knowing
51    /// about `DebugDumper`. Pass `None` when debug dumps are not needed.
52    #[allow(clippy::type_complexity)]
53    pub on_anchored_summary: Option<Arc<dyn Fn(&AnchoredSummary, bool) + Send + Sync>>,
54}
55
56/// Attempt structured summarization via `chat_typed_erased::<AnchoredSummary>()`.
57///
58/// Returns `Ok(AnchoredSummary)` on success, `Err` when mandatory fields are missing
59/// or the LLM fails. The caller is responsible for falling back to prose on `Err`.
60///
61/// # Errors
62/// Returns [`zeph_llm::LlmError`] when the LLM call fails, times out, or the
63/// returned summary is incomplete.
64pub async fn summarize_structured(
65    deps: &SummarizationDeps,
66    messages: &[Message],
67    guidelines: &str,
68) -> Result<AnchoredSummary, zeph_llm::LlmError> {
69    async move {
70        let prompt = build_anchored_summary_prompt(messages, guidelines);
71        let msgs = [Message {
72            role: Role::User,
73            content: prompt,
74            parts: vec![],
75            metadata: MessageMetadata::default(),
76        }];
77        let summary: AnchoredSummary = tokio::time::timeout(
78            deps.llm_timeout,
79            deps.provider.chat_typed_erased::<AnchoredSummary>(&msgs),
80        )
81        .await
82        .map_err(|_| zeph_llm::LlmError::Timeout)??;
83
84        if !summary.files_modified.is_empty() && summary.decisions_made.is_empty() {
85            tracing::warn!("structured summary: decisions_made is empty");
86        } else if summary.files_modified.is_empty() {
87            tracing::warn!(
88                "structured summary: files_modified is empty (may be a pure discussion session)"
89            );
90        }
91
92        if !summary.is_complete() {
93            tracing::warn!(
94                session_intent_empty = summary.session_intent.trim().is_empty(),
95                next_steps_empty = summary.next_steps.is_empty(),
96                "structured summary incomplete: mandatory fields missing, falling back to prose"
97            );
98            return Err(zeph_llm::LlmError::StructuredParse(
99                "structured summary missing mandatory fields".into(),
100            ));
101        }
102
103        if let Err(msg) = summary.validate() {
104            tracing::warn!(
105                error = %msg,
106                "structured summary failed field validation, falling back to prose"
107            );
108            return Err(zeph_llm::LlmError::StructuredParse(msg));
109        }
110
111        Ok(summary)
112    }
113    .instrument(tracing::info_span!(
114        "context.summarization.structured",
115        message_count = messages.len(),
116    ))
117    .await
118}
119
120/// Single-pass LLM summarization over a message slice.
121///
122/// # Errors
123/// Returns [`zeph_llm::LlmError`] when the LLM call fails or times out.
124pub async fn single_pass_summary(
125    deps: &SummarizationDeps,
126    messages: &[Message],
127    guidelines: &str,
128) -> Result<String, zeph_llm::LlmError> {
129    async move {
130        let prompt = build_chunk_prompt(messages, guidelines);
131        let msgs = [Message {
132            role: Role::User,
133            content: prompt,
134            parts: vec![],
135            metadata: MessageMetadata::default(),
136        }];
137        tokio::time::timeout(deps.llm_timeout, deps.provider.chat(&msgs))
138            .await
139            .map_err(|_| zeph_llm::LlmError::Timeout)?
140    }
141    .instrument(tracing::info_span!(
142        "context.summarization.single_pass",
143        message_count = messages.len(),
144    ))
145    .await
146}
147
148/// Chunked multi-pass LLM summarization with bounded concurrency.
149///
150/// Splits `messages` into token-bounded chunks and summarizes each chunk with the LLM.
151/// Partial results are consolidated into a final summary. Falls back to single-pass on
152/// chunk failures or context length errors.
153///
154/// # Errors
155/// Returns [`zeph_llm::LlmError`] when all summarization attempts fail.
156pub async fn summarize_with_llm(
157    deps: &SummarizationDeps,
158    messages: &[Message],
159    guidelines: &str,
160) -> Result<String, zeph_llm::LlmError> {
161    async move {
162        const CHUNK_TOKEN_BUDGET: usize = 4096;
163        const OVERSIZED_THRESHOLD: usize = CHUNK_TOKEN_BUDGET / 2;
164
165        let tc = Arc::clone(&deps.token_counter);
166        let chunks = crate::slot::chunk_messages(
167            messages,
168            CHUNK_TOKEN_BUDGET,
169            OVERSIZED_THRESHOLD,
170            move |msg| tc.count_message_tokens(msg),
171        );
172
173        if chunks.len() <= 1 {
174            return single_pass_summary(deps, messages, guidelines).await;
175        }
176
177        let partial_summaries = run_chunk_summaries(deps, chunks, guidelines).await;
178
179        if partial_summaries.is_empty() {
180            return single_pass_summary(deps, messages, guidelines).await;
181        }
182
183        let numbered = join_partial_summaries(&partial_summaries);
184
185        if deps.structured_summaries
186            && let Some(result) = try_structured_consolidation(deps, &numbered).await
187        {
188            return Ok(result);
189        }
190
191        prose_consolidation(deps, &numbered).await
192    }
193    .instrument(tracing::info_span!(
194        "context.summarization.with_llm",
195        message_count = messages.len(),
196    ))
197    .await
198}
199
200/// Summarizes each chunk concurrently with bounded parallelism. Any chunk error discards all
201/// partials and forces single-pass fallback.
202async fn run_chunk_summaries(
203    deps: &SummarizationDeps,
204    chunks: Vec<Vec<Message>>,
205    guidelines: &str,
206) -> Vec<String> {
207    let chunk_count = chunks.len();
208    async move {
209        let provider = deps.provider.clone();
210        let guidelines_arc: Arc<str> = Arc::from(guidelines);
211        let timeout = deps.llm_timeout;
212
213        let results: Vec<_> = futures::stream::iter(chunks.into_iter().map(|chunk| {
214            let guidelines_ref = Arc::clone(&guidelines_arc);
215            let prompt = build_chunk_prompt(&chunk, &guidelines_ref);
216            let p = provider.clone();
217            async move {
218                tokio::time::timeout(
219                    timeout,
220                    p.chat(&[Message {
221                        role: Role::User,
222                        content: prompt,
223                        parts: vec![],
224                        metadata: MessageMetadata::default(),
225                    }]),
226                )
227                .await
228                .map_err(|_| zeph_llm::LlmError::Timeout)?
229            }
230        }))
231        .buffer_unordered(4)
232        .collect()
233        .await;
234
235        results
236            .into_iter()
237            .collect::<Result<Vec<_>, zeph_llm::LlmError>>()
238            .unwrap_or_else(|e| {
239                tracing::warn!(
240                    "chunked compaction: one or more chunks failed: {e:#}, falling back to single-pass"
241                );
242                Vec::new()
243            })
244    }
245    .instrument(tracing::info_span!(
246        "context.summarization.chunk_summaries",
247        chunk_count,
248    ))
249    .await
250}
251
252fn join_partial_summaries(partials: &[String]) -> String {
253    let cap: usize = partials.iter().map(|s| s.len() + 8).sum();
254    let mut buf = String::with_capacity(cap);
255    for (i, s) in partials.iter().enumerate() {
256        if i > 0 {
257            buf.push_str("\n\n");
258        }
259        let _ = write!(buf, "{}. {s}", i + 1);
260    }
261    buf
262}
263
264async fn try_structured_consolidation(deps: &SummarizationDeps, numbered: &str) -> Option<String> {
265    async move {
266        let timeout = deps.llm_timeout;
267        let anchored_prompt = format!(
268            "<analysis>\n\
269             Merge these partial conversation summaries into a single structured summary.\n\
270             </analysis>\n\
271             \n\
272             Produce a JSON object with exactly these 5 fields:\n\
273             - session_intent: string — what the user is trying to accomplish\n\
274             - files_modified: string[] — file paths, function names, structs touched\n\
275             - decisions_made: string[] — each entry: \"Decision: X — Reason: Y\"\n\
276             - open_questions: string[] — unresolved questions or blockers\n\
277             - next_steps: string[] — concrete next actions\n\
278             \n\
279             Partial summaries:\n{numbered}"
280        );
281        let anchored_msgs = [Message {
282            role: Role::User,
283            content: anchored_prompt,
284            parts: vec![],
285            metadata: MessageMetadata::default(),
286        }];
287        match tokio::time::timeout(
288            timeout,
289            deps.provider
290                .chat_typed_erased::<AnchoredSummary>(&anchored_msgs),
291        )
292        .await
293        {
294            Ok(Ok(anchored)) if anchored.is_complete() => {
295                if let Some(ref cb) = deps.on_anchored_summary {
296                    cb(&anchored, false);
297                }
298                Some(crate::slot::cap_summary(anchored.to_markdown(), 16_000))
299            }
300            Ok(Ok(anchored)) => {
301                tracing::warn!(
302                    "chunked consolidation: structured summary incomplete, falling back to prose"
303                );
304                if let Some(ref cb) = deps.on_anchored_summary {
305                    cb(&anchored, true);
306                }
307                None
308            }
309            Ok(Err(e)) => {
310                tracing::warn!(error = %e, "chunked consolidation: structured output failed, falling back to prose");
311                None
312            }
313            Err(_) => {
314                tracing::warn!(
315                    "chunked consolidation: structured output timed out, falling back to prose"
316                );
317                None
318            }
319        }
320    }
321    .instrument(tracing::info_span!("context.summarization.structured_consolidation"))
322    .await
323}
324
325async fn prose_consolidation(
326    deps: &SummarizationDeps,
327    numbered: &str,
328) -> Result<String, zeph_llm::LlmError> {
329    async move {
330        let timeout = deps.llm_timeout;
331        let consolidation_prompt = format!(
332            "<analysis>\n\
333             Merge these partial conversation summaries into a single structured compaction note.\n\
334             Produce exactly these 9 sections covering all partial summaries:\n\
335             1. User Intent\n2. Technical Concepts\n3. Files & Code\n4. Errors & Fixes\n\
336             5. Problem Solving\n6. User Messages\n7. Pending Tasks\n8. Current Work\n9. Next Step\n\
337             </analysis>\n\n\
338             Partial summaries:\n{numbered}"
339        );
340        let consolidation_msgs = [Message {
341            role: Role::User,
342            content: consolidation_prompt,
343            parts: vec![],
344            metadata: MessageMetadata::default(),
345        }];
346        tokio::time::timeout(timeout, deps.provider.chat(&consolidation_msgs))
347            .await
348            .map_err(|_| zeph_llm::LlmError::Timeout)?
349    }
350    .instrument(tracing::info_span!("context.summarization.prose_consolidation"))
351    .await
352}
353
354/// Build a prose summarization prompt from a message slice and optional guidelines.
355///
356/// The returned string is suitable for sending to an LLM as a `User` message.
357/// Guidelines are injected inside `<compression-guidelines>` XML tags when non-empty.
358///
359/// # Examples
360///
361/// ```no_run
362/// use zeph_context::summarization::build_chunk_prompt;
363/// let prompt = build_chunk_prompt(&[], "be concise");
364/// assert!(prompt.contains("compression-guidelines"));
365/// ```
366#[must_use]
367pub fn build_chunk_prompt(messages: &[Message], guidelines: &str) -> String {
368    let history_text = format_history(messages);
369
370    let guidelines_section = guidelines_xml(guidelines);
371
372    format!(
373        "<analysis>\n\
374         Analyze this conversation and produce a structured compaction note for self-consumption.\n\
375         This note replaces the original messages in your context window — be thorough.\n\
376         Longer is better if it preserves actionable detail.\n\
377         </analysis>\n\
378         {guidelines_section}\n\
379         Produce exactly these 9 sections:\n\
380         1. User Intent — what the user is ultimately trying to accomplish\n\
381         2. Technical Concepts — key technologies, patterns, constraints discussed\n\
382         3. Files & Code — file paths, function names, structs, enums touched or relevant\n\
383         4. Errors & Fixes — every error encountered and whether/how it was resolved\n\
384         5. Problem Solving — approaches tried, decisions made, alternatives rejected\n\
385         6. User Messages — verbatim user requests that are still pending or relevant\n\
386         7. Pending Tasks — items explicitly promised or left TODO\n\
387         8. Current Work — the exact task in progress at the moment of compaction\n\
388         9. Next Step — the single most important action to take immediately after compaction\n\
389         \n\
390         Conversation:\n{history_text}"
391    )
392}
393
394/// Build a structured JSON summarization prompt for `AnchoredSummary` output.
395///
396/// The returned string is suitable for sending to an LLM as a `User` message.
397/// Guidelines are injected inside `<compression-guidelines>` XML tags when non-empty.
398///
399/// # Examples
400///
401/// ```no_run
402/// use zeph_context::summarization::build_anchored_summary_prompt;
403/// let prompt = build_anchored_summary_prompt(&[], "");
404/// assert!(prompt.contains("session_intent"));
405/// ```
406#[must_use]
407pub fn build_anchored_summary_prompt(messages: &[Message], guidelines: &str) -> String {
408    let history_text = format_history(messages);
409    let guidelines_section = guidelines_xml(guidelines);
410
411    format!(
412        "<analysis>\n\
413         You are compacting a conversation into a structured summary for self-consumption.\n\
414         This summary replaces the original messages in your context window.\n\
415         Every field MUST be populated — empty fields mean lost information.\n\
416         </analysis>\n\
417         {guidelines_section}\n\
418         Produce a JSON object with exactly these 5 fields:\n\
419         - session_intent: string — what the user is trying to accomplish\n\
420         - files_modified: string[] — file paths, function names, structs touched\n\
421         - decisions_made: string[] — each entry: \"Decision: X — Reason: Y\"\n\
422         - open_questions: string[] — unresolved questions or blockers\n\
423         - next_steps: string[] — concrete next actions\n\
424         \n\
425         Be thorough. Preserve all file paths, line numbers, error messages, \
426         and specific identifiers — they cannot be recovered.\n\
427         \n\
428         Conversation:\n{history_text}"
429    )
430}
431
432/// Build a last-resort metadata summary without calling the LLM.
433///
434/// Used when LLM summarization repeatedly fails. The result records message counts
435/// and truncated previews of the last user and assistant messages.
436#[must_use]
437pub fn build_metadata_summary(messages: &[Message], truncate: fn(&str, usize) -> String) -> String {
438    let mut user_count = 0usize;
439    let mut assistant_count = 0usize;
440    let mut system_count = 0usize;
441    let mut last_user = String::new();
442    let mut last_assistant = String::new();
443
444    for m in messages {
445        match m.role {
446            Role::User => {
447                user_count += 1;
448                if !m.content.is_empty() {
449                    last_user.clone_from(&m.content);
450                }
451            }
452            Role::Assistant => {
453                assistant_count += 1;
454                if !m.content.is_empty() {
455                    last_assistant.clone_from(&m.content);
456                }
457            }
458            Role::System => system_count += 1,
459        }
460    }
461
462    let last_user_preview = truncate(&last_user, 200);
463    let last_assistant_preview = truncate(&last_assistant, 200);
464
465    format!(
466        "[metadata summary — LLM compaction unavailable]\n\
467         Messages compacted: {} ({} user, {} assistant, {} system)\n\
468         Last user message: {last_user_preview}\n\
469         Last assistant message: {last_assistant_preview}",
470        messages.len(),
471        user_count,
472        assistant_count,
473        system_count,
474    )
475}
476
477/// Build a summarization prompt for a single tool-call pair.
478///
479/// The returned string is suitable for sending to an LLM as a `User` message.
480#[must_use]
481pub fn build_tool_pair_summary_prompt(req: &Message, res: &Message) -> String {
482    format!(
483        "Produce a concise but technically precise summary of this tool invocation.\n\
484         Preserve all facts that would be needed to continue work without re-running the tool:\n\
485         - Tool name and key input parameters (file paths, function names, patterns, line ranges)\n\
486         - Exact findings: line numbers, struct/enum/function names, error messages, numeric values\n\
487         - Outcome: what was found, changed, created, or confirmed\n\
488         Do NOT omit specific identifiers, paths, or numbers — they cannot be recovered later.\n\
489         Use 2-4 sentences maximum.\n\n\
490         <tool_request>\n{}\n</tool_request>\n\n<tool_response>\n{}\n</tool_response>",
491        req.content, res.content
492    )
493}
494
495/// Remove a fraction of tool-response messages from a conversation using a middle-out strategy.
496///
497/// This function compacts verbose tool outputs by selectively replacing them with placeholders,
498/// preserving critical initial and final exchanges while collapsing middle responses. The
499/// middle-out strategy ensures the model still sees the most recent (and thus most relevant)
500/// tool results, reducing token usage without destroying conversation flow.
501///
502/// # Parameters
503///
504/// * `messages` — the full message history
505/// * `fraction` — the fraction of tool responses to replace (range `(0.0, 1.0]`). For example,
506///   `0.5` means half of the tool responses will be compacted.
507///
508/// # Behavior
509///
510/// - Identifies all messages containing `ToolResult` or `ToolOutput` parts
511/// - Selects which ones to compact using middle-out heuristic (starting near the center,
512///   spiraling outward)
513/// - Tool outputs with an overflow UUID are replaced with a `read_overflow` hint; others become `[compacted]`
514/// - Returns the modified message list with compacted responses in place
515///
516/// # Returns
517///
518/// A new message list with selected tool responses replaced by compact references.
519///
520/// # Examples
521///
522/// ```text
523/// // Given a message history with several ToolResult/ToolOutput parts:
524/// // [assistant] Let me search...
525/// // [user] ToolResult { content: "..." }
526/// // [assistant] Found it, now querying the database...
527/// // [user] ToolResult { content: "..." }
528/// // [assistant] Processing...
529/// // [user] ToolResult { content: "..." }
530/// //
531/// // Calling remove_tool_responses_middle_out(messages, 0.5) compacts about
532/// // half the tool responses using a middle-out strategy — preserving the most
533/// // recent and oldest results while collapsing middle ones to [compacted].
534/// ```
535#[allow(
536    clippy::cast_precision_loss,
537    clippy::cast_possible_truncation,
538    clippy::cast_sign_loss,
539    clippy::cast_possible_wrap
540)]
541#[must_use]
542pub fn remove_tool_responses_middle_out(mut messages: Vec<Message>, fraction: f32) -> Vec<Message> {
543    let tool_indices: Vec<usize> = messages
544        .iter()
545        .enumerate()
546        .filter(|(_, m)| {
547            m.parts.iter().any(|p| {
548                matches!(
549                    p,
550                    MessagePart::ToolResult { .. } | MessagePart::ToolOutput { .. }
551                )
552            })
553        })
554        .map(|(i, _)| i)
555        .collect();
556
557    if tool_indices.is_empty() {
558        return messages;
559    }
560
561    let n = tool_indices.len();
562    let to_remove = ((n as f32 * fraction).ceil() as usize).min(n);
563
564    let center = n / 2;
565    let mut remove_set: Vec<usize> = Vec::with_capacity(to_remove);
566    let mut left = center as isize - 1;
567    let mut right = center;
568    let mut count = 0;
569
570    while count < to_remove {
571        if right < n {
572            remove_set.push(tool_indices[right]);
573            count += 1;
574            right += 1;
575        }
576        if count < to_remove && left >= 0 {
577            let idx = left as usize;
578            if !remove_set.contains(&tool_indices[idx]) {
579                remove_set.push(tool_indices[idx]);
580                count += 1;
581            }
582        }
583        left -= 1;
584        if left < 0 && right >= n {
585            break;
586        }
587    }
588
589    for &msg_idx in &remove_set {
590        let msg = &mut messages[msg_idx];
591        for part in &mut msg.parts {
592            match part {
593                MessagePart::ToolResult { content, .. } => {
594                    let ref_notice = extract_overflow_ref(content).map_or_else(
595                        || String::from("[compacted]"),
596                        |uuid| {
597                            format!("[tool output pruned; use read_overflow {uuid} to retrieve]")
598                        },
599                    );
600                    *content = ref_notice;
601                }
602                MessagePart::ToolOutput {
603                    body, compacted_at, ..
604                } if compacted_at.is_none() => {
605                    let ref_notice = extract_overflow_ref(body)
606                        .map(|uuid| {
607                            format!("[tool output pruned; use read_overflow {uuid} to retrieve]")
608                        })
609                        .unwrap_or_default();
610                    *body = ref_notice;
611                    *compacted_at = Some(
612                        std::time::SystemTime::now()
613                            .duration_since(std::time::UNIX_EPOCH)
614                            .unwrap_or_default()
615                            .as_secs()
616                            .cast_signed(),
617                    );
618                }
619                _ => {}
620            }
621        }
622        msg.rebuild_content();
623    }
624    messages
625}
626
627/// Extract the overflow UUID from a tool output body, if present.
628///
629/// The overflow notice has the format:
630/// `\n[full output stored — ID: {uuid} — {bytes} bytes, use read_overflow tool to retrieve]`
631///
632/// Returns the UUID substring on success, or `None` if the notice is absent.
633#[must_use]
634pub fn extract_overflow_ref(body: &str) -> Option<&str> {
635    let start = body.find(OVERFLOW_NOTICE_PREFIX)?;
636    let rest = &body[start + OVERFLOW_NOTICE_PREFIX.len()..];
637    let end = rest.find(" \u{2014} ")?;
638    Some(&rest[..end])
639}
640
641fn format_history(messages: &[Message]) -> String {
642    let estimated_len: usize = messages
643        .iter()
644        .map(|m| "[assistant]: ".len() + m.content.len() + 2)
645        .sum();
646    let mut history_text = String::with_capacity(estimated_len);
647    for (i, m) in messages.iter().enumerate() {
648        if i > 0 {
649            history_text.push_str("\n\n");
650        }
651        let role = match m.role {
652            Role::User => "user",
653            Role::Assistant => "assistant",
654            Role::System => "system",
655        };
656        let _ = write!(history_text, "[{role}]: {}", m.content);
657    }
658    history_text
659}
660
661fn guidelines_xml(guidelines: &str) -> String {
662    if guidelines.is_empty() {
663        String::new()
664    } else {
665        format!("\n<compression-guidelines>\n{guidelines}\n</compression-guidelines>\n")
666    }
667}
668
669#[cfg(test)]
670mod tests {
671    use super::*;
672    use zeph_llm::provider::{Message, MessageMetadata, Role};
673
674    fn user_msg(content: &str) -> Message {
675        Message {
676            role: Role::User,
677            content: content.to_string(),
678            parts: vec![],
679            metadata: MessageMetadata::default(),
680        }
681    }
682
683    fn assistant_msg(content: &str) -> Message {
684        Message {
685            role: Role::Assistant,
686            content: content.to_string(),
687            parts: vec![],
688            metadata: MessageMetadata::default(),
689        }
690    }
691
692    #[test]
693    fn build_chunk_prompt_includes_guidelines_section() {
694        let msgs = [user_msg("hello")];
695        let prompt = build_chunk_prompt(&msgs, "be concise");
696        assert!(
697            prompt.contains("<compression-guidelines>"),
698            "prompt must include guidelines XML"
699        );
700        assert!(
701            prompt.contains("be concise"),
702            "prompt must embed the guidelines text"
703        );
704    }
705
706    #[test]
707    fn build_chunk_prompt_no_guidelines_omits_section() {
708        let prompt = build_chunk_prompt(&[], "");
709        assert!(
710            !prompt.contains("<compression-guidelines>"),
711            "empty guidelines must not produce the XML section"
712        );
713    }
714
715    #[test]
716    fn build_anchored_summary_prompt_contains_json_fields() {
717        let prompt = build_anchored_summary_prompt(&[], "");
718        assert!(prompt.contains("session_intent"));
719        assert!(prompt.contains("files_modified"));
720        assert!(prompt.contains("next_steps"));
721    }
722
723    #[test]
724    fn build_metadata_summary_counts_messages() {
725        let msgs = [user_msg("hi"), assistant_msg("hello"), user_msg("bye")];
726        let summary = build_metadata_summary(&msgs, |s, n| s.chars().take(n).collect());
727        assert!(summary.contains("3 (2 user, 1 assistant, 0 system)"));
728    }
729
730    #[test]
731    fn build_tool_pair_summary_prompt_contains_request_and_response() {
732        let req = user_msg("req content");
733        let res = assistant_msg("res content");
734        let prompt = build_tool_pair_summary_prompt(&req, &res);
735        assert!(prompt.contains("req content"));
736        assert!(prompt.contains("res content"));
737    }
738
739    #[test]
740    fn extract_overflow_ref_returns_uuid_when_present() {
741        let uuid = "550e8400-e29b-41d4-a716-446655440000";
742        let body =
743            format!("some output\n[full output stored \u{2014} ID: {uuid} \u{2014} 12345 bytes]");
744        assert_eq!(extract_overflow_ref(&body), Some(uuid));
745    }
746
747    #[test]
748    fn extract_overflow_ref_returns_none_when_absent() {
749        assert_eq!(extract_overflow_ref("normal output"), None);
750    }
751
752    fn tool_result_msg(content: &str) -> Message {
753        use zeph_llm::provider::MessagePart;
754        Message {
755            role: Role::User,
756            content: content.to_string(),
757            parts: vec![
758                MessagePart::ToolUse {
759                    id: "t1".into(),
760                    name: "bash".into(),
761                    input: serde_json::Value::Null,
762                },
763                MessagePart::ToolResult {
764                    tool_use_id: "t1".into(),
765                    content: content.to_string(),
766                    is_error: false,
767                },
768            ],
769            metadata: MessageMetadata::default(),
770        }
771    }
772
773    #[test]
774    fn remove_tool_responses_middle_out_clears_correct_fraction() {
775        // 4 tool messages, fraction=0.5 → ceil(4*0.5)=2 must be replaced with [compacted]
776        let mut messages = vec![
777            tool_result_msg("out0"),
778            tool_result_msg("out1"),
779            tool_result_msg("out2"),
780            tool_result_msg("out3"),
781        ];
782        messages = remove_tool_responses_middle_out(messages, 0.5);
783
784        let compacted_count = messages
785            .iter()
786            .flat_map(|m| m.parts.iter())
787            .filter(|p| {
788                if let zeph_llm::provider::MessagePart::ToolResult { content, .. } = p {
789                    content == "[compacted]"
790                } else {
791                    false
792                }
793            })
794            .count();
795
796        assert_eq!(
797            compacted_count, 2,
798            "ceil(4 * 0.5) = 2 tool results must be replaced with [compacted]"
799        );
800    }
801
802    #[test]
803    fn remove_tool_responses_middle_out_no_tool_messages_returns_unchanged() {
804        let messages = vec![user_msg("hello"), assistant_msg("hi")];
805        let result = remove_tool_responses_middle_out(messages.clone(), 0.5);
806        assert_eq!(result.len(), messages.len());
807        assert!(
808            result.iter().all(|m| m.parts.is_empty()),
809            "non-tool messages must be unchanged"
810        );
811    }
812}