use std::path::{Path, PathBuf};
pub const MAX_CONTEXT_BYTES: usize = 16 * 1024;
const CLAUDE_MD: &str = "CLAUDE.md";
pub fn load_project_context(project_root: &Path) -> Option<String> {
let path = locate_claude_md(project_root)?;
let raw = std::fs::read_to_string(&path).ok()?;
Some(truncate_with_note(&raw, MAX_CONTEXT_BYTES))
}
fn locate_claude_md(project_root: &Path) -> Option<PathBuf> {
let root = project_root.join(CLAUDE_MD);
if root.is_file() {
return Some(root);
}
let nested =
crate::paths::resolve_project_entry(project_root, CLAUDE_MD, crate::paths::EntryKind::File);
(nested.source != crate::paths::ConfigSource::Default).then_some(nested.path)
}
fn truncate_with_note(content: &str, max_bytes: usize) -> String {
if content.len() <= max_bytes {
return content.to_string();
}
let mut end = max_bytes;
while end > 0 && !content.is_char_boundary(end) {
end -= 1;
}
let kept = &content[..end];
format!(
"{kept}\n\n[CLAUDE.md truncated to {end} bytes of {total} total]",
total = content.len()
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn loads_root_claude_md() {
let tmp = tempfile::tempdir().expect("tempdir");
std::fs::write(tmp.path().join("CLAUDE.md"), "PROJECT RULES: be concise.")
.expect("write CLAUDE.md");
let ctx = load_project_context(tmp.path()).expect("context loaded");
assert_eq!(ctx, "PROJECT RULES: be concise.");
}
#[test]
fn loads_nested_claude_md() {
let tmp = tempfile::tempdir().expect("tempdir");
let nested = tmp.path().join(".claude");
std::fs::create_dir_all(&nested).expect("mkdir .claude");
std::fs::write(nested.join("CLAUDE.md"), "NESTED RULES").expect("write nested");
let ctx = load_project_context(tmp.path()).expect("nested context loaded");
assert_eq!(ctx, "NESTED RULES");
}
#[test]
fn loads_trusty_code_claude_md() {
let tmp = tempfile::tempdir().expect("tempdir");
let native = tmp.path().join(".trusty-code");
std::fs::create_dir_all(&native).expect("mkdir .trusty-code");
std::fs::write(native.join("CLAUDE.md"), "NATIVE RULES").expect("write native");
assert_eq!(
load_project_context(tmp.path()).expect("native context loaded"),
"NATIVE RULES"
);
let compat = tmp.path().join(".claude");
std::fs::create_dir_all(&compat).expect("mkdir .claude");
std::fs::write(compat.join("CLAUDE.md"), "COMPAT RULES").expect("write compat");
assert_eq!(
load_project_context(tmp.path()).expect("native context still wins"),
"NATIVE RULES"
);
}
#[test]
fn root_takes_precedence_over_nested() {
let tmp = tempfile::tempdir().expect("tempdir");
std::fs::write(tmp.path().join("CLAUDE.md"), "ROOT").expect("write root");
let nested = tmp.path().join(".claude");
std::fs::create_dir_all(&nested).expect("mkdir .claude");
std::fs::write(nested.join("CLAUDE.md"), "NESTED").expect("write nested");
let ctx = load_project_context(tmp.path()).expect("loaded");
assert_eq!(ctx, "ROOT", "root CLAUDE.md must take precedence");
}
#[test]
fn missing_file_returns_none() {
let tmp = tempfile::tempdir().expect("tempdir");
assert!(
load_project_context(tmp.path()).is_none(),
"missing CLAUDE.md must return None, not error"
);
}
#[test]
fn within_budget_is_returned_verbatim() {
let small = "a".repeat(100);
let out = truncate_with_note(&small, MAX_CONTEXT_BYTES);
assert_eq!(out, small);
assert!(!out.contains("truncated"), "no note for in-budget content");
}
#[test]
fn oversized_context_is_truncated_with_note() {
let content = "x".repeat(50);
let out = truncate_with_note(&content, 10);
assert!(
out.starts_with(&"x".repeat(10)),
"kept prefix must be the first 10 bytes"
);
assert!(
out.contains("[CLAUDE.md truncated to 10 bytes of 50 total]"),
"must carry the provenance note, got: {out}"
);
}
#[test]
fn truncation_respects_utf8_boundary() {
let content = "é".repeat(10); let out = truncate_with_note(&content, 5);
let note_start = out.find("\n\n[CLAUDE.md").expect("note present");
assert_eq!(&out[..note_start], "éé", "must back off to a char boundary");
}
#[test]
fn loaded_context_is_positioned_after_agent_prompt() {
use crate::agents::AgentConfig;
use crate::agents::config::{AgentInfo, SystemPrompt};
use crate::prompt::assemble_system_prompt;
let tmp = tempfile::tempdir().expect("tempdir");
std::fs::write(tmp.path().join("CLAUDE.md"), "PROJECT-CONTEXT-MARKER").expect("write");
let ctx = load_project_context(tmp.path()).expect("loaded");
let config = AgentConfig {
agent: AgentInfo {
name: "pm".into(),
..Default::default()
},
system_prompt: SystemPrompt {
content: "AGENT-PROMPT-MARKER".into(),
..Default::default()
},
..Default::default()
};
let prompt = assemble_system_prompt(&config, Some(&ctx), None);
let agent_at = prompt
.find("AGENT-PROMPT-MARKER")
.expect("agent marker present");
let ctx_at = prompt
.find("PROJECT-CONTEXT-MARKER")
.expect("project context marker present");
assert!(
ctx_at > agent_at,
"project context must be positioned after the agent prompt"
);
}
#[test]
fn loader_enforces_byte_cap() {
let tmp = tempfile::tempdir().expect("tempdir");
let big = "z".repeat(MAX_CONTEXT_BYTES + 4096);
std::fs::write(tmp.path().join("CLAUDE.md"), &big).expect("write big");
let ctx = load_project_context(tmp.path()).expect("loaded");
assert!(
ctx.contains("[CLAUDE.md truncated to"),
"oversized file must be truncated with a note"
);
let note_start = ctx.find("\n\n[CLAUDE.md").expect("note present");
assert!(
note_start <= MAX_CONTEXT_BYTES,
"kept body must be within the byte cap"
);
}
}