use super::{
CrpMode, ReadMode, ReadOutput, ReadTuning, SessionCache, count_tokens, dedup_hook,
handle_with_options_inner, protocol,
};
const MAX_RELAY_CONTENT_BYTES: usize = 8192;
fn is_terminal_poll_file(path: &str) -> bool {
(path.contains("/.cursor/") || path.contains("\\.cursor\\"))
&& (path.contains("/terminals/") || path.contains("\\terminals\\"))
&& std::path::Path::new(path)
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("txt"))
}
const RELAY_ELIGIBLE_MODES: &[&str] = &["map", "signatures"];
fn relay_eligible<'a>(mode: &'a str, content: &'a str) -> (Option<&'a str>, Option<&'a str>) {
if RELAY_ELIGIBLE_MODES.contains(&mode) && content.len() <= MAX_RELAY_CONTENT_BYTES {
(Some(content), Some(mode))
} else {
(None, None)
}
}
fn relay_variant_matches(
mode: &str,
stored: &str,
crp_mode: CrpMode,
tuning: ReadTuning<'_>,
) -> bool {
RELAY_ELIGIBLE_MODES.iter().any(|candidate| {
(mode == "auto" || mode == *candidate)
&& stored
== super::compressed_cache_key(
candidate,
crp_mode,
None,
tuning.aggressiveness,
tuning.protect,
)
})
}
pub fn handle(cache: &mut SessionCache, path: &str, mode: &str, crp_mode: CrpMode) -> String {
handle_with_options(cache, path, mode, false, crp_mode, None)
}
pub fn handle_fresh(cache: &mut SessionCache, path: &str, mode: &str, crp_mode: CrpMode) -> String {
handle_with_options(cache, path, mode, true, crp_mode, None)
}
pub fn handle_with_task(
cache: &mut SessionCache,
path: &str,
mode: &str,
crp_mode: CrpMode,
task: Option<&str>,
) -> String {
handle_with_task_result(cache, path, mode, crp_mode, task).content
}
pub fn handle_with_task_result(
cache: &mut SessionCache,
path: &str,
mode: &str,
crp_mode: CrpMode,
task: Option<&str>,
) -> ReadOutput {
let mut result = handle_with_options_resolved(
cache,
path,
mode,
false,
crp_mode,
task,
ReadTuning::resolve(None, &[]),
);
result.output_tokens = count_tokens(&result.content);
result
}
pub fn handle_with_task_resolved(
cache: &mut SessionCache,
path: &str,
mode: &str,
crp_mode: CrpMode,
task: Option<&str>,
) -> ReadOutput {
handle_with_options_resolved(
cache,
path,
mode,
false,
crp_mode,
task,
ReadTuning::resolve(None, &[]),
)
}
pub fn handle_with_task_resolved_tuned(
cache: &mut SessionCache,
path: &str,
mode: &str,
crp_mode: CrpMode,
task: Option<&str>,
aggressiveness: Option<f64>,
protect: &[String],
) -> ReadOutput {
handle_with_options_resolved(
cache,
path,
mode,
false,
crp_mode,
task,
ReadTuning::resolve(aggressiveness, protect),
)
}
#[allow(clippy::too_many_arguments)]
pub fn handle_with_preread(
cache: &mut SessionCache,
path: &str,
mode: &str,
fresh: bool,
crp_mode: CrpMode,
task: Option<&str>,
aggressiveness: Option<f64>,
protect: &[String],
preread: String,
) -> ReadOutput {
handle_with_options_resolved_preread(
cache,
path,
mode,
fresh,
crp_mode,
task,
ReadTuning::resolve(aggressiveness, protect),
Some(preread),
)
}
pub fn handle_fresh_with_task(
cache: &mut SessionCache,
path: &str,
mode: &str,
crp_mode: CrpMode,
task: Option<&str>,
) -> String {
handle_fresh_with_task_result(cache, path, mode, crp_mode, task).content
}
pub fn handle_fresh_with_task_result(
cache: &mut SessionCache,
path: &str,
mode: &str,
crp_mode: CrpMode,
task: Option<&str>,
) -> ReadOutput {
handle_with_options_resolved(
cache,
path,
mode,
true,
crp_mode,
task,
ReadTuning::resolve(None, &[]),
)
}
pub fn handle_fresh_with_task_resolved(
cache: &mut SessionCache,
path: &str,
mode: &str,
crp_mode: CrpMode,
task: Option<&str>,
) -> ReadOutput {
handle_with_options_resolved(
cache,
path,
mode,
true,
crp_mode,
task,
ReadTuning::resolve(None, &[]),
)
}
pub fn handle_fresh_with_task_resolved_tuned(
cache: &mut SessionCache,
path: &str,
mode: &str,
crp_mode: CrpMode,
task: Option<&str>,
aggressiveness: Option<f64>,
protect: &[String],
) -> ReadOutput {
handle_with_options_resolved(
cache,
path,
mode,
true,
crp_mode,
task,
ReadTuning::resolve(aggressiveness, protect),
)
}
fn handle_with_options(
cache: &mut SessionCache,
path: &str,
mode: &str,
fresh: bool,
crp_mode: CrpMode,
task: Option<&str>,
) -> String {
handle_with_options_resolved(
cache,
path,
mode,
fresh,
crp_mode,
task,
ReadTuning::resolve(None, &[]),
)
.content
}
pub(crate) fn force_fresh_env() -> bool {
static FORCE_FRESH: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*FORCE_FRESH.get_or_init(|| {
std::env::var("LEAN_CTX_FORCE_FRESH").is_ok_and(|v| v == "1" || v == "true")
})
}
pub(crate) use lean_ctx::core::conversation::is_subagent_context;
#[allow(clippy::fn_params_excessive_bools)]
pub(crate) fn effective_fresh_flags(
fresh: bool,
force_fresh: bool,
subagent_context: bool,
delivery_for_subagents: bool,
) -> (bool, bool) {
let effective_fresh_for_cache = fresh || force_fresh || subagent_context;
let effective_fresh_for_delivery =
fresh || force_fresh || (subagent_context && !delivery_for_subagents);
(effective_fresh_for_cache, effective_fresh_for_delivery)
}
pub(crate) fn effective_fresh_for_delivery(fresh: bool) -> bool {
let config = lean_ctx::core::config::Config::load();
effective_fresh_flags(
fresh,
force_fresh_env(),
is_subagent_context(),
config.ocla.delivery.delivery_for_subagents,
)
.1
}
fn handle_with_options_resolved(
cache: &mut SessionCache,
path: &str,
mode: &str,
fresh: bool,
crp_mode: CrpMode,
task: Option<&str>,
tuning: ReadTuning<'_>,
) -> ReadOutput {
handle_with_options_resolved_preread(cache, path, mode, fresh, crp_mode, task, tuning, None)
}
fn handle_with_options_resolved_preread(
cache: &mut SessionCache,
path: &str,
mode: &str,
fresh: bool,
crp_mode: CrpMode,
task: Option<&str>,
tuning: ReadTuning<'_>,
preread: Option<String>,
) -> ReadOutput {
let config = lean_ctx::core::config::Config::load();
let (effective_fresh_for_cache, effective_fresh_for_delivery) = effective_fresh_flags(
fresh,
force_fresh_env(),
is_subagent_context(),
config.ocla.delivery.delivery_for_subagents,
);
let compress_protected = (mode != "raw"
&& !mode.starts_with("lines:")
&& lean_ctx::core::config::Config::load()
.proxy
.is_path_compress_protected(path))
|| super::instruction_file_forces_full(path, mode);
let delivery_metadata = config
.ocla
.delivery_enabled()
.then(|| file_blake3_prefix(path))
.flatten();
if !effective_fresh_for_delivery
&& !compress_protected
&& let Some(fp) = delivery_metadata
&& let Some(stub) = try_cross_agent_stub(path, mode, fp.hash, fp.mtime, crp_mode, tuning)
{
return stub;
}
if mode == "auto" {
let touched: Vec<String> = cache
.get_all_entries()
.iter()
.map(|(p, _)| (*p).clone())
.collect();
if lean_ctx::core::relevance_gate::should_gate(path, mode, task, &touched) {
let meta = std::fs::metadata(path);
let byte_count = meta.as_ref().map_or(0, std::fs::Metadata::len);
let line_count = preread
.as_ref()
.map_or(0, |c| bytecount::count(c.as_bytes(), b'\n'));
let stub =
lean_ctx::core::relevance_gate::irrelevant_stub(path, line_count, byte_count);
let stub_tokens = count_tokens(&stub);
return ReadOutput {
content: stub,
resolved_mode: "auto".into(),
output_tokens: stub_tokens,
is_cache_hit: false,
};
}
}
if let Ok(mut bt) = lean_ctx::core::bounce_tracker::global().lock() {
bt.next_seq();
}
let mut result = handle_with_options_inner(
cache,
path,
mode,
effective_fresh_for_cache,
crp_mode,
task,
tuning,
preread,
);
if let Some(entry) = cache.get_mut(path) {
entry.last_mode.clone_from(&result.resolved_mode);
if matches!(result.resolved_mode.as_str(), "full" | "full-compact")
&& entry.full_content_delivered
&& result.is_cache_hit
&& entry.bump_reread() >= lean_ctx::core::cache::full_degradation_threshold()
{
entry.full_content_delivered = false;
entry.reset_reread_count();
lean_ctx::core::auto_mode_resolver::count_source("full_delivery_degraded");
}
if !matches!(result.resolved_mode.as_str(), "full" | "full-compact")
|| super::dedup_hook::exceeds_turn_budget(&result.content)
{
entry.full_content_delivered = false;
entry.reset_reread_count();
}
}
if !result.is_cache_hit
&& let Some(fp) = delivery_metadata
{
record_read_delivery(
path,
fp,
&result.resolved_mode,
&result.content,
result.output_tokens,
crp_mode,
tuning,
);
}
let dedup_allowed = result
.resolved_mode
.parse::<ReadMode>()
.is_ok_and(|m| m.is_lossy_summary());
if dedup_allowed && let Some(deduped) = cache.apply_dedup(path, &result.content) {
let new_tokens = count_tokens(&deduped);
if new_tokens < result.output_tokens {
result.content = deduped;
result.output_tokens = new_tokens;
}
}
if let Some(stub) = dedup_hook::maybe_dedup(path, &result.content, mode, fresh) {
let stub_tokens = count_tokens(&stub);
if stub_tokens < result.output_tokens {
result.content = stub;
result.output_tokens = stub_tokens;
result.is_cache_hit = true;
}
}
lean_ctx::core::context_kernel::adaptive_hook::update_from_bounce_tracker();
if let Ok(mut bt) = lean_ctx::core::bounce_tracker::global().lock() {
let original_tokens = cache.get(path).map_or(0, |e| e.original_tokens);
bt.record_read(
path,
&result.resolved_mode,
result.output_tokens,
original_tokens,
);
}
lean_ctx::core::context_gc::maybe_gc(cache);
result
}
pub fn try_stub_hit_readonly(cache: &SessionCache, path: &str) -> Option<ReadOutput> {
let current_conversation = lean_ctx::core::conversation::current_conversation_id_fresh();
try_stub_hit_readonly_scoped(cache, path, current_conversation.as_deref())
}
pub(crate) fn try_stub_hit_readonly_scoped(
cache: &SessionCache,
path: &str,
current_conversation: Option<&str>,
) -> Option<ReadOutput> {
if !stub_policy_allows() {
return None;
}
if let Some(file_ref) = cache.get_file_ref_readonly(path) {
let (cached_mtime, cached_hash, line_count, delivered_conv) = {
let entry = cache.get(path)?;
(
entry.stored_mtime,
entry.hash.clone(),
entry.line_count,
entry.delivered_conversation.clone(),
)
};
if lean_ctx::core::cache::is_cache_entry_stale_verified(path, cached_mtime, &cached_hash)
|| !cache.is_full_delivered(path)
{
return None;
}
let is_terminal_poll = is_terminal_poll_file(path);
if !is_terminal_poll
&& !lean_ctx::core::conversation::conversation_allows_stub(
current_conversation,
delivered_conv.as_deref(),
)
{
return None;
}
let original_tokens = cache.record_cache_hit(path)?.original_tokens;
lean_ctx::core::telemetry::global_metrics().record_cache(true);
let stub = render_unchanged_stub(&file_ref, path, line_count);
lean_ctx::core::stats::record_reread(original_tokens.saturating_sub(stub.output_tokens));
return Some(stub);
}
let rec = lean_ctx::core::read_stub_index::lookup(path)?;
if lean_ctx::core::cache::is_cache_entry_stale_verified(path, rec.stored_mtime(), &rec.hash) {
return None;
}
if !is_terminal_poll_file(path)
&& !lean_ctx::core::conversation::conversation_allows_cold_stub(
current_conversation,
rec.delivered_conversation.as_deref(),
)
{
return None;
}
Some(render_unchanged_stub(&rec.file_ref, path, rec.line_count))
}
pub(crate) fn stub_policy_allows() -> bool {
let no_deg = lean_ctx::core::config::Config::load().no_degrade_effective();
let prof = lean_ctx::core::profiles::active_profile();
let force_full = no_deg
|| (prof.read.default_mode_effective() == "full"
&& prof.compression.crp_mode.as_deref() == Some("off"));
crate::server::compaction_sync::effective_cache_policy() != "safe" && !force_full
}
fn render_unchanged_stub(file_ref: &str, path: &str, line_count: usize) -> ReadOutput {
let short = protocol::shorten_path(path);
let out = if lean_ctx::core::protocol::meta_visible() {
format!(
"{file_ref}={short} [unchanged {line_count}L]\nUnchanged on disk. Use fresh=true to force re-read.",
)
} else {
format!("{file_ref}={short} [unchanged {line_count}L · fresh=true to re-read]")
};
let out = lean_ctx::core::redaction::redact_text_if_enabled(&out);
let sent = count_tokens(&out);
ReadOutput {
content: out,
resolved_mode: "full".into(),
output_tokens: sent,
is_cache_hit: true,
}
}
pub(crate) fn render_unchanged_variant_stub(file_ref: &str, path: &str, mode: &str) -> ReadOutput {
let short = protocol::shorten_path(path);
let out = if lean_ctx::core::protocol::meta_visible() {
format!(
"{file_ref}={short} [unchanged {mode} view]\nSame {mode} output you already received. Use fresh=true to re-emit.",
)
} else {
format!("{file_ref}={short} [unchanged {mode} view · fresh=true to re-emit]")
};
let out = lean_ctx::core::redaction::redact_text_if_enabled(&out);
let sent = count_tokens(&out);
ReadOutput {
content: out,
resolved_mode: mode.to_string(),
output_tokens: sent,
is_cache_hit: true,
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeltaExplicitDecision {
pub mode: String,
pub note: Option<String>,
}
pub fn resolve_explicit_delta_mode(
cache: &SessionCache,
path: &str,
mode: &str,
explicit_mode: bool,
fresh: bool,
enabled: bool,
) -> DeltaExplicitDecision {
let unchanged = DeltaExplicitDecision {
mode: mode.to_string(),
note: None,
};
if fresh
|| !enabled
|| !explicit_mode
|| !(mode == "full" || mode == "full-compact" || mode.starts_with("lines:"))
{
return unchanged;
}
let Some(entry) = cache.get(path) else {
return unchanged;
};
let stale =
lean_ctx::core::cache::is_cache_entry_stale_verified(path, entry.stored_mtime, &entry.hash);
if stale {
if entry.content().is_some() {
return DeltaExplicitDecision {
mode: "diff".to_string(),
note: Some(format!(
"[delta-explicit] requested mode={mode} served as a diff: the file \
changed since your last read and the diff is the new information. \
Pass fresh=true if you need the full content re-emitted."
)),
};
}
return unchanged;
}
if mode.starts_with("lines:") && cache.is_full_delivered(path) {
return DeltaExplicitDecision {
mode: "full".to_string(),
note: None,
};
}
unchanged
}
pub(crate) fn file_blake3_prefix(path: &str) -> Option<DeliveryFingerprint> {
let meta = std::fs::metadata(path).ok()?;
let mtime = meta
.modified()
.ok()?
.duration_since(std::time::UNIX_EPOCH)
.ok()?
.as_secs();
let bytes = std::fs::read(path).ok()?;
let hash = blake3::hash(&bytes);
let full = hash.as_bytes();
let mut prefix = [0u8; 12];
prefix.copy_from_slice(&full[..12]);
Some(DeliveryFingerprint {
hash: prefix,
mtime,
line_count: line_count_of(&bytes),
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct DeliveryFingerprint {
pub hash: [u8; 12],
pub mtime: u64,
pub line_count: u32,
}
fn line_count_of(bytes: &[u8]) -> u32 {
let newlines = bytecount::count(bytes, b'\n');
let unterminated = usize::from(bytes.last().is_some_and(|b| *b != b'\n'));
u32::try_from(newlines + unterminated).unwrap_or(u32::MAX)
}
fn delivery_agent(
profile: Option<&lean_ctx::core::a2a::task::delivery_authority::DeliverySigningProfileV1>,
) -> String {
profile.map_or_else(
|| lean_ctx::core::agent_identity::delivery_agent_id().to_string(),
|profile| profile.agent_id.clone(),
)
}
pub(crate) fn try_cross_agent_stub(
path: &str,
mode: &str,
hash: [u8; 12],
mtime: u64,
crp_mode: CrpMode,
tuning: ReadTuning<'_>,
) -> Option<ReadOutput> {
if !lean_ctx::core::config::Config::load()
.ocla
.delivery_enabled()
{
return None;
}
if mode != "auto" && !RELAY_ELIGIBLE_MODES.contains(&mode) {
return None;
}
let profile =
lean_ctx::core::a2a::task::delivery_authority::DeliverySigningProfileV1::from_environment()
.ok()?;
let current_agent = delivery_agent(profile.as_ref());
let current_conversation = lean_ctx::core::conversation::current_conversation_id()
.unwrap_or_else(|| current_agent.clone());
let reg = lean_ctx::core::ocla::OclaRegistry::global();
let record = if let Some(profile) = profile {
lean_ctx::daemon_client::scoped_delivery_check_blocking(
&profile,
path,
hash,
¤t_conversation,
)
.ok()
.flatten()
} else {
lean_ctx::daemon_client::try_delivery_check_blocking(
&hash,
mtime,
path,
Some(¤t_agent),
Some(¤t_conversation),
)
.or_else(|| {
reg.delivery_registry.check_delivery(
&hash,
mtime,
path,
Some(¤t_agent),
Some(¤t_conversation),
)
})
}?;
render_cross_agent_relay(path, mode, &record, crp_mode, tuning)
}
fn render_cross_agent_relay(
path: &str,
mode: &str,
record: &lean_ctx::core::ocla::types::DeliveryRecord,
crp_mode: CrpMode,
tuning: ReadTuning<'_>,
) -> Option<ReadOutput> {
let relay_mode = record.relay_mode.as_deref()?;
if !relay_variant_matches(mode, relay_mode, crp_mode, tuning) {
return None;
}
let content = record.relay_content.as_ref()?;
let short = protocol::shorten_path(path);
let header = format!(
"{short} [relayed from {} · {relay_mode} · {}L]",
record.agent_id, record.line_count,
);
let body = format!("{header}\n{content}");
let tokens = count_tokens(&body);
lean_ctx::core::ocla::OclaRegistry::global()
.delivery_registry
.record_stub_served(record, tokens as u64);
Some(ReadOutput {
content: body,
resolved_mode: "cross-agent-relay".into(),
output_tokens: tokens,
is_cache_hit: true,
})
}
pub(crate) fn record_read_delivery(
path: &str,
fingerprint: DeliveryFingerprint,
resolved_mode: &str,
content: &str,
output_tokens: usize,
crp_mode: CrpMode,
tuning: ReadTuning<'_>,
) {
let relay = relay_eligible(resolved_mode, content);
let relay_key = relay.1.map(|mode| {
super::compressed_cache_key(mode, crp_mode, None, tuning.aggressiveness, tuning.protect)
});
record_cross_agent_delivery(
path,
fingerprint.hash,
fingerprint.mtime,
fingerprint.line_count,
output_tokens,
relay.0,
relay_key.as_deref(),
);
}
pub(crate) fn record_cross_agent_delivery(
path: &str,
hash: [u8; 12],
mtime: u64,
line_count: u32,
tokens: usize,
relay_content: Option<&str>,
relay_mode: Option<&str>,
) {
if !lean_ctx::core::config::Config::load()
.ocla
.delivery_enabled()
{
return;
}
let Ok(profile) =
lean_ctx::core::a2a::task::delivery_authority::DeliverySigningProfileV1::from_environment()
else {
return;
};
let agent_id = delivery_agent(profile.as_ref());
let conversation_id =
lean_ctx::core::conversation::current_conversation_id().unwrap_or_else(|| agent_id.clone());
let mut entry = lean_ctx::core::ocla::types::DeliveryEntry {
access: None,
blake3: hash,
path: path.into(),
line_count,
token_count: tokens as u64,
agent_id,
conversation_id,
mtime,
relay_content: relay_content
.filter(|c| c.len() <= MAX_RELAY_CONTENT_BYTES)
.map(str::to_string),
relay_mode: relay_mode.map(str::to_string),
};
if let Some(profile) = profile {
let Ok(scope) = lean_ctx_ocla::delivery_scope::DeliveryScopeV1::new(
profile.tenant_id.clone(),
profile.project_id.clone(),
) else {
return;
};
entry.access = Some(lean_ctx_ocla::delivery_scope::DeliveryAccessV1 {
scope,
privacy: profile.privacy,
});
let _ = lean_ctx::daemon_client::scoped_delivery_record_blocking(&profile, entry);
return;
}
lean_ctx::daemon_client::try_delivery_record_blocking(&entry);
let reg = lean_ctx::core::ocla::OclaRegistry::global();
reg.delivery_registry.record_delivery(entry);
}
#[cfg(test)]
mod tests {
use super::{
CrpMode, ReadOutput, ReadTuning, SessionCache, effective_fresh_flags,
is_terminal_poll_file, relay_variant_matches, render_cross_agent_relay,
try_cross_agent_stub, try_stub_hit_readonly_scoped,
};
use std::sync::atomic::Ordering;
#[test]
fn cross_agent_stub_miss_returns_none() {
let stub = try_cross_agent_stub(
"/nonexistent/file.rs",
"auto",
[0; 12],
0,
CrpMode::Off,
ReadTuning::default(),
);
assert!(stub.is_none());
}
#[test]
fn relay_variants_preserve_requested_read_semantics() {
let defaults = ReadTuning::default();
assert!(relay_variant_matches(
"map",
"map:v2",
CrpMode::Off,
defaults
));
assert!(relay_variant_matches(
"auto",
"signatures:v2",
CrpMode::Off,
defaults
));
for mode in [
"signatures",
"lines:1-5",
"raw",
"full",
"task",
"reference",
] {
assert!(!relay_variant_matches(
mode,
"map:v2",
CrpMode::Off,
defaults
));
}
for stored in ["map", "map:v3", "mapping", "map:v2:tdd", "map:v2:unknown"] {
assert!(!relay_variant_matches(
"map",
stored,
CrpMode::Off,
defaults
));
}
assert!(relay_variant_matches(
"map",
"map:v2:tdd",
CrpMode::Tdd,
defaults
));
let protect = vec!["important".to_owned()];
let tuned = ReadTuning {
aggressiveness: Some(0.7),
protect: &protect,
};
let key = super::super::compressed_cache_key(
"map",
CrpMode::Off,
None,
tuned.aggressiveness,
tuned.protect,
);
assert!(relay_variant_matches("map", &key, CrpMode::Off, tuned));
assert!(!relay_variant_matches("map", &key, CrpMode::Off, defaults));
assert!(!relay_variant_matches("map", "map:v2", CrpMode::Off, tuned));
}
#[test]
fn relay_rendering_requires_matching_usable_payload() {
let _isolated = lean_ctx::core::data_dir::isolated_data_dir();
let mut record = lean_ctx::core::ocla::types::DeliveryRecord {
access: None,
blake3: [81; 12],
path: "variant.rs".into(),
line_count: 12,
token_count: 200,
agent_id: "source-agent".into(),
conversation_id: "source-conversation".into(),
read_at: 1,
mtime: 1,
relay_content: Some("fn shared_context()".into()),
relay_mode: Some("map:v2".into()),
fresh: true,
};
let render = |record: &lean_ctx::core::ocla::types::DeliveryRecord, mode| {
render_cross_agent_relay(
"variant.rs",
mode,
record,
CrpMode::Off,
ReadTuning::default(),
)
};
let output = render(&record, "map").expect("matching relay");
assert!(output.is_cache_hit);
assert_eq!(output.resolved_mode, "cross-agent-relay");
assert!(output.content.contains("fn shared_context()"));
assert_eq!(output.output_tokens, super::count_tokens(&output.content));
for mode in ["signatures", "lines:1-2", "raw", "full"] {
assert!(render(&record, mode).is_none());
}
record.relay_mode = None;
assert!(render(&record, "map").is_none());
record.relay_mode = Some("map:v2".into());
record.relay_content = None;
assert!(render(&record, "map").is_none());
}
#[test]
fn invalid_signing_profile_never_reads_legacy_delivery() {
const CHILD: &str = "LEAN_CTX_TEST_INVALID_DELIVERY_PROFILE";
if std::env::var_os(CHILD).is_none() {
let status = std::process::Command::new(std::env::current_exe().expect("test executable"))
.args(["--exact", "tools::ctx_read::dispatch::tests::invalid_signing_profile_never_reads_legacy_delivery", "--nocapture"])
.env(CHILD, "1")
.env("LEAN_CTX_DELIVERY_PROFILE", "{invalid")
.status().expect("child test");
assert!(status.success());
return;
}
let _isolated = lean_ctx::core::data_dir::isolated_data_dir();
let registry = lean_ctx::core::ocla::OclaRegistry::global();
let path = "/legacy-delivery-profile-regression";
registry
.delivery_registry
.record_delivery(lean_ctx::core::ocla::types::DeliveryEntry {
access: None,
blake3: [93; 12],
path: path.into(),
line_count: 1,
token_count: 100,
agent_id: "legacy-owner".into(),
conversation_id: "legacy-conversation".into(),
mtime: 1,
relay_content: Some("must not leak".into()),
relay_mode: Some("map".into()),
});
assert!(
registry
.delivery_registry
.check_delivery(&[93; 12], 1, path, None, None)
.is_some()
);
assert!(
try_cross_agent_stub(
path,
"auto",
[93; 12],
1,
CrpMode::Off,
ReadTuning::default()
)
.is_none()
);
}
#[test]
fn subagent_delivery_policy_keeps_cache_fresh_but_allows_delivery_by_default() {
let delivery_for_subagents =
lean_ctx::core::config::DeliveryConfig::default().delivery_for_subagents;
assert!(
delivery_for_subagents,
"delivery must default to enabled for subagents"
);
let (cache_fresh, delivery_fresh) =
effective_fresh_flags(false, false, true, delivery_for_subagents);
assert!(cache_fresh, "subagent cache must remain isolated");
assert!(
!delivery_fresh,
"default policy must allow a cross-agent delivery lookup"
);
}
#[test]
fn subagent_delivery_policy_can_force_fresh_delivery() {
let (cache_fresh, delivery_fresh) = effective_fresh_flags(false, false, true, false);
assert!(cache_fresh);
assert!(delivery_fresh, "disabled policy must bypass delivery stubs");
}
#[test]
fn line_count_matches_str_lines_semantics() {
for sample in ["", "a", "a\n", "a\nb", "a\nb\n", "\n\n", "a\r\nb\r\n"] {
assert_eq!(
super::line_count_of(sample.as_bytes()) as usize,
sample.lines().count(),
"line count diverged for {sample:?}"
);
}
}
#[test]
fn fingerprint_carries_real_line_count() {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("three.rs");
std::fs::write(&file, "fn a() {}\nfn b() {}\nfn c() {}\n").unwrap();
let fp = super::file_blake3_prefix(file.to_str().unwrap()).unwrap();
assert_eq!(fp.line_count, 3);
}
fn foreign_delivery(
relay: Option<(&str, &str)>,
) -> (tempfile::TempDir, String, super::DeliveryFingerprint) {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("foreign.rs");
std::fs::write(&file, "pub fn foreign() {}\npub fn other() {}\n").unwrap();
let path = file.to_string_lossy().to_string();
let fp = super::file_blake3_prefix(&path).unwrap();
lean_ctx::core::ocla::OclaRegistry::global()
.delivery_registry
.record_delivery(lean_ctx::core::ocla::types::DeliveryEntry {
access: None,
blake3: fp.hash,
path: path.clone(),
line_count: fp.line_count,
token_count: 40,
agent_id: "claude-foreign".into(),
conversation_id: "conv-foreign".into(),
mtime: fp.mtime,
relay_content: relay.map(|(c, _)| c.to_string()),
relay_mode: relay.map(|(_, m)| m.to_string()),
});
(dir, path, fp)
}
fn cross_agent(path: &str, mode: &str, fp: super::DeliveryFingerprint) -> Option<ReadOutput> {
try_cross_agent_stub(
path,
mode,
fp.hash,
fp.mtime,
CrpMode::Off,
ReadTuning::default(),
)
}
#[test]
fn content_free_stub_is_never_served_for_another_agents_read() {
let _isolated = lean_ctx::core::data_dir::isolated_data_dir();
let (_dir, path, fp) = foreign_delivery(None);
for mode in ["auto", "signatures", "map", "aggressive"] {
assert!(
cross_agent(&path, mode, fp).is_none(),
"content-free cross-agent stub leaked for mode={mode}"
);
}
}
#[test]
fn relay_served_only_for_matching_mode() {
let _isolated = lean_ctx::core::data_dir::isolated_data_dir();
let relay = "pub fn foreign() {}";
let (_dir, path, fp) = foreign_delivery(Some((relay, "map:v2")));
let hit = cross_agent(&path, "map", fp).expect("map request must accept a map:v2 relay");
assert!(hit.content.contains(relay), "{}", hit.content);
assert!(hit.content.contains("· 2L]"), "{}", hit.content);
assert!(
cross_agent(&path, "signatures", fp).is_none(),
"a signatures request must not be answered with a map relay"
);
assert!(cross_agent(&path, "lines:1-2", fp).is_none());
}
#[test]
fn warm_stub_hit_records_central_telemetry() {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("telemetry-hit.rs");
std::fs::write(&file, "fn telemetry_hit() {}\n").unwrap();
let path = file.to_string_lossy();
let mut cache = SessionCache::new();
cache.store(&path, "fn telemetry_hit() {}\n");
cache.mark_full_delivered(&path);
let metrics = lean_ctx::core::telemetry::global_metrics();
let before = metrics.cache_hits.load(Ordering::Relaxed);
let output = try_stub_hit_readonly_scoped(&cache, &path, None);
let after = metrics.cache_hits.load(Ordering::Relaxed);
assert!(output.is_some(), "warm re-read must use the stub cache");
assert!(
after > before,
"stub cache hit must increment central telemetry"
);
}
#[test]
fn relay_does_not_poison_session_cache() {
let mut cache = SessionCache::new();
let path = "/tmp/test_relay_poison.rs";
cache.store(path, "original content");
assert_eq!(
cache
.get(path)
.map(|e| e.compressed_outputs.contains_key("cross-agent-relay")),
Some(false),
"cross-agent-relay must not exist in session cache"
);
}
#[test]
fn is_terminal_poll_file_matches_cursor_terminal_paths() {
assert!(is_terminal_poll_file(
"/Users/me/.cursor/projects/Users-me-proj/terminals/12345.txt"
));
assert!(is_terminal_poll_file(
"/home/dev/.cursor/projects/foo/terminals/857272.txt"
));
assert!(!is_terminal_poll_file("/Users/me/project/src/main.rs"));
assert!(!is_terminal_poll_file(
"/Users/me/.cursor/projects/foo/agent-transcripts/abc.jsonl"
));
assert!(!is_terminal_poll_file("/Users/me/.cursor/terminals.log"));
}
#[test]
fn terminal_poll_file_stub_bypasses_conversation_gate() {
let dir = tempfile::tempdir().unwrap();
let terminals_dir = dir.path().join(".cursor").join("proj").join("terminals");
std::fs::create_dir_all(&terminals_dir).unwrap();
let file = terminals_dir.join("252028.txt");
std::fs::write(&file, "---\npid: 1234\n---\nterminal output\n").unwrap();
let path = file.to_string_lossy().to_string();
let mut cache = SessionCache::new();
cache.store(&path, "---\npid: 1234\n---\nterminal output\n");
cache.mark_full_delivered(&path);
let output = try_stub_hit_readonly_scoped(&cache, &path, Some("different-conv"));
assert!(
output.is_some(),
"terminal poll file must bypass conversation gate and serve stub"
);
}
#[test]
fn non_terminal_file_respects_conversation_gate() {
if lean_ctx::test_env::run_with_conversation_scope(
"tools::ctx_read::dispatch::tests::non_terminal_file_respects_conversation_gate",
true,
) {
return;
}
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("main.rs");
std::fs::write(&file, "fn main() {}\n").unwrap();
let path = file.to_string_lossy().to_string();
let mut cache = SessionCache::new();
cache.store(&path, "fn main() {}\n");
cache.mark_full_delivered(&path);
if let Some(entry) = cache.get_mut(&path) {
entry.delivered_conversation = Some("conv-A".to_string());
}
let output = try_stub_hit_readonly_scoped(&cache, &path, Some("conv-B"));
assert!(
output.is_none(),
"regular file must respect conversation gate"
);
}
#[test]
fn a_read_deposits_no_scent() {
let _data = lean_ctx::core::data_dir::isolated_data_dir();
let project = tempfile::tempdir().unwrap();
let file = project.path().join("scent_marker.rs");
std::fs::write(&file, "pub fn scent_marker() {}\n").unwrap();
let root = project.path().to_str().unwrap();
let mut cache = SessionCache::new();
let out = lean_ctx::core::policy::runtime::REQUEST_PROJECT.sync_scope(
std::cell::RefCell::new(Some(project.path().to_path_buf())),
|| super::handle(&mut cache, file.to_str().unwrap(), "full", CrpMode::Off),
);
assert!(out.contains("scent_marker"), "{out}");
assert!(
lean_ctx::core::scent_field::sync_block(root).is_empty(),
"a read must not deposit a scent"
);
}
}