merry-runtime 0.1.4

Structured session, task ledger, context compaction, memory, permission, and tool runtime for Merry.
Documentation
//! The automatic compaction phase of one provider step that crossed the threshold.
//!
//! This module owns the whole decision for one step: estimate the fixed dynamic
//! body, build the window budget, prepare a compaction, fit a request the
//! compaction model window can host, and install either a checkpoint replacement
//! or an archive-only reduction. It emits the compaction lifecycle events, so the
//! caller only has to recompile its request and report a completed checkpoint.

use super::super::journal_emission::{
    send_cancelled_event, send_compaction_started_event, send_failed_event,
    trace_provider_step_cancelled, trace_provider_step_failed,
};
use super::super::memory_activation::clear_current_activated_memories;
use super::super::provider_request::{
    RequestContextBudget, StepRequestInputs, estimate_compaction_fixed_dynamic_tokens,
    step_request_compile_diagnostic,
};
use super::super::{RuntimeInner, diagnostic_from_text, runtime_error_message};
use super::{
    ArchiveOnlyReason, CompactionAttempt, CompactionRequestBudget,
    compaction_preparation_for_budget, generate_and_install_compaction,
    install_archive_only_compaction_transactionally, plan_compaction_attempt,
};
use crate::{
    CitationCompactionPolicy, CompactionError, CompactionOutcome,
    compaction::{ArchiveOnlyCompactionInput, CompactionPreparation},
    events::{ActiveStepPermit, RuntimeJournalEventBatch, RuntimeRateUpdateSender},
    step::StepInput,
};
use merry_core::{ErrorInfo, ToolSpec};
use merry_llm::{GenerationConfig, ModelName, ReasoningEffort};
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

/// Everything the compaction phase needs from the step that triggered it.
pub(in crate::runtime) struct CompactionPhase<'a> {
    /// Compaction policy for this step.
    pub(crate) policy: CitationCompactionPolicy,
    /// Reasoning level compaction requests use, resolved from the runtime config.
    pub(crate) reasoning_effort: Option<&'a ReasoningEffort>,
    /// Compaction prompt configuration, resolved from the runtime config.
    pub(crate) prompt: &'a crate::CompactionPrompt,
    /// Resolved budget of the request that crossed the compaction threshold.
    pub(crate) request_budget: &'a RequestContextBudget,
    /// Current step input.
    pub(crate) input: &'a StepInput,
    /// Compiled request inputs of the current step.
    pub(crate) request_inputs: &'a StepRequestInputs,
    /// Tool specs of the current step.
    pub(crate) tool_specs: Vec<ToolSpec>,
    /// Generation controls of the current step.
    pub(crate) generation_config: GenerationConfig,
    /// Primary model, used to estimate the replacement request.
    pub(crate) primary_model: &'a ModelName,
    pub(crate) request: &'a merry_llm::ModelRequest,
}

/// Result of the automatic compaction phase for one step.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(in crate::runtime) enum CompactionPhaseOutcome {
    /// The step continues, carrying the checkpoint replacement to report when there is one.
    Continue {
        /// Installed replacement, when compaction replaced the checkpoint.
        replacement: Option<CompactionOutcome>,
        /// Destination dynamic-body target for this compaction pass.
        target_dynamic_body_tokens: u64,
    },
    /// The phase already emitted the step's terminal event; the caller returns.
    Aborted,
}

