merry-runtime 0.1.4

Structured session, task ledger, context compaction, memory, permission, and tool runtime for Merry.
Documentation
use crate::{
    AUTO_COMPACT_THRESHOLD_PERCENT, CitationCompactionPolicy, CompactionError, CompactionPrompt,
};
use merry_llm::ReasoningEffort;
use std::num::{NonZeroU64, NonZeroUsize};

/// Policy governing how compacted session history is retained in memory and on disk.
///
/// Orthogonal to persistence (`session_store`):
/// - [`Self::Archive`] (the default) retains compacted history cleanly:
///   - When a `session_store` is attached, compaction always rotates turns `<= compacted_through`
///     out of hot memory and live `journal.jsonl` into `<session_dir>/archive/seg_NNNN.jsonl`
///     (plus a unified deterministic Markdown archive `seg_NNNN.md` + `INDEX.md` when enabled),
///     keeping startup and hot RAM bounded. By default there is no segment or byte cap, so full
///     history is preserved; optional `max_segments` / `max_bytes` caps enable rolling eviction
///     and orphan image GC for long-lived bots.
///   - When no `session_store` is attached, nothing is written to disk and turns stay in memory
///     so history is not silently discarded.
/// - [`Self::ContextOnly`] retains only the active post-compaction tail and checkpoint summary:
///   prunes compacted turns from hot memory, rewrites `journal.jsonl` when persisted, deletes
///   unreferenced sidecar images, and writes no `archive/` files.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HistoryRetention {
    /// Rotate compacted segments into `<session_dir>/archive/` when persisted
    /// (unbounded by default; optionally capped via [`ArchiveConfig`]).
    Archive(ArchiveConfig),
    /// Retain only the active post-compaction tail and compacted checkpoint summary.
    ContextOnly,
}

impl Default for HistoryRetention {
    fn default() -> Self {
        Self::Archive(ArchiveConfig::new())
    }
}

impl HistoryRetention {
    /// Retains all compacted history in `<session_dir>/archive/` with no retention cap.
    #[must_use]
    pub const fn archive() -> Self {
        Self::Archive(ArchiveConfig::new())
    }

    /// Retains compacted history in `<session_dir>/archive/` using custom [`ArchiveConfig`].
    #[must_use]
    pub const fn archive_with(config: ArchiveConfig) -> Self {
        Self::Archive(config)
    }

    /// Retains only the active context tail and compaction summary.
    #[must_use]
    pub const fn context_only() -> Self {
        Self::ContextOnly
    }

    /// Whether compacted turns should be pruned from hot memory (and rotated on disk when stored).
    #[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,
        }
    }

    /// Returns archive settings when [`Self::Archive`] is active.
    #[must_use]
    pub const fn archive_config(&self) -> Option<ArchiveConfig> {
        match self {
            Self::Archive(config) => Some(*config),
            Self::ContextOnly => None,
        }
    }
}

/// Configuration for [`HistoryRetention::Archive`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ArchiveConfig {
    max_segments: Option<NonZeroUsize>,
    max_bytes: Option<NonZeroU64>,
    export_markdown: bool,
}

impl ArchiveConfig {
    /// Creates archive settings with deterministic Markdown export enabled and no size cap.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            max_segments: None,
            max_bytes: None,
            export_markdown: true,
        }
    }

    /// Sets the maximum number of archived segments to retain on disk.
    #[must_use]
    pub const fn with_max_segments(mut self, max_segments: Option<NonZeroUsize>) -> Self {
        self.max_segments = max_segments;
        self
    }

    /// Sets the maximum total byte size across archived segment files.
    #[must_use]
    pub const fn with_max_bytes(mut self, max_bytes: Option<NonZeroU64>) -> Self {
        self.max_bytes = max_bytes;
        self
    }

    /// Enables or disables deterministic Markdown archive export (`seg_NNNN.md` + `INDEX.md`).
    #[must_use]
    pub const fn with_export_markdown(mut self, export_markdown: bool) -> Self {
        self.export_markdown = export_markdown;
        self
    }

    /// Maximum number of archived segments retained on disk, if bounded.
    #[must_use]
    pub const fn max_segments(&self) -> Option<NonZeroUsize> {
        self.max_segments
    }

    /// Maximum total bytes retained across archived segment files, if bounded.
    #[must_use]
    pub const fn max_bytes(&self) -> Option<NonZeroU64> {
        self.max_bytes
    }

    /// Whether deterministic Markdown archives (`seg_NNNN.md` + `INDEX.md`) are written.
    #[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()
}

/// Runtime-owned policy for checkpoint compaction.
///
/// `enabled`, `policy`, and `trigger_threshold_percent` drive automatic compaction before
/// a provider request. Manual [`crate::Runtime::compact_context`] calls default to this
/// configured policy (and accept a per-call override), while
/// [`crate::Runtime::compact_context_once`] always requires an explicit
/// [`CitationCompactionPolicy`].
///
/// `reasoning_effort` and `prompt` are not path-specific: they apply to every compaction
/// request, automatic or manual, which is why this type is named for compaction
/// rather than for one of its callers.
///
/// Compaction is a summarization turn over the whole covered window, so it never
/// inherits the primary model's reasoning effort: a primary tuned for hard
/// coding turns can spend its entire output budget reasoning about history and
/// never write the checkpoint. `None` leaves the provider default in place.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompactionConfig {
    enabled: bool,
    policy: CitationCompactionPolicy,
    reasoning_effort: Option<ReasoningEffort>,
    trigger_threshold_percent: u8,
    prompt: CompactionPrompt,
}

impl CompactionConfig {
    /// Enables automatic compaction with the provided policy.
    #[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(),
        }
    }

    /// Disables automatic compaction.
    ///
    /// The policy remains populated with defaults so disabled configs can be
    /// inspected or re-enabled by callers without constructing a dummy policy.
    #[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
    }

    /// Replaces the summary acceptance and retention policy.
    #[must_use]
    pub const fn with_policy(mut self, policy: CitationCompactionPolicy) -> Self {
        self.policy = policy;
        self
    }

    /// Returns a copy configured with a reasoning-effort level for compaction.
    #[must_use]
    pub fn with_reasoning_effort(mut self, reasoning_effort: Option<ReasoningEffort>) -> Self {
        self.reasoning_effort = reasoning_effort;
        self
    }

    /// Optional reasoning-effort level for compaction model requests.
    #[must_use]
    pub fn reasoning_effort(&self) -> Option<&ReasoningEffort> {
        self.reasoning_effort.as_ref()
    }

    /// Sets the dynamic-body usage percentage (`1..=100`) that triggers automatic compaction.
    ///
    /// Defaults to [`AUTO_COMPACT_THRESHOLD_PERCENT`] (`85`).
    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)
    }

    /// Dynamic-body usage percentage (`1..=100`) that triggers automatic compaction.
    #[must_use]
    pub const fn trigger_threshold_percent(&self) -> u8 {
        self.trigger_threshold_percent
    }

    /// Sets the prompt configuration applied to both automatic and manual compaction requests.
    #[must_use]
    pub fn with_prompt(mut self, prompt: CompactionPrompt) -> Self {
        self.prompt = prompt;
        self
    }

    /// Prompt configuration applied to automatic and manual compaction requests.
    #[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);
    }
}