aither-agent 0.29.0

AI agent framework with planning, memory, and tool execution for aither
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Prompt cache control for the assembled window.
//!
//! Every provider's prompt cache keys on an unbroken prefix, but they differ
//! in how that prefix is nominated. OpenAI-compatible endpoints want a stable
//! routing key so successive requests from one session land on the same
//! shard. Claude wants explicit breakpoints naming the blocks worth
//! persisting. Both are derived from the same two facts the agent already
//! knows: the window layout, and whether the cacheable part of it changed
//! since the previous request.
//!
//! The second fact is why this type exists rather than a free function.
//! Reassembly and compaction deliberately rewrite the stable prefix, which
//! makes everything cached under the old key dead weight. [`PromptCache`]
//! notices the change through a fingerprint and bumps an *epoch* that is
//! folded into the cache key, so a rewritten window asks for a new shard
//! instead of repeatedly missing on the old one.

use std::time::Duration;

use aither_core::llm::Message;
use aither_core::llm::model::Parameters;
use aither_core::llm::model::{
    CacheOptions, ClaudeCacheBreakpointTarget, ClaudeExplicitCacheBreakpoint,
    ClaudeExplicitCacheBreakpoints, ClaudePromptCache, ClaudePromptCacheTtl,
};

/// How long the conversation's prompt cache stays warm.
///
/// This is what an idle gap is measured against: reassembly rewrites the
/// cacheable prefix, so it is only free once the provider's copy can no
/// longer be read anyway.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CacheLifetime {
    /// The cache expires after this long without a hit.
    Limited(Duration),
    /// The cache never expires; rewriting the prefix is never free.
    Permanent,
}

/// Derives per-request cache controls for one agent session.
#[derive(Debug, Clone)]
pub struct PromptCache {
    /// Stable identity for this session's cache shard.
    session_key: String,
    /// Incremented whenever the cacheable prefix is rewritten.
    epoch: u32,
    /// Fingerprint of the stable prefix as of the last request.
    fingerprint: Option<u64>,
    /// How long Claude should retain the cached prefix.
    ttl: ClaudePromptCacheTtl,
    /// How long the conversation's cache stays warm, across providers.
    lifetime: CacheLifetime,
}

impl PromptCache {
    /// Creates a cache controller for a session identified by `session_key`.
    ///
    /// Agent runs are long and their prefixes are large, so the one-hour
    /// retention is the default: a five-minute TTL expires during ordinary
    /// think time and turns the next request into a full re-read.
    #[must_use]
    pub fn new(session_key: impl Into<String>) -> Self {
        Self {
            session_key: session_key.into(),
            epoch: 0,
            fingerprint: None,
            ttl: ClaudePromptCacheTtl::OneHour,
            lifetime: CacheLifetime::Limited(Duration::from_secs(3600)),
        }
    }

    /// Overrides the Claude cache retention.
    ///
    /// The cache lifetime follows the retention, since Claude's advertised
    /// TTL is exactly how long the provider promises to keep the prefix.
    #[must_use]
    pub const fn with_ttl(mut self, ttl: ClaudePromptCacheTtl) -> Self {
        self.ttl = ttl;
        self.lifetime = CacheLifetime::Limited(match ttl {
            ClaudePromptCacheTtl::FiveMinutes => Duration::from_secs(300),
            ClaudePromptCacheTtl::OneHour => Duration::from_secs(3600),
        });
        self
    }

    /// Overrides how long the conversation's cache is trusted to stay warm.
    ///
    /// A host whose provider retains prompts longer than the breakpoints ask
    /// for (or forever) widens the idle-reassembly gap here; a lifetime of
    /// [`CacheLifetime::Permanent`] means an idle gap never justifies a
    /// reassembly.
    #[must_use]
    pub const fn with_lifetime(mut self, lifetime: CacheLifetime) -> Self {
        self.lifetime = lifetime;
        self
    }

    /// How long the conversation's cache stays warm.
    #[must_use]
    pub const fn lifetime(&self) -> CacheLifetime {
        self.lifetime
    }

    /// Resumes a session's key at `epoch`, as read back from
    /// [`PromptCache::epoch`] on the agent being replaced.
    ///
    /// A rebuilt agent keyed by the same session identity and epoch asks the
    /// provider for the same cache shard its predecessor warmed, instead of
    /// restarting at epoch zero.
    #[must_use]
    pub const fn with_epoch(mut self, epoch: u32) -> Self {
        self.epoch = epoch;
        self
    }

    /// The current cache key, which changes only when the prefix is rewritten.
    #[must_use]
    pub fn key(&self) -> String {
        format!("{}:{}", self.session_key, self.epoch)
    }

    /// How many times the cacheable prefix has been rewritten.
    #[must_use]
    pub const fn epoch(&self) -> u32 {
        self.epoch
    }

