use super::{CitationCompactionInput, format_compact_summary, is_degenerate_summary};
use crate::{
RuntimeError,
model_completion::{ModelCompletionError, complete_single_text},
token_estimate::estimate_model_input_tokens,
};
use merry_core::{ProviderName, SessionId};
use merry_llm::{
FinishReason, ModelCapabilities, ModelProvider, ModelRequest, ModelStreamContext,
ProviderErrorKind,
};
use std::{sync::Arc, time::Duration};
use tokio_util::sync::CancellationToken;
const MAX_COMPACTION_PROVIDER_ATTEMPTS: usize = 3;
const COMPACTION_RETRY_DELAY: Duration = Duration::from_millis(100);
pub(crate) fn compaction_model_window(
capabilities: &ModelCapabilities,
primary_window_tokens: u64,
session_id: &SessionId,
provider_name: &ProviderName,
) -> Result<u64, RuntimeError> {
match capabilities.max_input_tokens() {
Some(compactor_window_tokens) if compactor_window_tokens < primary_window_tokens => {
Err(RuntimeError::CompactionModelWindowTooSmall {
primary_window_tokens,
compactor_window_tokens,
})
}
Some(compactor_window_tokens) => Ok(compactor_window_tokens),
None => {
tracing::debug!(
event = "runtime.compaction.model_window_assumed",
session_id = session_id.as_str(),
provider = provider_name.as_str(),
primary_window_tokens,
"compaction model input capability is absent; assuming the primary context window"
);
Ok(primary_window_tokens)
}
}
}
pub(crate) fn compaction_request_required_tokens(request: &ModelRequest) -> (u64, u64) {
(
estimate_model_input_tokens(request.input()).saturating_add(
crate::token_estimate::estimate_request_contract_tokens(request),
),
request.generation().max_output_tokens().unwrap_or(0),
)
}
pub(crate) fn validate_compaction_model_window(
request: &ModelRequest,
compactor_window_tokens: u64,
) -> Result<(), RuntimeError> {
let (estimated_input_tokens, max_output_tokens) = compaction_request_required_tokens(request);
let required_tokens = estimated_input_tokens
.checked_add(max_output_tokens)
.ok_or(RuntimeError::CompactionModelRequestTooLarge {
estimated_input_tokens,
max_output_tokens,
compactor_window_tokens,
})?;
if required_tokens > compactor_window_tokens {
return Err(RuntimeError::CompactionModelRequestTooLarge {
estimated_input_tokens,
max_output_tokens,
compactor_window_tokens,
});
}
Ok(())
}
pub(crate) async fn generate_validated_compaction_candidate(
provider: Arc<dyn ModelProvider>,
request: ModelRequest,
stream_context: ModelStreamContext,
input: &CitationCompactionInput,
compactor_window_tokens: u64,
session_id: &SessionId,
token: &CancellationToken,
) -> Result<String, RuntimeError> {
for attempt in 1..=MAX_COMPACTION_PROVIDER_ATTEMPTS {
if token.is_cancelled() {
return Err(cancelled_setup_error("before compaction model setup"));
}
tracing::debug!(
event = "runtime.compaction.attempt_started",
session_id = session_id.as_str(),
attempt,
max_attempts = MAX_COMPACTION_PROVIDER_ATTEMPTS,
soft_target_tokens = input.resolved_budget().target_output_tokens(),
output_limit_tokens = input.resolved_budget().output_token_limit(),
retained_turn_count = input.window_plan().retained_turn_ids().len(),
covered_turn_count = input.window_plan().covered_turn_ids().len(),
archived_tool_count = input.window_plan().archived_tool_call_ids().len(),
model = request.model().as_str(),
message_count = request.messages().len(),
estimated_input_tokens = estimate_model_input_tokens(request.input()),
"starting compaction model attempt"
);
let candidate = match run_compaction_attempt(
provider.as_ref(),
request.clone(),
stream_context.clone(),
token,
)
.await
{
Ok(candidate) => candidate,
Err(failure) => {
trace_attempt_failure(attempt, &failure);
if failure.cancelled
|| !failure.retryable
|| attempt == MAX_COMPACTION_PROVIDER_ATTEMPTS
{
return Err(failure.error);
}
trace_retry(attempt, &failure.error);
wait_before_compaction_retry(token).await?;
continue;
}
};
let cleaned = format_compact_summary(&candidate);
let accepted = !is_degenerate_summary(&candidate);
tracing::debug!(
event = "runtime.compaction.summary_evaluated",
session_id = session_id.as_str(),
attempt,
accepted,
summary_chars = cleaned.chars().count(),
compactor_window_tokens,
"compaction summary cleaned without a schema check"
);
if accepted {
return Ok(cleaned);
}
let error = RuntimeError::Compaction {
source: crate::CompactionError::InvalidModelResponseShape {
reason: "compaction summary was empty or too short to continue the session",
},
};
if attempt == MAX_COMPACTION_PROVIDER_ATTEMPTS {
return Err(error);
}
trace_retry(attempt, &error);
wait_before_compaction_retry(token).await?;
}
unreachable!("compaction attempt loop always returns on its final attempt")
}
async fn run_compaction_attempt(
provider: &dyn ModelProvider,
request: ModelRequest,
stream_context: ModelStreamContext,
token: &CancellationToken,
) -> Result<String, AttemptFailure> {
complete_single_text(provider, request, stream_context, token)
.await
.map_err(|error| completion_failure(&error))
}
fn completion_failure(error: &ModelCompletionError) -> AttemptFailure {
match error {
ModelCompletionError::Cancelled => {
AttemptFailure::cancelled(cancelled_stream_error("while running the compaction model"))
}
ModelCompletionError::Setup { kind, message } => AttemptFailure {
error: RuntimeError::CompactionModelSetup {
message: message.clone(),
},
cancelled: false,
retryable: is_retryable_provider_error(*kind),
},
ModelCompletionError::Stream { kind, message } => AttemptFailure {
error: RuntimeError::CompactionModelStream {
message: message.clone(),
},
cancelled: false,
retryable: is_retryable_provider_error(*kind),
},
ModelCompletionError::ToolCallRequested => {
AttemptFailure::retryable(RuntimeError::CompactionModelStream {
message: "compaction model requested a tool call".to_owned(),
})
}
ModelCompletionError::NonStopFinish {
finish_reason,
finish_detail,
} => {
let detail = finish_detail
.map(|detail| format!(" ({})", detail.as_str()))
.unwrap_or_default();
let message = format!("compaction model finished with {finish_reason:?}{detail}");
if is_truncated_finish(*finish_reason) {
AttemptFailure {
error: RuntimeError::CompactionModelTruncated { message },
cancelled: false,
retryable: false,
}
} else {
AttemptFailure::retryable(RuntimeError::CompactionModelStream { message })
}
}
ModelCompletionError::NotSingleText => {
AttemptFailure::retryable(RuntimeError::CompactionModelStream {
message: "compaction model must return exactly one text output".to_owned(),
})
}
ModelCompletionError::EndedBeforeCompletion => {
AttemptFailure::retryable(RuntimeError::CompactionModelStream {
message: "compaction model stream ended before completion".to_owned(),
})
}
}
}
fn is_retryable_provider_error(kind: ProviderErrorKind) -> bool {
!matches!(
kind,
ProviderErrorKind::InvalidRequest
| ProviderErrorKind::InvalidToolCall
| ProviderErrorKind::Cancelled
| ProviderErrorKind::Authentication
)
}
fn cancelled_setup_error(stage: &'static str) -> RuntimeError {
RuntimeError::CompactionModelSetup {
message: format!("compaction cancelled {stage}"),
}
}
fn is_truncated_finish(finish_reason: FinishReason) -> bool {
matches!(finish_reason, FinishReason::Length)
}
fn cancelled_stream_error(stage: &'static str) -> RuntimeError {
RuntimeError::CompactionModelStream {
message: format!("compaction cancelled {stage}"),
}
}
async fn wait_before_compaction_retry(token: &CancellationToken) -> Result<(), RuntimeError> {
tokio::select! {
biased;
() = token.cancelled() => Err(cancelled_setup_error("before compaction retry")),
() = tokio::time::sleep(COMPACTION_RETRY_DELAY) => Ok(()),
}
}
fn trace_retry(attempt: usize, error: &RuntimeError) {
tracing::debug!(
event = "runtime.compaction.retry",
attempt,
next_attempt = attempt + 1,
max_attempts = MAX_COMPACTION_PROVIDER_ATTEMPTS,
error = %error,
"retrying failed compaction model attempt"
);
}
fn trace_attempt_failure(attempt: usize, failure: &AttemptFailure) {
tracing::debug!(
event = "runtime.compaction.attempt_failed",
attempt,
max_attempts = MAX_COMPACTION_PROVIDER_ATTEMPTS,
cancelled = failure.cancelled,
retryable = failure.retryable,
error = %failure.error,
"compaction model attempt failed"
);
}
struct AttemptFailure {
error: RuntimeError,
cancelled: bool,
retryable: bool,
}
impl AttemptFailure {
fn retryable(error: RuntimeError) -> Self {
Self {
error,
cancelled: false,
retryable: true,
}
}
fn cancelled(error: RuntimeError) -> Self {
Self {
error,
cancelled: true,
retryable: false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use merry_llm::{FinishDetail, FinishReason};
#[test]
fn non_stop_finish_message_carries_the_provider_detail() {
let failure = completion_failure(&ModelCompletionError::NonStopFinish {
finish_reason: FinishReason::Length,
finish_detail: Some(FinishDetail::MaxOutputTokens),
});
assert!(
!failure.retryable,
"a truncated checkpoint must not be retried with the identical request"
);
assert!(
failure
.error
.to_string()
.contains("compaction model finished with Length (max_output_tokens)"),
"unexpected message: {}",
failure.error
);
assert!(matches!(
failure.error,
RuntimeError::CompactionModelTruncated { .. }
));
}
#[test]
fn non_stop_finish_without_detail_keeps_the_plain_reason() {
let failure = completion_failure(&ModelCompletionError::NonStopFinish {
finish_reason: FinishReason::Blocked,
finish_detail: None,
});
assert!(
failure
.error
.to_string()
.contains("compaction model finished with Blocked"),
"unexpected message: {}",
failure.error
);
}
}