magi-code 0.96.1

Repository-aware CLI coding agent for terminal work
Documentation
use std::cmp::Ordering;

pub(crate) const MAX_SERVICE_TIER_BYTES: usize = 64;

/// Accept only small, opaque provider identifiers; metadata is untrusted and is never echoed raw.
pub(crate) fn parse_returned_service_tier(value: &serde_json::Value) -> Option<String> {
    let value = value.as_str()?.trim();
    if value.is_empty()
        || value.len() > MAX_SERVICE_TIER_BYTES
        || !value
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
    {
        return None;
    }
    let lower = value.to_ascii_lowercase();
    if lower.starts_with("sk-")
        || lower.starts_with("eyj")
        || ["token", "secret", "bearer", "api_key", "authorization"]
            .iter()
            .any(|marker| lower.contains(marker))
    {
        return None;
    }
    Some(value.to_string())
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub(crate) enum FastOutcome {
    #[default]
    NotRequested,
    Confirmed,
    Different(String),
    Unconfirmed,
}

pub(crate) fn fast_outcome(requested: Option<&str>, returned: Option<&str>) -> FastOutcome {
    let Some(requested) = requested else {
        return FastOutcome::NotRequested;
    };
    match returned {
        Some(returned) if returned == requested => FastOutcome::Confirmed,
        Some(returned) => FastOutcome::Different(returned.to_string()),
        None => FastOutcome::Unconfirmed,
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct FastObservation {
    pub(crate) requested_service_tier: String,
    pub(crate) outcome: FastOutcome,
    pub(crate) request_sequence: u64,
    pub(crate) run_order: Option<u64>,
}

pub(crate) fn fast_requested_service_tier_matches_request(
    requested_service_tier: &str,
    request: &FastRequestState,
) -> bool {
    request
        .service_tier()
        .is_some_and(|configured_service_tier| {
            !configured_service_tier.trim().is_empty()
                && requested_service_tier == configured_service_tier
        })
}

pub(crate) fn fast_observation_matches_request(
    observation: &FastObservation,
    request: &FastRequestState,
) -> bool {
    fast_requested_service_tier_matches_request(&observation.requested_service_tier, request)
}

pub(crate) fn display_service_tier(value: &str) -> String {
    let value = value.trim();
    if value.is_empty()
        || value.len() > MAX_SERVICE_TIER_BYTES
        || !value
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
    {
        return "<invalid>".to_string();
    }
    let lower = value.to_ascii_lowercase();
    if lower.starts_with("sk-")
        || lower.starts_with("eyj")
        || ["token", "secret", "bearer", "api_key", "authorization"]
            .iter()
            .any(|marker| lower.contains(marker))
    {
        return "<redacted>".to_string();
    }
    value.to_string()
}

/// Return the stable key used for session-local Fast observations.
pub(crate) fn canonical_fast_observation_key(provider_id: &str, model: &str) -> (String, String) {
    let provider_id = provider_id.trim();
    let model = model.trim();
    (
        if provider_id.is_empty() {
            "provider".to_string()
        } else {
            provider_id.to_string()
        },
        if model.is_empty() {
            "model".to_string()
        } else {
            model.to_string()
        },
    )
}

pub(crate) type FastWarningKey = (String, String, String, FastOutcome);

pub(crate) fn canonical_fast_warning_key(
    provider_id: &str,
    model: &str,
    observation: &FastObservation,
) -> FastWarningKey {
    let (provider_id, model) = canonical_fast_observation_key(provider_id, model);
    let outcome = match &observation.outcome {
        FastOutcome::Different(service_tier) => {
            FastOutcome::Different(display_service_tier(service_tier))
        }
        outcome => outcome.clone(),
    };
    (
        provider_id,
        model,
        display_service_tier(&observation.requested_service_tier),
        outcome,
    )
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct FastObservationOrder {
    pub(crate) run_order: Option<u64>,
    pub(crate) accept_sequence: u64,
}

/// Compare a candidate observation with the observation already stored for a key.
///
/// Consumers assign `accept_sequence` when they receive an observation. A known
/// run order always outranks an unknown one, and a known run order is compared
/// before the consumer-owned arrival sequence. This prevents a late observation
/// from an older run from replacing a newer run while preserving arrival order
/// for observations from the same run.
pub(crate) fn compare_fast_observation_order(
    current: Option<FastObservationOrder>,
    candidate: FastObservationOrder,
) -> Ordering {
    let Some(current) = current else {
        return Ordering::Greater;
    };
    (candidate.run_order, candidate.accept_sequence)
        .cmp(&(current.run_order, current.accept_sequence))
}

use crate::config::Settings;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FastWorkload {
    Primary,
    Subagent,
    Compaction,
    SessionTitle,
}

impl FastWorkload {
    pub(crate) const fn allows_fast(self) -> bool {
        !matches!(self, Self::SessionTitle)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum FastCapability {
    Unsupported,
    ServiceTier(String),
}

impl FastCapability {
    pub(crate) fn service_tier(service_tier: impl Into<String>) -> Self {
        Self::ServiceTier(service_tier.into())
    }
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) enum FastRequestState {
    #[default]
    Standard,
    Fast {
        service_tier: String,
    },
}

impl FastRequestState {
    pub(crate) fn service_tier(&self) -> Option<&str> {
        match self {
            Self::Standard => None,
            Self::Fast { service_tier } => Some(service_tier),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) enum FastProjection {
    #[default]
    Off,
    Unavailable,
    WillRequest {
        service_tier: String,
    },
    Confirmed {
        service_tier: String,
    },
    Different {
        requested_service_tier: String,
        returned_service_tier: String,
    },
    Unconfirmed {
        service_tier: String,
    },
}

impl FastProjection {
    pub(crate) fn status_message(&self, provider: &str, model: &str) -> String {
        match self {
            Self::Off => "Fast mode off. Magi-code will not request Fast processing.".to_string(),
            Self::Unavailable => {
                format!("Fast remains on, but {provider}/{model} has no configured Fast support.")
            }
            Self::WillRequest { .. } => format!(
                "Fast mode on. {provider}/{model} will request Fast processing.\nEligible requests may have higher provider pricing."
            ),
            Self::Confirmed { service_tier } => format!(
                "Fast was confirmed for {provider}/{model} (tier \"{}\").",
                display_service_tier(service_tier)
            ),
            Self::Different {
                returned_service_tier,
                ..
            } => format!(
                "Fast requested, but {provider}/{model} reported tier \"{}\".",
                display_service_tier(returned_service_tier)
            ),
            Self::Unconfirmed { .. } => format!(
                "Fast was requested, but {provider}/{model} did not report its processing tier."
            ),
        }
    }
}

pub(crate) fn resolve_fast_projection(
    enabled: bool,
    request: &FastRequestState,
    observation: Option<&FastObservation>,
) -> FastProjection {
    if !enabled {
        return FastProjection::Off;
    }
    let Some(configured_service_tier) = request
        .service_tier()
        .filter(|service_tier| !service_tier.trim().is_empty())
    else {
        return FastProjection::Unavailable;
    };
    let observation =
        observation.filter(|observation| fast_observation_matches_request(observation, request));
    let requested_service_tier = configured_service_tier.to_string();
    match observation.map(|observation| &observation.outcome) {
        Some(FastOutcome::Confirmed) => FastProjection::Confirmed {
            service_tier: requested_service_tier,
        },
        Some(FastOutcome::Different(returned_service_tier)) => FastProjection::Different {
            requested_service_tier,
            returned_service_tier: display_service_tier(returned_service_tier),
        },
        Some(FastOutcome::Unconfirmed) => FastProjection::Unconfirmed {
            service_tier: requested_service_tier,
        },
        Some(FastOutcome::NotRequested) | None => FastProjection::WillRequest {
            service_tier: requested_service_tier,
        },
    }
}

pub(crate) fn resolve_fast_capability(
    settings: &Settings,
    provider_id: &str,
    model: &str,
    paths: &crate::config::McPaths,
    custom: Option<&crate::config::CustomProviderConfig>,
    workload: FastWorkload,
) -> FastRequestState {
    let capability = if provider_id == crate::providers::OPENAI_CODEX_PROVIDER {
        crate::model_catalog::cached_codex_fast_service_tier(paths, model)
            .map(FastCapability::service_tier)
            .unwrap_or(FastCapability::Unsupported)
    } else if let Some(custom) = custom {
        custom
            .fast_mode
            .as_ref()
            .and_then(|fast| {
                let exact = fast.models.iter().any(|candidate| candidate == model);
                let wildcard = fast.models.len() == 1 && fast.models[0] == "*";
                (exact || wildcard).then(|| FastCapability::service_tier(fast.service_tier.clone()))
            })
            .unwrap_or(FastCapability::Unsupported)
    } else {
        FastCapability::Unsupported
    };
    resolve_fast_request(settings, workload, capability)
}
pub(crate) fn resolve_fast_request(
    settings: &Settings,
    workload: FastWorkload,
    capability: FastCapability,
) -> FastRequestState {
    if !settings.fast.enabled || !workload.allows_fast() {
        return FastRequestState::Standard;
    }
    match capability {
        FastCapability::Unsupported => FastRequestState::Standard,
        FastCapability::ServiceTier(service_tier) if service_tier.trim().is_empty() => {
            FastRequestState::Standard
        }
        FastCapability::ServiceTier(service_tier) => FastRequestState::Fast { service_tier },
    }
}

pub(crate) fn fast_status_message(
    enabled: bool,
    provider: &str,
    model: &str,
    capability: &FastRequestState,
    observation: Option<&FastObservation>,
) -> String {
    resolve_fast_projection(enabled, capability, observation).status_message(provider, model)
}

pub(crate) fn fast_observation_warning(
    provider: &str,
    model: &str,
    observation: &FastObservation,
) -> Option<String> {
    if !matches!(
        observation.outcome,
        FastOutcome::Different(_) | FastOutcome::Unconfirmed
    ) {
        return None;
    }
    let capability = FastRequestState::Fast {
        service_tier: observation.requested_service_tier.clone(),
    };
    Some(fast_status_message(
        true,
        provider,
        model,
        &capability,
        Some(observation),
    ))
}