    /// Records the stable prefix fingerprint for the request about to be sent.
    ///
    /// Returns `true` when the prefix changed, which means the previous
    /// epoch's cache is now unreachable.
    pub fn observe_prefix(&mut self, fingerprint: u64) -> bool {
        match self.fingerprint {
            Some(previous) if previous == fingerprint => false,
            Some(_) => {
                self.epoch += 1;
                self.fingerprint = Some(fingerprint);
                tracing::debug!(epoch = self.epoch, "prompt cache prefix rewritten");
                true
            }
            None => {
                self.fingerprint = Some(fingerprint);
                false
            }
        }
    }

    /// Applies cache controls to `parameters` for a request.
    ///
    /// `messages` is the assembled request, `has_tools` says whether tool
    /// definitions are attached, and `mid_conversation_system` whether the
    /// model keeps later system messages in the conversation; all three decide
    /// where a Claude breakpoint can legally be placed.
    #[must_use]
    pub fn apply(
        &self,
        mut parameters: Parameters,
        messages: &[Message],
        has_tools: bool,
        mid_conversation_system: bool,
    ) -> Parameters {
        parameters.cache = CacheOptions {
            openai: Some(aither_core::llm::model::OpenAIPromptCache {
                key: Some(self.key()),
                retention: Some(aither_core::llm::model::OpenAIPromptCacheRetention::Hours24),
            }),
            claude: Some(ClaudePromptCache::explicit(
                self.ttl,
                Self::breakpoints(messages, has_tools, mid_conversation_system),
            )),
            gemini: None,
        };
        parameters
    }

    /// Chooses the Claude breakpoints for an assembled request.
    ///
    /// The subtlety is the last message: it is the live tail, rebuilt from
    /// context sources on every single request, so caching *it* would write a
    /// new entry each turn and read none of them back. The breakpoint goes on
    /// the message before it — the newest byte that will still be there next
    /// time.
    fn breakpoints(
        messages: &[Message],
        has_tools: bool,
        mid_conversation_system: bool,
    ) -> ClaudeExplicitCacheBreakpoints {
        // Tool definitions precede the system prompt in Claude's cacheable
        // order, so a breakpoint there protects the whole tool surface.
        let breakpoints = if has_tools {
            ClaudeExplicitCacheBreakpoints::new(ClaudeExplicitCacheBreakpoint::new(
                ClaudeCacheBreakpointTarget::LastTool,
            ))
            .with_second(ClaudeExplicitCacheBreakpoint::new(
                ClaudeCacheBreakpointTarget::LastSystem,
            ))
        } else {
            ClaudeExplicitCacheBreakpoints::new(ClaudeExplicitCacheBreakpoint::new(
                ClaudeCacheBreakpointTarget::LastSystem,
            ))
        };
        let Some(index) = stable_conversation_end(messages)
            .and_then(|end| claude_message_index(messages, end, mid_conversation_system))
        else {
            return breakpoints;
        };
        let breakpoint = ClaudeExplicitCacheBreakpoint::new(ClaudeCacheBreakpointTarget::Message {
            message_index: index,
            block_index: 0,
        });
        if has_tools {
            breakpoints.with_third(breakpoint)
        } else {
            breakpoints.with_second(breakpoint)
        }
    }
}

/// Index of the last conversation message that will still be present, byte
/// for byte, in the next request.
///
/// Returns `None` when there is nothing but the prefix and the live tail.
pub(crate) const fn stable_conversation_end(messages: &[Message]) -> Option<usize> {
    // The final message is the live tail; the one before it is the newest
    // append-only item.
    messages.len().checked_sub(2)
}

/// Position, in the message list Claude is sent, of `messages[index]`.
///
/// Claude lifts the leading system messages into the top-level `system`. A
/// model that takes mid-conversation system messages keeps every later one in
/// place; any other folds all of them into `system`, so none counts. Returns
/// `None` when the message itself is lifted out.
fn claude_message_index(
    messages: &[Message],
    index: usize,
    mid_conversation_system: bool,
) -> Option<usize> {
    let is_system = |message: &Message| message.role() == aither_core::llm::Role::System;
    let lifted = if mid_conversation_system {
        messages
            .iter()
            .take_while(|message| is_system(message))
            .count()
    } else {
        if is_system(&messages[index]) {
            return None;
        }
        messages[..index]
            .iter()
            .filter(|message| is_system(message))
            .count()
    };
    index.checked_sub(lifted)
}

#[cfg(test)]
mod tests {
    use super::*;
    use aither_core::llm::model::ClaudePromptCacheStrategy;

    fn claude_strategy(parameters: &Parameters) -> ClaudePromptCacheStrategy {
        parameters
            .cache
            .claude
            .expect("claude cache options are set")
            .strategy
    }

    #[test]
    fn the_key_is_stable_until_the_prefix_is_rewritten() {
        let mut cache = PromptCache::new("session-1");
        assert!(!cache.observe_prefix(7));
        let first = cache.key();
        assert!(!cache.observe_prefix(7));
        assert_eq!(cache.key(), first);

        assert!(cache.observe_prefix(9));
        assert_ne!(cache.key(), first);
        assert_eq!(cache.epoch(), 1);
    }

