magi-code 0.102.2

Repository-aware CLI coding agent for terminal work
Documentation
//! Public control facts only; never inspect output or private compaction input.
use super::ActivityStatus;
use serde_json::{Value, json};

pub(crate) fn operation(params: &Value) -> Option<&'static str> {
    match params["action"].as_str()? {
        "compact" => Some("compact"),
        "context_usage" => Some("context_usage"),
        _ => None,
    }
}

pub(crate) fn metadata(source: &Value) -> Value {
    let mut selected = json!({"action": operation(source)});
    for key in ["percentage", "current_tokens", "max_tokens"] {
        selected[key] = source[key].as_u64().map_or(Value::Null, Value::from);
    }
    // Preserve only the shared public protection inputs, never assessment payloads.
    for key in ["bash_protection", "humanize_protection"] {
        if let Some((_, label)) = source[key]["decision"]
            .as_str()
            .and_then(super::tool_card::public_decision_label)
        {
            selected[key] = json!({"decision": label});
        }
    }
    if let Some(action @ ("allow" | "annotate" | "quarantine" | "escalate")) =
        source["prompt_injection_protection"]["action"].as_str()
    {
        selected["prompt_injection_protection"] = json!({
            "action": action,
            "enforced": source["prompt_injection_protection"]["enforced"].as_bool(),
        });
    }
    selected
}

pub(crate) fn compaction_status(
    params: &Value,
    metadata: &Value,
    status: ActivityStatus,
) -> Option<&'static str> {
    if operation(params) != Some("compact") {
        return None;
    }
    match status {
        ActivityStatus::Success if operation(metadata) == Some("compact") => Some("requested"),
        ActivityStatus::Failed => Some("request failed"),
        ActivityStatus::Canceled => Some("request cancelled"),
        _ => None,
    }
}

pub(crate) fn summary(params: &Value, metadata: &Value, status: ActivityStatus) -> String {
    match operation(params) {
        Some("compact") => compaction_status(params, metadata, status)
            .map(|status| format!("Compaction: {status}"))
            .unwrap_or_else(|| "Compaction status unavailable".into()),
        Some("context_usage") => {
            let count = |key| {
                metadata[key]
                    .as_u64()
                    .map(|n| n.to_string())
                    .unwrap_or_else(|| "unknown".into())
            };
            format!(
                "Context: {}/{} tokens; {}% used",
                count("current_tokens"),
                count("max_tokens"),
                count("percentage")
            )
        }
        _ => "Control detail unavailable".into(),
    }
}