use crate::core::config::{Config, RecoveryHints};
pub(crate) const COMPACT_VIEW_HEADER: &str =
"[lean-ctx: compact view — nothing lost, full source on request]";
#[must_use]
pub(crate) fn tier() -> RecoveryHints {
if let Some(t) = RecoveryHints::from_env() {
return t;
}
match crate::core::profiles::active_profile()
.output_hints
.compressed_hint
{
Some(true) => RecoveryHints::Full,
Some(false) => RecoveryHints::Off,
None => Config::load().recovery_hints,
}
}
#[must_use]
pub fn read_footer(file_path: &str) -> Option<String> {
match tier() {
RecoveryHints::Off => None,
RecoveryHints::Minimal => Some(format!(
"[lean-ctx] full source: read \"{file_path}\" directly (no MCP) · exact part: ctx_read same path, symbol=\"Name\" or mode=\"lines:N-M\""
)),
RecoveryHints::Full => Some(format!(
"{COMPACT_VIEW_HEADER}\n full: read \"{file_path}\" directly (no MCP) · ctx_read(\"{file_path}\", mode=\"full\") · exact bytes: ctx_read(\"{file_path}\", raw=true)"
)),
}
}
#[must_use]
pub fn handle_clause(id: &str, on_disk_path: Option<&str>) -> String {
if on_disk_path.is_some_and(|path| {
crate::core::protected_store::enforce_protected_store_path(std::path::Path::new(path))
.is_err()
}) {
return format!("full: ctx_expand(id=\"{id}\")");
}
match (tier(), on_disk_path) {
(RecoveryHints::Off, Some(p)) => format!("full: {p}"),
(RecoveryHints::Off, None) => format!("full: ctx_expand(id=\"{id}\")"),
(_, Some(p)) => format!("full: read {p} directly (no MCP) · or ctx_expand(id=\"{id}\")"),
(_, None) => format!("full: ctx_expand(id=\"{id}\") · or read the shown path"),
}
}
#[must_use]
pub(crate) fn original_pointer(id: &str) -> String {
format!("[Original available via ctx_expand id={id}]")
}
#[must_use]
pub(crate) fn archive_original(tool: &str, label: &str, original: &str) -> Option<String> {
if Config::minimal_escape_hatch() {
return None;
}
let to_store = crate::core::redaction::redact_text_if_enabled(original);
crate::core::archive::store(tool, label, &to_store, None).map(|id| original_pointer(&id))
}
#[must_use]
pub fn with_original_pointer(tool: &str, label: &str, original: &str, truncated: String) -> String {
match archive_original(tool, label, original) {
Some(pointer) => format!("{truncated}\n{pointer}"),
None => truncated,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn original_pointer_is_content_addressed_and_resolves() {
let _data = crate::core::data_dir::isolated_data_dir();
let _community = crate::core::policy::runtime::TestPolicyOverride::set(None);
let original = "line\n".repeat(400);
let first = archive_original("ctx_test", "label", &original).expect("archived");
let second = archive_original("ctx_test", "label", &original).expect("archived");
assert_eq!(first, second, "byte-stable pointer");
let id = first
.strip_prefix("[Original available via ctx_expand id=")
.and_then(|rest| rest.strip_suffix(']'))
.expect("pointer grammar");
assert_eq!(
crate::core::archive::retrieve(id).as_deref(),
Some(original.as_str())
);
let other = archive_original("ctx_test", "label", "different").expect("archived");
assert_ne!(first, other);
}
#[cfg(target_os = "macos")]
#[test]
fn protected_runtime_recovery_does_not_recommend_raw_file_access() {
let isolated = crate::core::data_dir::isolated_data_dir();
let artifact = isolated.path().join("tee/output.log");
let path = artifact.to_str().unwrap();
let _pin = crate::core::protected_store::pin_synthetic_session(isolated.path()).unwrap();
let hint = handle_clause("fixture-archive", Some(path));
assert!(hint.contains("ctx_expand"));
assert!(!hint.contains("directly"));
assert!(!hint.contains(path));
}
use crate::core::data_dir::test_env_lock;
#[test]
fn env_override_pins_tier() {
let _lock = test_env_lock();
crate::test_env::set_var("LEAN_CTX_RECOVERY_HINTS", "off");
assert_eq!(tier(), RecoveryHints::Off);
crate::test_env::set_var("LEAN_CTX_RECOVERY_HINTS", "full");
assert_eq!(tier(), RecoveryHints::Full);
crate::test_env::remove_var("LEAN_CTX_RECOVERY_HINTS");
}
#[test]
fn read_footer_leads_with_native_path_and_respects_off() {
let _lock = test_env_lock();
crate::test_env::set_var("LEAN_CTX_RECOVERY_HINTS", "off");
assert!(
read_footer("src/x.rs").is_none(),
"off suppresses the footer"
);
crate::test_env::set_var("LEAN_CTX_RECOVERY_HINTS", "minimal");
let minimal = read_footer("src/x.rs").expect("minimal emits a footer");
assert!(minimal.lines().count() == 1, "minimal is a single line");
let native = minimal.find("read \"src/x.rs\" directly").unwrap();
let mcp = minimal.find("ctx_read").unwrap();
assert!(minimal.contains("symbol=") && minimal.contains("lines:N-M"));
assert!(native < mcp, "native path must precede the MCP route");
crate::test_env::set_var("LEAN_CTX_RECOVERY_HINTS", "full");
let full = read_footer("src/x.rs").expect("full emits a footer");
assert!(full.contains("raw=true") && !full.contains("ctx_retrieve"));
assert!(
full.find("read \"src/x.rs\" directly").unwrap() < full.find("raw=true").unwrap(),
"full ladder still leads with the native path"
);
crate::test_env::remove_var("LEAN_CTX_RECOVERY_HINTS");
}
#[test]
fn footer_and_clause_are_byte_stable() {
let _lock = test_env_lock();
crate::test_env::set_var("LEAN_CTX_RECOVERY_HINTS", "minimal");
assert_eq!(read_footer("src/a.rs"), read_footer("src/a.rs"));
assert_eq!(
handle_clause("id1", Some("/tmp/t.log")),
handle_clause("id1", Some("/tmp/t.log"))
);
crate::test_env::remove_var("LEAN_CTX_RECOVERY_HINTS");
}
#[test]
fn handle_clause_is_non_mcp_first() {
let _lock = test_env_lock();
crate::test_env::set_var("LEAN_CTX_RECOVERY_HINTS", "minimal");
let with_path = handle_clause("abc123", Some("/tmp/tee/run.log"));
assert!(
with_path.find("/tmp/tee/run.log").unwrap() < with_path.find("ctx_expand").unwrap(),
"on-disk path must precede ctx_expand"
);
let no_path = handle_clause("abc123", None);
assert!(no_path.contains("ctx_expand(id=\"abc123\")"));
crate::test_env::set_var("LEAN_CTX_RECOVERY_HINTS", "off");
assert_eq!(
handle_clause("abc123", Some("/tmp/tee/run.log")),
"full: /tmp/tee/run.log",
"off keeps the bare functional pointer, drops the coaching"
);
crate::test_env::remove_var("LEAN_CTX_RECOVERY_HINTS");
}
}