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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Failure detection for ACON compression guidelines (#1647).
//!
//! Pure detection helpers live in [`zeph_context::compression_feedback`].
//! This module contains only the `Agent`-level integration: logging to `SQLite`
//! and extracting the compaction summary from message history.
use crate::agent::Agent;
use crate::channel::Channel;
pub use zeph_context::compression_feedback::{
classify_failure_category, detect_compression_failure,
};
impl<C: Channel> Agent<C> {
/// Check the LLM response for signs of context loss after compaction.
///
/// Fires only when:
/// 1. The feature is enabled in config
/// 2. A hard compaction has occurred in this session
/// 3. The number of turns since last compaction is within the detection window
/// 4. Both uncertainty and prior-context signals are present in the response
///
/// If all conditions are met, logs a failure pair to `SQLite` (non-fatal on error).
pub(crate) async fn maybe_log_compression_failure(&self, response: &str) {
let config = &self.memory_state.compaction.compression_guidelines_config;
if !config.enabled {
return;
}
let Some(turns) = self.context_manager.turns_since_last_hard_compaction else {
return;
};
if turns > config.detection_window_turns {
return;
}
let Some(detection_meta) = detect_compression_failure(response, true) else {
return;
};
tracing::debug!(meta = %detection_meta, "compression failure detected");
let compressed_context = self.extract_last_compaction_summary();
let Some(memory) = &self.memory_state.persistence.memory else {
return;
};
let Some(cid) = self.memory_state.persistence.conversation_id else {
return;
};
let category = classify_failure_category(&compressed_context);
let sqlite = memory.sqlite();
if let Err(e) = sqlite
.log_compression_failure(cid, &compressed_context, response, category)
.await
{
tracing::warn!("failed to log compression failure pair: {e:#}");
} else {
tracing::info!(
turns_since_compaction = turns,
category,
"compression failure detected and logged"
);
}
}
/// Extract the most recent compaction summary text from the message history.
///
/// After `compact_context()`, a `[conversation summary — N messages compacted]`
/// system message is inserted at index 1. This method scans positions 1..4
/// to find and return that summary text.
fn extract_last_compaction_summary(&self) -> String {
const SUMMARY_MARKER: &str = "[conversation summary";
for msg in self.msg.messages.iter().skip(1).take(3) {
if msg.content.starts_with(SUMMARY_MARKER) {
return msg.content.clone();
}
}
String::new()
}
}