zeph_context/slot.rs
1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Context slot types, compaction outcome, and message-chunking helpers.
5//!
6//! [`ContextSlot`] tags async fetch results so the assembler's `FuturesUnordered`
7//! collector can dispatch results without tuple indexing.
8//!
9//! [`CompactionOutcome`] communicates the result of one compaction attempt to
10//! `maybe_compact` in `zeph-core`.
11
12use zeph_llm::provider::Message;
13
14/// Tagged output of each concurrent context-fetch future.
15///
16/// Using an enum instead of a tuple allows individual sources to be added or
17/// removed (including cfg-gated ones) without rewriting the join combinator.
18#[non_exhaustive]
19pub enum ContextSlot {
20 /// Past-session summaries (contextual recall).
21 Summaries(Option<Message>),
22 /// Cross-session memory recall.
23 CrossSession(Option<Message>),
24 /// Semantic recall result. Carries the formatted message and the top-1 similarity score.
25 SemanticRecall(Option<Message>, Option<f32>),
26 /// Document RAG result.
27 DocumentRag(Option<Message>),
28 /// Past user corrections recalled for this turn.
29 Corrections(Option<Message>),
30 /// Code-index RAG result (repo-map or file context).
31 CodeContext(Option<String>),
32 /// Knowledge graph fact recall.
33 GraphFacts(Option<Message>),
34 /// Persona memory facts injected after the system prompt (#2461).
35 PersonaFacts(Option<Message>),
36 /// Top-k procedural trajectory hints recalled for the current turn (#2498).
37 TrajectoryHints(Option<Message>),
38 /// `TiMem` tree summary nodes recalled for context (#2262).
39 TreeMemory(Option<Message>),
40 /// Distilled reasoning strategies recalled for the current turn (#3343).
41 ///
42 /// The second field carries the `JoinHandle` for the background `mark_reasoning_used` task
43 /// spawned after injection. Callers must store it in `PreparedContext::background_tasks`.
44 ReasoningStrategies(Option<Message>, Option<tokio::task::JoinHandle<()>>),
45}
46
47/// Return type from `compact_context()` that distinguishes between successful compaction,
48/// probe rejection, and no-op.
49///
50/// Gives `maybe_compact()` enough information to handle probe rejection without triggering
51/// the `Exhausted` state — which would only be correct if summarization itself is stuck.
52#[derive(Debug, Clone, Copy, PartialEq, Eq)]
53pub enum CompactionOutcome {
54 /// Messages were drained and replaced with a summary.
55 Compacted,
56 /// Probe rejected the summary — original messages are preserved.
57 /// Caller must NOT check `freed_tokens` or transition to `Exhausted`.
58 ProbeRejected,
59 /// No compaction was performed (too few messages, empty `to_compact`, etc.).
60 NoChange,
61}
62
63/// Prefix prepended to persona memory injections.
64pub const PERSONA_PREFIX: &str = "[Persona context]\n";
65/// Prefix prepended to trajectory-hint injections.
66pub const TRAJECTORY_PREFIX: &str = "[Past experience]\n";
67/// Prefix prepended to reasoning-strategy injections.
68pub const REASONING_PREFIX: &str = "[Reasoning Strategy]\n";
69/// Prefix prepended to `TiMem` tree memory injections.
70pub const TREE_MEMORY_PREFIX: &str = "[Memory summary]\n";
71
72/// Split a message slice into chunks that each fit within `budget` tokens.
73///
74/// Messages larger than `oversized` tokens each get their own chunk. All other
75/// messages are greedily packed. Callers that need at least one chunk will always
76/// receive one (empty `Vec<Message>` wrapped in a single chunk).
77///
78/// `count_message_tokens` is a caller-supplied function that returns the token count
79/// for a single message. This avoids a direct dependency on `zeph-memory::TokenCounter`.
80#[must_use]
81pub fn chunk_messages(
82 messages: &[Message],
83 budget: usize,
84 oversized: usize,
85 count_message_tokens: impl Fn(&Message) -> usize,
86) -> Vec<Vec<Message>> {
87 let mut chunks: Vec<Vec<Message>> = Vec::new();
88 let mut current: Vec<Message> = Vec::new();
89 let mut current_tokens = 0usize;
90
91 for msg in messages {
92 let msg_tokens = count_message_tokens(msg);
93
94 if msg_tokens >= oversized {
95 if !current.is_empty() {
96 chunks.push(std::mem::take(&mut current));
97 current_tokens = 0;
98 }
99 chunks.push(vec![msg.clone()]);
100 } else if current_tokens + msg_tokens > budget && !current.is_empty() {
101 chunks.push(std::mem::take(&mut current));
102 current_tokens = 0;
103 current.push(msg.clone());
104 current_tokens += msg_tokens;
105 } else {
106 current.push(msg.clone());
107 current_tokens += msg_tokens;
108 }
109 }
110
111 if !current.is_empty() {
112 chunks.push(current);
113 }
114
115 if chunks.is_empty() {
116 chunks.push(Vec::new());
117 }
118
119 chunks
120}
121
122/// Cap an LLM summary to `max_chars` characters (SEC-02).
123///
124/// Prevents a misbehaving LLM backend from returning an arbitrarily large summary that
125/// would expand rather than shrink the context window after compaction.
126#[must_use]
127pub fn cap_summary(s: String, max_chars: usize) -> String {
128 match s.char_indices().nth(max_chars) {
129 Some((byte_idx, _)) => {
130 tracing::warn!(
131 original_chars = s.chars().count(),
132 cap = max_chars,
133 "LLM summary exceeded cap, truncating"
134 );
135 format!("{}…", &s[..byte_idx])
136 }
137 None => s,
138 }
139}