forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_tracker.rs
More file actions
397 lines (350 loc) · 14.1 KB
/
cache_tracker.rs
File metadata and controls
397 lines (350 loc) · 14.1 KB
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
//! Client-side cache tracking for append-only validation
//!
//! When providers don't report cache tokens, we can still detect cache violations
//! by tracking the message prefix ourselves. If the prefix changes between requests,
//! we know the cache was invalidated.
//!
//! This is a fallback mechanism for providers like Fireworks (via OpenRouter) that
//! have automatic caching but don't report cache hit/miss metrics.
use crate::message::{Message, stable_message_hash};
use std::collections::VecDeque;
/// Maximum number of prefix hashes to remember (for detecting intermittent violations)
const MAX_HISTORY: usize = 10;
/// Tracks message prefixes to detect cache violations
#[derive(Debug, Clone, Default)]
pub struct CacheTracker {
/// Hash of the previous message prefix
previous_prefix_hash: Option<u64>,
/// Number of messages in the previous request
previous_message_count: usize,
/// Turn counter (number of complete request/response cycles)
turn_count: u32,
/// History of prefix hashes for debugging
hash_history: VecDeque<u64>,
/// Whether append-only was violated on the last request
last_violation: Option<CacheViolation>,
}
/// Information about a cache violation
#[derive(Debug, Clone)]
pub struct CacheViolation {
/// Turn number when violation occurred
pub turn: u32,
/// Number of messages at time of violation
pub message_count: usize,
/// Expected prefix hash
pub _expected_hash: String,
/// Actual prefix hash
pub _actual_hash: String,
/// Human-readable reason
pub reason: String,
}
impl CacheTracker {
pub fn new() -> Self {
Self::default()
}
fn hash_label(hash: u64) -> String {
format!("{hash:016x}")
}
fn prefix_hashes_for_messages(messages: &[Message]) -> Vec<u64> {
let mut prefix_hashes = Vec::with_capacity(messages.len());
for message in messages {
let message_hash = stable_message_hash(message);
let prefix_hash = prefix_hashes
.last()
.copied()
.map(|prev| crate::message::extend_stable_hash(prev, message_hash))
.unwrap_or(message_hash);
prefix_hashes.push(prefix_hash);
}
prefix_hashes
}
/// Record a request and check for cache violations
///
/// Call this BEFORE sending each request to the provider.
/// Returns Some(violation) if the append-only property was violated.
pub fn record_request(&mut self, messages: &[Message]) -> Option<CacheViolation> {
let prefix_hashes = Self::prefix_hashes_for_messages(messages);
self.record_prefix_hashes(&prefix_hashes)
}
pub fn record_prefix_hashes(&mut self, prefix_hashes: &[u64]) -> Option<CacheViolation> {
let current_count = prefix_hashes.len();
let current_full_hash = prefix_hashes.last().copied();
let previous_count = self.previous_message_count;
let prefix_hash_at_previous_count = if previous_count == 0 || previous_count > current_count
{
None
} else {
Some(prefix_hashes[previous_count - 1])
};
self.record_prefix_hash_snapshot(
current_count,
prefix_hash_at_previous_count,
current_full_hash,
)
}
pub fn record_prefix_hash_snapshot(
&mut self,
current_count: usize,
prefix_hash_at_previous_count: Option<u64>,
current_full_hash: Option<u64>,
) -> Option<CacheViolation> {
self.turn_count += 1;
// First turn - just record the baseline
if self.turn_count == 1 || self.previous_prefix_hash.is_none() {
let hash = current_full_hash.unwrap_or(0);
self.previous_prefix_hash = Some(hash);
self.previous_message_count = current_count;
self.hash_history.push_back(hash);
if self.hash_history.len() > MAX_HISTORY {
self.hash_history.pop_front();
}
self.last_violation = None;
return None;
}
let previous_hash = self.previous_prefix_hash.as_ref()?;
let previous_count = self.previous_message_count;
// For append-only caching, the current messages should START with
// all the previous messages (same prefix)
if current_count < previous_count {
// Messages were removed - definite violation
let current_hash = current_full_hash.unwrap_or(0);
let violation = CacheViolation {
turn: self.turn_count,
message_count: current_count,
_expected_hash: Self::hash_label(*previous_hash),
_actual_hash: Self::hash_label(current_hash),
reason: format!(
"Messages removed: had {} messages, now have {}",
previous_count, current_count
),
};
// Update state
self.previous_prefix_hash = Some(current_hash);
self.previous_message_count = current_count;
self.hash_history.push_back(current_hash);
if self.hash_history.len() > MAX_HISTORY {
self.hash_history.pop_front();
}
self.last_violation = Some(violation.clone());
return Some(violation);
}
// Check if the prefix (first N messages) matches
let prefix_hash = prefix_hash_at_previous_count.unwrap_or(0);
if prefix_hash != *previous_hash {
// Prefix changed - violation
let violation = CacheViolation {
turn: self.turn_count,
message_count: current_count,
_expected_hash: Self::hash_label(*previous_hash),
_actual_hash: Self::hash_label(prefix_hash),
reason: format!(
"Prefix modified: first {} messages changed (hash {} -> {})",
previous_count,
Self::hash_label(*previous_hash),
Self::hash_label(prefix_hash)
),
};
// Update state
let current_hash = current_full_hash.unwrap_or(0);
self.previous_prefix_hash = Some(current_hash);
self.previous_message_count = current_count;
self.hash_history.push_back(current_hash);
if self.hash_history.len() > MAX_HISTORY {
self.hash_history.pop_front();
}
self.last_violation = Some(violation.clone());
return Some(violation);
}
// No violation - update state with new full message list
let full_hash = current_full_hash.unwrap_or(0);
self.previous_prefix_hash = Some(full_hash);
self.previous_message_count = current_count;
self.hash_history.push_back(full_hash);
if self.hash_history.len() > MAX_HISTORY {
self.hash_history.pop_front();
}
self.last_violation = None;
None
}
/// Get the current turn count
pub fn turn_count(&self) -> u32 {
self.turn_count
}
pub fn previous_message_count(&self) -> usize {
self.previous_message_count
}
/// Reset the tracker (e.g., when switching models or compacting)
pub fn reset(&mut self) {
self.previous_prefix_hash = None;
self.previous_message_count = 0;
self.turn_count = 0;
self.hash_history.clear();
self.last_violation = None;
}
/// Check if we detected a violation on the last request
pub fn had_violation(&self) -> bool {
self.last_violation.is_some()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::message::{ContentBlock, Role};
fn make_message(role: Role, text: &str) -> Message {
Message {
role,
content: vec![ContentBlock::Text {
text: text.to_string(),
cache_control: None,
}],
timestamp: None,
tool_duration_ms: None,
}
}
#[test]
fn test_append_only_no_violation() {
let mut tracker = CacheTracker::new();
// First request
let msgs1 = vec![make_message(Role::User, "Hello")];
assert!(tracker.record_request(&msgs1).is_none());
// Second request - append assistant response and new user message
let msgs2 = vec![
make_message(Role::User, "Hello"),
make_message(Role::Assistant, "Hi there!"),
make_message(Role::User, "How are you?"),
];
assert!(tracker.record_request(&msgs2).is_none());
// Third request - append more
let msgs3 = vec![
make_message(Role::User, "Hello"),
make_message(Role::Assistant, "Hi there!"),
make_message(Role::User, "How are you?"),
make_message(Role::Assistant, "I'm doing well!"),
make_message(Role::User, "Great!"),
];
assert!(tracker.record_request(&msgs3).is_none());
}
#[test]
fn test_prefix_modification_violation() {
let mut tracker = CacheTracker::new();
// First request
let msgs1 = vec![make_message(Role::User, "Hello")];
assert!(tracker.record_request(&msgs1).is_none());
// Second request - modify the first message (violation!)
let msgs2 = vec![
make_message(Role::User, "Hello MODIFIED"),
make_message(Role::Assistant, "Hi there!"),
];
let violation = tracker.record_request(&msgs2);
assert!(violation.is_some());
assert!(violation.unwrap().reason.contains("Prefix modified"));
}
#[test]
fn test_message_removal_violation() {
let mut tracker = CacheTracker::new();
// First request with multiple messages
let msgs1 = vec![
make_message(Role::User, "Hello"),
make_message(Role::Assistant, "Hi there!"),
make_message(Role::User, "How are you?"),
];
assert!(tracker.record_request(&msgs1).is_none());
// Second request - remove messages (violation!)
let msgs2 = vec![make_message(Role::User, "Hello")];
let violation = tracker.record_request(&msgs2);
assert!(violation.is_some());
assert!(violation.unwrap().reason.contains("Messages removed"));
}
#[test]
fn test_reset() {
let mut tracker = CacheTracker::new();
let msgs1 = vec![make_message(Role::User, "Hello")];
tracker.record_request(&msgs1);
// Reset and start fresh - no violation
tracker.reset();
let msgs2 = vec![make_message(Role::User, "Different message")];
assert!(tracker.record_request(&msgs2).is_none());
}
/// Verify normal multi-turn conversation growth never triggers a false positive.
/// This is the pattern that happens every real session: each turn appends a new
/// assistant response and user message onto the unchanged prior history.
#[test]
fn test_no_false_positive_on_normal_growth() {
let mut tracker = CacheTracker::new();
// Turn 1: initial user message (no memory)
let turn1 = vec![make_message(Role::User, "Q1")];
assert!(
tracker.record_request(&turn1).is_none(),
"Turn 1: no violation"
);
// Turn 2: assistant replied, user sent follow-up (base messages without memory)
let turn2 = vec![
make_message(Role::User, "Q1"),
make_message(Role::Assistant, "A1"),
make_message(Role::User, "Q2"),
];
assert!(
tracker.record_request(&turn2).is_none(),
"Turn 2: no violation"
);
// Turn 3: another exchange appended
let turn3 = vec![
make_message(Role::User, "Q1"),
make_message(Role::Assistant, "A1"),
make_message(Role::User, "Q2"),
make_message(Role::Assistant, "A2"),
make_message(Role::User, "Q3"),
];
assert!(
tracker.record_request(&turn3).is_none(),
"Turn 3: no violation"
);
// Turn 4: another exchange appended
let turn4 = vec![
make_message(Role::User, "Q1"),
make_message(Role::Assistant, "A1"),
make_message(Role::User, "Q2"),
make_message(Role::Assistant, "A2"),
make_message(Role::User, "Q3"),
make_message(Role::Assistant, "A3"),
make_message(Role::User, "Q4"),
];
assert!(
tracker.record_request(&turn4).is_none(),
"Turn 4: no violation"
);
}
/// Verify that memory injection (an ephemeral suffix NOT saved to conversation history)
/// does NOT cause false positives when tracked BEFORE the memory push.
/// This validates the fix where agent.rs calls record_request(&messages) — not
/// record_request(&messages_with_memory) — so the ephemeral suffix is invisible to
/// the tracker.
#[test]
fn test_no_false_positive_when_memory_excluded() {
let mut tracker = CacheTracker::new();
// Turn 1: base messages only (no memory injected yet)
let base1 = vec![make_message(Role::User, "Q1")];
assert!(tracker.record_request(&base1).is_none());
// Turn 2: conversation grew, no memory → no violation
let base2 = vec![
make_message(Role::User, "Q1"),
make_message(Role::Assistant, "A1"),
make_message(Role::User, "Q2"),
];
assert!(tracker.record_request(&base2).is_none());
// Turn 3: conversation grew again → no violation
// (If we had tracked messages_with_memory containing a memory suffix at turn 2,
// this would falsely flag a violation because the suffix is replaced by A2 here.)
let base3 = vec![
make_message(Role::User, "Q1"),
make_message(Role::Assistant, "A1"),
make_message(Role::User, "Q2"),
make_message(Role::Assistant, "A2"),
make_message(Role::User, "Q3"),
];
assert!(
tracker.record_request(&base3).is_none(),
"Should NOT flag a violation — memory suffix from turn 2 is NOT tracked here"
);
}
}