use crate::{
AUTO_COMPACT_THRESHOLD_PERCENT, CitationCompactionPolicy, CompactionError, CompactionPrompt,
};
use merry_llm::ReasoningEffort;
use std::num::{NonZeroU64, NonZeroUsize};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HistoryRetention {
Archive(ArchiveConfig),
ContextOnly,
}
impl Default for HistoryRetention {
fn default() -> Self {
Self::Archive(ArchiveConfig::new())
}
}
impl HistoryRetention {
#[must_use]
pub const fn archive() -> Self {
Self::Archive(ArchiveConfig::new())
}
#[must_use]
pub const fn archive_with(config: ArchiveConfig) -> Self {
Self::Archive(config)
}
#[must_use]
pub const fn context_only() -> Self {
Self::ContextOnly
}
#[must_use]
pub(crate) const fn prunes_compacted_history(&self, has_session_store: bool) -> bool {
match self {
Self::ContextOnly => true,
Self::Archive(_) => has_session_store,
}
}
#[must_use]
pub const fn archive_config(&self) -> Option<ArchiveConfig> {
match self {
Self::Archive(config) => Some(*config),
Self::ContextOnly => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ArchiveConfig {
max_segments: Option<NonZeroUsize>,
max_bytes: Option<NonZeroU64>,
export_markdown: bool,
}
impl ArchiveConfig {
#[must_use]
pub const fn new() -> Self {
Self {
max_segments: None,
max_bytes: None,
export_markdown: true,
}
}
#[must_use]
pub const fn with_max_segments(mut self, max_segments: Option<NonZeroUsize>) -> Self {
self.max_segments = max_segments;
self
}
#[must_use]
pub const fn with_max_bytes(mut self, max_bytes: Option<NonZeroU64>) -> Self {
self.max_bytes = max_bytes;
self
}
#[must_use]
pub const fn with_export_markdown(mut self, export_markdown: bool) -> Self {
self.export_markdown = export_markdown;
self
}
#[must_use]
pub const fn max_segments(&self) -> Option<NonZeroUsize> {
self.max_segments
}
#[must_use]
pub const fn max_bytes(&self) -> Option<NonZeroU64> {
self.max_bytes
}
#[must_use]
pub const fn export_markdown(&self) -> bool {
self.export_markdown
}
}
impl Default for ArchiveConfig {
fn default() -> Self {
Self::new()
}
}
fn default_automatic_compaction_policy() -> CitationCompactionPolicy {
CitationCompactionPolicy::default()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompactionConfig {
enabled: bool,
policy: CitationCompactionPolicy,
reasoning_effort: Option<ReasoningEffort>,
trigger_threshold_percent: u8,
prompt: CompactionPrompt,
}
impl CompactionConfig {
#[must_use]
pub fn enabled(policy: CitationCompactionPolicy) -> Self {
Self {
enabled: true,
policy,
reasoning_effort: None,
trigger_threshold_percent: AUTO_COMPACT_THRESHOLD_PERCENT,
prompt: CompactionPrompt::default(),
}
}
#[must_use]
pub fn disabled() -> Self {
Self {
enabled: false,
policy: default_automatic_compaction_policy(),
reasoning_effort: None,
trigger_threshold_percent: AUTO_COMPACT_THRESHOLD_PERCENT,
prompt: CompactionPrompt::default(),
}
}
#[must_use]
pub const fn is_enabled(&self) -> bool {
self.enabled
}
#[must_use]
pub const fn policy(&self) -> CitationCompactionPolicy {
self.policy
}
#[must_use]
pub const fn with_policy(mut self, policy: CitationCompactionPolicy) -> Self {
self.policy = policy;
self
}
#[must_use]
pub fn with_reasoning_effort(mut self, reasoning_effort: Option<ReasoningEffort>) -> Self {
self.reasoning_effort = reasoning_effort;
self
}
#[must_use]
pub fn reasoning_effort(&self) -> Option<&ReasoningEffort> {
self.reasoning_effort.as_ref()
}
pub fn with_trigger_threshold_percent(mut self, percent: u8) -> Result<Self, CompactionError> {
if !(1..=100).contains(&percent) {
return Err(CompactionError::InvalidTriggerThresholdPercent { percent });
}
self.trigger_threshold_percent = percent;
Ok(self)
}
#[must_use]
pub const fn trigger_threshold_percent(&self) -> u8 {
self.trigger_threshold_percent
}
#[must_use]
pub fn with_prompt(mut self, prompt: CompactionPrompt) -> Self {
self.prompt = prompt;
self
}
#[must_use]
pub fn prompt(&self) -> &CompactionPrompt {
&self.prompt
}
}
impl Default for CompactionConfig {
fn default() -> Self {
Self::enabled(default_automatic_compaction_policy())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn compaction_config_validates_trigger_threshold_bounds() {
assert_eq!(
CompactionConfig::default().with_trigger_threshold_percent(0),
Err(CompactionError::InvalidTriggerThresholdPercent { percent: 0 })
);
assert_eq!(
CompactionConfig::default().with_trigger_threshold_percent(101),
Err(CompactionError::InvalidTriggerThresholdPercent { percent: 101 })
);
let low = CompactionConfig::default()
.with_trigger_threshold_percent(1)
.expect("1% should be valid");
assert_eq!(low.trigger_threshold_percent(), 1);
let high = CompactionConfig::default()
.with_trigger_threshold_percent(100)
.expect("100% should be valid");
assert_eq!(high.trigger_threshold_percent(), 100);
}
#[test]
fn compaction_prompt_sanitizes_fields_and_rejects_blank() {
assert_eq!(
CompactionPrompt::custom(" \r\n\t\x00 "),
Err(CompactionError::EmptyPromptField {
field: "instructions"
})
);
assert_eq!(
CompactionPrompt::default().with_user_context(" \r\n\t\x00 "),
Err(CompactionError::EmptyPromptField {
field: "user_context"
})
);
let prompt = CompactionPrompt::custom("Line 1\r\nLine\x00 2")
.expect("non-blank instructions should sanitize")
.with_user_context("Keep\x00 auth\r\ninvariants")
.expect("non-blank user context should sanitize");
let configured = CompactionConfig::default().with_prompt(prompt);
assert_eq!(configured.prompt().instructions(), Some("Line 1\nLine 2"));
assert_eq!(
configured.prompt().user_context(),
Some("Keep auth\ninvariants")
);
let reset = configured.with_prompt(CompactionPrompt::default());
assert_eq!(reset.prompt().instructions(), None);
assert_eq!(reset.prompt().user_context(), None);
}
}