/// Reduces context for one step that crossed the compaction threshold.
pub(in crate::runtime) async fn reduce_context_over_threshold(
    inner: &Arc<RuntimeInner>,
    sender: &mpsc::Sender<RuntimeJournalEventBatch>,
    rate_sender: &RuntimeRateUpdateSender,
    token: &CancellationToken,
    active_permit: &ActiveStepPermit,
    parts: CompactionPhase<'_>,
) -> CompactionPhaseOutcome {
    let CompactionPhase {
        policy,
        reasoning_effort,
        prompt,
        request_budget,
        input,
        request_inputs,
        tool_specs,
        generation_config,
        primary_model,
        request,
    } = parts;

    let fixed_dynamic_body_tokens = match estimate_compaction_fixed_dynamic_tokens(
        input,
        primary_model,
        request_inputs,
        tool_specs,
        generation_config,
        &inner.models.prompt_profile,
        inner.models.progress_commentary,
    ) {
        Ok(tokens) => tokens,
        Err(error) => {
            return abort_with_diagnostic(
                inner,
                sender,
                token,
                step_request_compile_diagnostic(&error),
            )
            .await;
        }
    };
    let history_ids = inner.session.lock().await.provider_transcript_history_ids();
    let source = match crate::compaction::CompactionRequestSource::new(
        request.clone(),
        &history_ids,
        input.user_messages_for_request().len(),
    ) {
        Ok(source) => source,
        Err(error) => {
            return abort_with_error(
                inner,
                sender,
                token,
                crate::RuntimeError::CompactionModelRequest {
                    message: error.to_string(),
                },
            )
            .await;
        }
    };
    let compaction_budget = match CompactionRequestBudget::new(
        source,
        policy,
        request_budget,
        fixed_dynamic_body_tokens,
    ) {
        Ok(budget) => budget.with_prompt(prompt.clone()),
        Err(error) => return abort_with_error(inner, sender, token, error.into()).await,
    };
    let preparation = compaction_preparation_for_budget(inner, &compaction_budget).await;
    let preparation = match preparation {
        Ok(Some(preparation)) => preparation,
        Ok(None)
            if request_budget.dynamic_body_estimated_tokens
                < compaction_budget.window_budget.max_dynamic_body_tokens() =>
        {
            return CompactionPhaseOutcome::Continue {
                replacement: None,
                target_dynamic_body_tokens: compaction_budget.target_dynamic_body_tokens(),
            };
        }
        Ok(None) => {
            return abort_with_diagnostic(
                inner,
                sender,
                token,
                diagnostic_from_text(
                    "auto_compaction",
                    CompactionError::NoCompressibleWindow.to_string(),
                ),
            )
            .await;
        }
        Err(error) => {
            return abort_with_error(inner, sender, token, error).await;
        }
    };

    match preparation {
        CompactionPreparation::ReplaceCheckpoint(compaction_input) => {
            let attempt = match plan_compaction_attempt(
                inner,
                CompactionPreparation::ReplaceCheckpoint(compaction_input),
                &compaction_budget,
                reasoning_effort,
                token,
            )
            .await
            {
                Ok(attempt) => attempt,
                Err(error) => return abort_with_error(inner, sender, token, error).await,
            };
            match attempt {
                CompactionAttempt::ArchiveOnly { input, reason } => {
                    log_archive_only(inner, reason);
                    if !install_archive_only_reduction(inner, sender, input, token, active_permit)
                        .await
                    {
                        return CompactionPhaseOutcome::Aborted;
                    }
                    CompactionPhaseOutcome::Continue {
                        replacement: None,
                        target_dynamic_body_tokens: compaction_budget.target_dynamic_body_tokens(),
                    }
                }
                CompactionAttempt::Generate(plan) => {
                    if !send_compaction_started_event(inner, sender, rate_sender, token).await {
                        return CompactionPhaseOutcome::Aborted;
                    }
                    match generate_and_install_compaction(
                        inner,
                        plan,
                        &compaction_budget,
                        reasoning_effort,
                        token.clone(),
                        active_permit,
                    )
                    .await
                    {
                        Ok(replacement) => CompactionPhaseOutcome::Continue {
                            replacement: Some(replacement),
                            target_dynamic_body_tokens: compaction_budget
                                .target_dynamic_body_tokens(),
                        },
                        Err(error) => abort_with_error(inner, sender, token, error).await,
                    }
                }
            }
        }
        CompactionPreparation::ArchiveToolResults(archive_input) => {
            if !install_archive_only_reduction(inner, sender, archive_input, token, active_permit)
                .await
            {
                return CompactionPhaseOutcome::Aborted;
            }
            CompactionPhaseOutcome::Continue {
                replacement: None,
                target_dynamic_body_tokens: compaction_budget.target_dynamic_body_tokens(),
            }
        }
    }
}

/// Installs one archive-only reduction and reports whether the step may continue.
///
/// Returns `false` when this call already emitted the terminal event for the
/// step, which happens on cancellation or install failure.
async fn install_archive_only_reduction(
    inner: &Arc<RuntimeInner>,
    sender: &mpsc::Sender<RuntimeJournalEventBatch>,
    archive_input: ArchiveOnlyCompactionInput,
    token: &CancellationToken,
    active_permit: &ActiveStepPermit,
) -> bool {
    if token.is_cancelled() {
        let _ = abort_cancelled(inner, sender).await;
        return false;
    }
    if let Err(error) = install_archive_only_compaction_transactionally(
        Arc::clone(inner),
        archive_input,
        token.clone(),
        active_permit.clone(),
    )
    .await
    {
        if token.is_cancelled() {
            let _ = abort_cancelled(inner, sender).await;
            return false;
        }
        let _ = abort_with_diagnostic(
            inner,
            sender,
            token,
            diagnostic_from_text("auto_compaction", error.to_string()),
        )
        .await;
        return false;
    }
    tracing::debug!(
        event = "runtime.compaction.archive_only",
        session_id = inner.session_id.as_str(),
        "archived retained tool results without replacing the checkpoint"
    );
    true
}

/// Records why the runtime kept every turn raw.
fn log_archive_only(inner: &RuntimeInner, reason: ArchiveOnlyReason) {
    if let Some(error) = reason.budget_failure() {
        tracing::debug!(
            event = "runtime.compaction.archive_only_budget",
            session_id = inner.session_id.as_str(),
            error = runtime_error_message(&error),
            "compaction window cannot host a checkpoint replacement; archiving tool results instead"
        );
    }
}

/// Ends the step with the failure a compaction error produced.
async fn abort_with_error(
    inner: &Arc<RuntimeInner>,
    sender: &mpsc::Sender<RuntimeJournalEventBatch>,
    token: &CancellationToken,
    error: crate::RuntimeError,
) -> CompactionPhaseOutcome {
    if token.is_cancelled() {
        return abort_cancelled(inner, sender).await;
    }
    abort_with_diagnostic(
        inner,
        sender,
        token,
        diagnostic_from_text("auto_compaction", runtime_error_message(&error)),
    )
    .await
}

/// Ends the step with one diagnostic.
async fn abort_with_diagnostic(
    inner: &Arc<RuntimeInner>,
    sender: &mpsc::Sender<RuntimeJournalEventBatch>,
    token: &CancellationToken,
    diagnostic: ErrorInfo,
) -> CompactionPhaseOutcome {
    clear_current_activated_memories(inner).await;
    trace_provider_step_failed(&diagnostic);
    let _ = send_failed_event(inner, sender, token, diagnostic).await;
    CompactionPhaseOutcome::Aborted
}

/// Ends the step as cancelled.
async fn abort_cancelled(
    inner: &Arc<RuntimeInner>,
    sender: &mpsc::Sender<RuntimeJournalEventBatch>,
) -> CompactionPhaseOutcome {
    clear_current_activated_memories(inner).await;
    trace_provider_step_cancelled();
    let _ = send_cancelled_event(inner, sender).await;
    CompactionPhaseOutcome::Aborted
}