    #[test]
    fn a_rebuilt_agent_keeps_the_sessions_key_and_rotates_with_the_epoch() {
        let mut first = PromptCache::new("session-1");
        first.observe_prefix(1);
        first.observe_prefix(2);
        let rebuilt = PromptCache::new("session-1").with_epoch(first.epoch());
        assert_eq!(rebuilt.key(), first.key());
        assert_eq!(rebuilt.key(), "session-1:1");
        assert_ne!(PromptCache::new("session-1").key(), rebuilt.key());
    }

    #[test]
    fn requests_carry_cache_options_for_every_provider_shape() {
        let cache = PromptCache::new("session-1");
        let messages = vec![
            Message::system("prefix"),
            Message::user("hello"),
            Message::system("live tail"),
        ];
        let parameters = cache.apply(Parameters::default(), &messages, true, true);

        assert!(!parameters.cache.is_empty());
        assert_eq!(
            parameters
                .cache
                .openai
                .as_ref()
                .and_then(|openai| openai.key.as_deref()),
            Some("session-1:0")
        );
        let ClaudePromptCacheStrategy::Explicit(breakpoints) = claude_strategy(&parameters) else {
            panic!("expected explicit claude breakpoints");
        };
        assert_eq!(
            breakpoints.first.target,
            ClaudeCacheBreakpointTarget::LastTool
        );
        assert_eq!(
            breakpoints.second.map(|point| point.target),
            Some(ClaudeCacheBreakpointTarget::LastSystem)
        );
    }

    /// The live tail changes every request; caching it would write an entry
    /// per turn and never read one back.
    #[test]
    fn the_message_breakpoint_skips_the_live_tail() {
        let cache = PromptCache::new("session-1");
        let messages = vec![
            Message::system("prefix"),
            Message::user("hello"),
            Message::assistant("hi"),
            Message::system("live tail"),
        ];
        let parameters = cache.apply(Parameters::default(), &messages, false, true);
        let ClaudePromptCacheStrategy::Explicit(breakpoints) = claude_strategy(&parameters) else {
            panic!("expected explicit claude breakpoints");
        };
        assert_eq!(
            breakpoints.second.map(|point| point.target),
            Some(ClaudeCacheBreakpointTarget::Message {
                message_index: 1,
                block_index: 0,
            }),
            "breakpoint must land on the last stable message, not the tail"
        );
    }

    /// The index is into the list Claude is sent, which lacks the leading
    /// system prefix.
    #[test]
    fn the_message_breakpoint_indexes_the_claude_message_list() {
        let cache = PromptCache::new("session-1");
        let messages = vec![
            Message::system("prefix"),
            Message::user("hello"),
            Message::system("live tail"),
        ];
        let parameters = cache.apply(Parameters::default(), &messages, false, true);
        let ClaudePromptCacheStrategy::Explicit(breakpoints) = claude_strategy(&parameters) else {
            panic!("expected explicit claude breakpoints");
        };
        assert_eq!(
            breakpoints.second.map(|point| point.target),
            Some(ClaudeCacheBreakpointTarget::Message {
                message_index: 0,
                block_index: 0,
            })
        );
    }

    /// A model without mid-conversation system messages folds the tail into
    /// `system`, so the Claude list holds only the conversation.
    #[test]
    fn without_the_ability_no_system_message_counts() {
        let cache = PromptCache::new("session-1");
        let messages = vec![
            Message::system("prefix"),
            Message::user("hello"),
            Message::assistant("hi"),
            Message::system("live tail"),
        ];
        let parameters = cache.apply(Parameters::default(), &messages, false, false);
        let ClaudePromptCacheStrategy::Explicit(breakpoints) = claude_strategy(&parameters) else {
            panic!("expected explicit claude breakpoints");
        };
        assert_eq!(
            breakpoints.second.map(|point| point.target),
            Some(ClaudeCacheBreakpointTarget::Message {
                message_index: 1,
                block_index: 0,
            })
        );
    }

    #[test]
    fn a_stable_system_prefix_gets_no_message_breakpoint() {
        let cache = PromptCache::new("session-1");
        let messages = vec![
            Message::system("prefix"),
            Message::system("more prefix"),
            Message::system("live tail"),
        ];
        let parameters = cache.apply(Parameters::default(), &messages, false, true);
        let ClaudePromptCacheStrategy::Explicit(breakpoints) = claude_strategy(&parameters) else {
            panic!("expected explicit claude breakpoints");
        };
        assert!(breakpoints.second.is_none());
    }

    #[test]
    fn with_ttl_keeps_the_lifetime_coherent() {
        let cache = PromptCache::new("s").with_ttl(ClaudePromptCacheTtl::FiveMinutes);
        assert_eq!(
            cache.lifetime(),
            CacheLifetime::Limited(Duration::from_secs(300))
        );
    }

    #[test]
    fn a_prefix_only_request_gets_no_message_breakpoint() {
        let cache = PromptCache::new("session-1");
        let messages = vec![Message::system("prefix")];
        let parameters = cache.apply(Parameters::default(), &messages, false, true);
        let ClaudePromptCacheStrategy::Explicit(breakpoints) = claude_strategy(&parameters) else {
            panic!("expected explicit claude breakpoints");
        };
        assert!(breakpoints.second.is_none());
    }
}