use std::{
fs,
path::{Path, PathBuf},
};
const PROJECT_MD_MAX_CHARS: usize = 20_000;
const PROJECT_MD_HEAD_RATIO: f64 = 0.70;
pub const DEFAULT_PROJECT_MD_FILE_NAMES: &[&str] = &[
"TINYHARNESS.md",
".tinyharness.md",
"AGENTS.md",
"CLAUDE.md",
];
pub const PROJECT_MD_FILE_NAMES: &[&str] = DEFAULT_PROJECT_MD_FILE_NAMES;
#[derive(Debug, Clone)]
pub struct WorkspaceContext {
pub root: PathBuf,
pub project_type: String,
pub project_name: String,
pub structure: Vec<String>,
pub is_git_repo: bool,
pub build_command: String,
pub test_command: String,
pub project_md: Option<(String, String)>, pub additional_project_mds: Vec<(String, String)>,
}
impl WorkspaceContext {
pub fn collect() -> Self {
Self::collect_in(&crate::paths::Paths::default())
}
pub fn collect_in(paths: &crate::paths::Paths) -> Self {
let root = paths.project_dir().to_path_buf();
let project_type = detect_project_type(&root);
let project_name = detect_project_name(&root, &project_type);
let structure = list_top_level(&root);
let is_git_repo = root.join(".git").is_dir();
let (build_command, test_command) = detect_commands(&project_type, &root);
let settings = crate::config::load_settings_from(paths);
let md_files = crate::config::resolve_project_md_files(Some(&settings));
let project_md = discover_project_md(&root, &md_files);
let additional_project_mds = crate::config::project_settings_or_none(paths)
.map(|proj| load_additional_md_files(&root, proj.project_md_files.as_deref()))
.unwrap_or_default();
WorkspaceContext {
root,
project_type: project_type.to_string(),
project_name,
structure,
is_git_repo,
build_command: build_command.to_string(),
test_command: test_command.to_string(),
project_md,
additional_project_mds,
}
}
pub fn format(&self) -> String {
let mut lines = Vec::new();
lines.push(format!(
"You are operating inside a {} project called \"{}\".",
self.project_type, self.project_name
));
lines.push(format!("Workspace root: {}", self.root.display()));
if self.is_git_repo {
lines.push("This is a git repository.".to_string());
}
lines.push("\nProject structure:".to_string());
for entry in &self.structure {
lines.push(format!(" {}", entry));
}
if !self.build_command.is_empty() {
lines.push(format!("\nBuild command: {}", self.build_command));
}
if !self.test_command.is_empty() {
lines.push(format!("Test command: {}", self.test_command));
}
if let Some((filename, content)) = &self.project_md {
lines.push(format!("\n---\n# Project Instructions (from {filename})\n"));
lines.push(content.clone());
}
for (filename, content) in &self.additional_project_mds {
lines.push(format!(
"\n---\n# Additional Instructions (from {filename})\n"
));
lines.push(content.clone());
}
lines.join("\n")
}
}
fn load_additional_md_files(root: &Path, files: Option<&[String]>) -> Vec<(String, String)> {
let Some(files) = files else {
return Vec::new();
};
files
.iter()
.filter_map(|name| {
let path = root.join(name);
if path.is_file()
&& let Ok(content) = fs::read_to_string(&path)
{
let truncated = truncate_content(&content, name);
Some((name.clone(), truncated))
} else {
None
}
})
.collect()
}
struct LanguageSignature {
label: &'static str,
markers: &'static [&'static str],
build_cmd: &'static str,
test_cmd: &'static str,
}
const LANGUAGE_SIGNATURES: &[LanguageSignature] = &[
LanguageSignature {
label: "Rust",
markers: &["Cargo.toml"],
build_cmd: "cargo build",
test_cmd: "cargo test",
},
LanguageSignature {
label: "Zig",
markers: &["build.zig", "build.zig.zon"],
build_cmd: "zig build",
test_cmd: "zig build test",
},
LanguageSignature {
label: "Deno",
markers: &["deno.json", "deno.jsonc"],
build_cmd: "deno task build",
test_cmd: "deno test",
},
LanguageSignature {
label: "Bun",
markers: &["bun.lockb", "bun.lock"],
build_cmd: "bun run build",
test_cmd: "bun test",
},
LanguageSignature {
label: "Swift",
markers: &["Package.swift"],
build_cmd: "swift build",
test_cmd: "swift test",
},
LanguageSignature {
label: "Ruby",
markers: &["Gemfile"],
build_cmd: "bundle install",
test_cmd: "bundle exec rspec",
},
LanguageSignature {
label: "Elixir",
markers: &["mix.exs"],
build_cmd: "mix compile",
test_cmd: "mix test",
},
LanguageSignature {
label: "Haskell",
markers: &["stack.yaml"],
build_cmd: "stack build",
test_cmd: "stack test",
},
LanguageSignature {
label: "Kotlin",
markers: &["build.gradle.kts", "settings.gradle.kts"],
build_cmd: "gradle build",
test_cmd: "gradle test",
},
LanguageSignature {
label: "Dart/Flutter",
markers: &["pubspec.yaml"],
build_cmd: "dart run build",
test_cmd: "dart test",
},
LanguageSignature {
label: "Nix",
markers: &["flake.nix", "default.nix"],
build_cmd: "nix build",
test_cmd: "nix flake check",
},
LanguageSignature {
label: "Node.js",
markers: &["package.json"],
build_cmd: "npm run build",
test_cmd: "npm test",
},
LanguageSignature {
label: "Python",
markers: &[
"pyproject.toml",
"setup.py",
"setup.cfg",
"requirements.txt",
],
build_cmd: "pip install -e .",
test_cmd: "pytest",
},
LanguageSignature {
label: "Go",
markers: &["go.mod"],
build_cmd: "go build ./...",
test_cmd: "go test ./...",
},
LanguageSignature {
label: "Java (Gradle)",
markers: &["build.gradle"],
build_cmd: "gradle build",
test_cmd: "gradle test",
},
LanguageSignature {
label: "Java (Maven)",
markers: &["pom.xml"],
build_cmd: "mvn compile",
test_cmd: "mvn test",
},
LanguageSignature {
label: "C/C++ (CMake)",
markers: &["CMakeLists.txt"],
build_cmd: "cmake --build build",
test_cmd: "ctest",
},
];
const SECONDARY_MARKERS: &[(&str, &str)] = &[
(".NET", "*.csproj"),
(".NET", "*.sln"),
("Haskell (Cabal)", "*.cabal"),
];
fn detect_project_type(root: &Path) -> String {
let mut found: Vec<&'static str> = Vec::new();
for sig in LANGUAGE_SIGNATURES {
for marker in sig.markers {
if root.join(marker).exists() {
if !found.contains(&sig.label) {
found.push(sig.label);
}
break; }
}
}
for (label, pattern) in SECONDARY_MARKERS {
if found.contains(label) {
continue;
}
if let Ok(entries) = glob::glob(&root.join(pattern).to_string_lossy())
&& entries.flatten().next().is_some()
{
found.push(label);
}
}
if found.is_empty() {
if root.join("Makefile").exists() {
"Unknown (has Makefile)".to_string()
} else if root.join("Justfile").exists() {
"Unknown (has Justfile)".to_string()
} else {
"Unknown".to_string()
}
} else if found.len() == 1 {
found[0].to_string()
} else {
found.join(" + ")
}
}
fn extract_quoted_field<'a>(line: &'a str, key: &str) -> Option<&'a str> {
let trimmed = line.trim();
for (prefix, quote) in [
(format!("{} = \"", key), '"'),
(format!("{} = '", key), '\''),
] {
if let Some(name) = trimmed
.strip_prefix(&prefix)
.and_then(|n| n.find(quote).map(|end| &n[..end]))
{
return Some(name);
}
}
None
}
fn detect_project_name(root: &Path, project_type: &str) -> String {
let fallback = || {
root.file_name()
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_else(|| "Unknown".to_string())
};
let primary = project_type
.split_once(" + ")
.map(|(a, _)| a)
.unwrap_or(project_type);
match primary {
"Rust" => {
if let Ok(content) = fs::read_to_string(root.join("Cargo.toml")) {
for line in content.lines() {
if let Some(name) = extract_quoted_field(line, "name") {
return name.to_string();
}
}
}
fallback()
}
"Node.js" | "Bun" | "Deno" => {
if let Ok(content) = fs::read_to_string(root.join("package.json"))
&& let Ok(json) = serde_json::from_str::<serde_json::Value>(&content)
&& let Some(name) = json.get("name").and_then(|n| n.as_str())
{
return name.to_string();
}
fallback()
}
"Python" => {
if let Ok(content) = fs::read_to_string(root.join("pyproject.toml")) {
for line in content.lines() {
if let Some(name) = extract_quoted_field(line, "name") {
return name.to_string();
}
}
}
fallback()
}
"Go" => {
if let Ok(content) = fs::read_to_string(root.join("go.mod")) {
for line in content.lines() {
if let Some(rest) = line.strip_prefix("module ") {
let name = rest.trim();
if !name.is_empty() {
return name.to_string();
}
}
}
}
fallback()
}
_ => fallback(),
}
}
fn list_top_level(root: &Path) -> Vec<String> {
let mut entries = Vec::new();
if let Ok(read_dir) = fs::read_dir(root) {
for entry in read_dir.flatten() {
let path = entry.path();
let name = path
.file_name()
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_default();
if name.starts_with('.') || name == "target" || name == "node_modules" {
continue;
}
if path.is_dir() {
let mut children: Vec<String> = Vec::new();
if let Ok(sub_dir) = fs::read_dir(&path) {
for sub in sub_dir.flatten() {
let sub_name = sub
.path()
.file_name()
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_default();
if !sub_name.starts_with('.') {
children.push(sub_name);
}
}
}
children.sort();
if children.len() <= 6 {
let child_list = children.join(", ");
entries.push(format!("{}/ ({})", name, child_list));
} else {
entries.push(format!("{}/ ({} entries)", name, children.len()));
}
} else {
entries.push(name);
}
}
}
entries.sort();
entries
}
fn detect_commands(project_type: &str, root: &Path) -> (&'static str, &'static str) {
let primary = project_type
.split_once(" + ")
.map(|(a, _)| a)
.unwrap_or(project_type);
for sig in LANGUAGE_SIGNATURES {
if sig.label == primary {
return (sig.build_cmd, sig.test_cmd);
}
}
if root.join("Makefile").exists() {
("make", "make test")
} else if root.join("Justfile").exists() {
("just build", "just test")
} else {
("", "")
}
}
fn discover_project_md(start_dir: &Path, file_names: &[String]) -> Option<(String, String)> {
if file_names.is_empty() {
return None;
}
let mut dir = start_dir.to_path_buf();
loop {
for filename in file_names {
let candidate = dir.join(filename);
if candidate.is_file()
&& let Ok(content) = fs::read_to_string(&candidate)
{
let truncated = truncate_content(&content, filename);
return Some((filename.to_string(), truncated));
}
}
if let Some(parent) = dir.parent() {
if parent == dir {
break;
}
dir = parent.to_path_buf();
} else {
break;
}
}
None
}
fn truncate_content(content: &str, filename: &str) -> String {
if content.len() <= PROJECT_MD_MAX_CHARS {
return content.to_string();
}
let head_end = (PROJECT_MD_MAX_CHARS as f64 * PROJECT_MD_HEAD_RATIO) as usize;
let tail_size = (PROJECT_MD_MAX_CHARS as f64 * (1.0 - PROJECT_MD_HEAD_RATIO)) as usize;
let head = &content[..content.floor_char_boundary(head_end)];
let tail_start = content.len().saturating_sub(tail_size);
let tail = &content[content.floor_char_boundary(tail_start)..];
let total = content.len();
let kept_head = head.len();
let kept_tail = tail.len();
format!(
"{head}\n\n[...truncated {filename}: kept {kept_head}+{kept_tail} of {total} chars. Use the read tool to view the full file.]\n\n{tail}"
)
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
fn default_md_names() -> Vec<String> {
DEFAULT_PROJECT_MD_FILE_NAMES
.iter()
.map(|s| s.to_string())
.collect()
}
fn custom_md_names(names: &[&str]) -> Vec<String> {
names.iter().map(|s| s.to_string()).collect()
}
#[test]
fn test_discover_project_md_tinyharness_md() {
let dir = tempfile::tempdir().unwrap();
let dir_path = dir.path();
fs::write(dir_path.join("TINYHARNESS.md"), "# Project\n\nUse Rust.").unwrap();
let result = discover_project_md(dir_path, &default_md_names());
assert!(result.is_some());
let (filename, content) = result.unwrap();
assert_eq!(filename, "TINYHARNESS.md");
assert!(content.contains("# Project"));
assert!(content.contains("Use Rust."));
}
#[test]
fn test_discover_project_md_priority() {
let dir = tempfile::tempdir().unwrap();
let dir_path = dir.path();
fs::write(dir_path.join("TINYHARNESS.md"), "# From TINYHARNESS.md").unwrap();
fs::write(dir_path.join("AGENTS.md"), "# From AGENTS.md").unwrap();
fs::write(dir_path.join("CLAUDE.md"), "# From CLAUDE.md").unwrap();
let result = discover_project_md(dir_path, &default_md_names());
assert!(result.is_some());
let (filename, content) = result.unwrap();
assert_eq!(filename, "TINYHARNESS.md");
assert!(content.contains("From TINYHARNESS.md"));
}
#[test]
fn test_discover_project_md_agents_md_fallback() {
let dir = tempfile::tempdir().unwrap();
let dir_path = dir.path();
fs::write(dir_path.join("AGENTS.md"), "# From AGENTS.md").unwrap();
let result = discover_project_md(dir_path, &default_md_names());
assert!(result.is_some());
let (filename, content) = result.unwrap();
assert_eq!(filename, "AGENTS.md");
assert!(content.contains("From AGENTS.md"));
}
#[test]
fn test_discover_project_md_claude_md_fallback() {
let dir = tempfile::tempdir().unwrap();
let dir_path = dir.path();
fs::write(dir_path.join("CLAUDE.md"), "# From CLAUDE.md").unwrap();
let result = discover_project_md(dir_path, &default_md_names());
assert!(result.is_some());
let (filename, content) = result.unwrap();
assert_eq!(filename, "CLAUDE.md");
assert!(content.contains("From CLAUDE.md"));
}
#[test]
fn test_discover_project_md_none() {
let dir = tempfile::tempdir().unwrap();
let result = discover_project_md(dir.path(), &default_md_names());
assert!(result.is_none());
}
#[test]
fn test_discover_project_md_walks_up() {
let dir = tempfile::tempdir().unwrap();
let dir_path = dir.path();
fs::write(dir_path.join("TINYHARNESS.md"), "# Found in parent").unwrap();
let subdir = dir_path.join("src").join("tools");
fs::create_dir_all(&subdir).unwrap();
let result = discover_project_md(&subdir, &default_md_names());
assert!(result.is_some());
let (filename, content) = result.unwrap();
assert_eq!(filename, "TINYHARNESS.md");
assert!(content.contains("Found in parent"));
}
#[test]
fn test_discover_project_md_hidden_variant() {
let dir = tempfile::tempdir().unwrap();
let dir_path = dir.path();
fs::write(dir_path.join(".tinyharness.md"), "# Hidden variant").unwrap();
let result = discover_project_md(dir_path, &default_md_names());
assert!(result.is_some());
let (filename, content) = result.unwrap();
assert_eq!(filename, ".tinyharness.md");
assert!(content.contains("Hidden variant"));
}
#[test]
fn test_discover_project_md_custom_names() {
let dir = tempfile::tempdir().unwrap();
let dir_path = dir.path();
fs::write(dir_path.join("TEAM_RULES.md"), "# Team Rules").unwrap();
fs::write(dir_path.join("CLAUDE.md"), "# Claude Rules").unwrap();
let names = custom_md_names(&["TEAM_RULES.md", "CLAUDE.md"]);
let result = discover_project_md(dir_path, &names);
assert!(result.is_some());
let (filename, content) = result.unwrap();
assert_eq!(filename, "TEAM_RULES.md");
assert!(content.contains("# Team Rules"));
}
#[test]
fn test_discover_project_md_empty_list() {
let dir = tempfile::tempdir().unwrap();
let dir_path = dir.path();
fs::write(dir_path.join("TINYHARNESS.md"), "# Project").unwrap();
let result = discover_project_md(dir_path, &[]);
assert!(result.is_none());
}
#[test]
fn test_truncate_content_under_limit() {
let content = "Hello, world!".to_string();
let result = truncate_content(&content, "TINYHARNESS.md");
assert_eq!(result, content); }
#[test]
fn test_truncate_content_over_limit() {
let content = "A".repeat(PROJECT_MD_MAX_CHARS + 5000);
let result = truncate_content(&content, "TINYHARNESS.md");
assert!(result.contains("[...truncated TINYHARNESS.md"));
assert!(result.contains("Use the read tool to view the full file"));
assert!(result.len() < content.len());
assert!(result.starts_with('A'));
assert!(result.ends_with('A'));
}
#[test]
fn test_format_includes_project_md() {
let ctx = WorkspaceContext {
root: PathBuf::from("/tmp/test"),
project_type: "Rust".to_string(),
project_name: "test-project".to_string(),
structure: vec!["src/ (main.rs)".to_string()],
is_git_repo: false,
build_command: "cargo build".to_string(),
test_command: "cargo test".to_string(),
project_md: Some((
"TINYHARNESS.md".to_string(),
"# My Rules\nAlways use Rust.".to_string(),
)),
additional_project_mds: Vec::new(),
};
let formatted = ctx.format();
assert!(formatted.contains("# Project Instructions (from TINYHARNESS.md)"));
assert!(formatted.contains("# My Rules"));
assert!(formatted.contains("Always use Rust."));
}
#[test]
fn test_format_without_project_md() {
let ctx = WorkspaceContext {
root: PathBuf::from("/tmp/test"),
project_type: "Rust".to_string(),
project_name: "test-project".to_string(),
structure: vec!["src/ (main.rs)".to_string()],
is_git_repo: false,
build_command: "cargo build".to_string(),
test_command: "cargo test".to_string(),
project_md: None,
additional_project_mds: Vec::new(),
};
let formatted = ctx.format();
assert!(!formatted.contains("Project Instructions"));
}
#[test]
fn test_format_includes_additional_mds() {
let ctx = WorkspaceContext {
root: PathBuf::from("/tmp/test"),
project_type: "Rust".to_string(),
project_name: "test-project".to_string(),
structure: vec!["src/ (main.rs)".to_string()],
is_git_repo: false,
build_command: "cargo build".to_string(),
test_command: "cargo test".to_string(),
project_md: None,
additional_project_mds: vec![("RULES.md".to_string(), "# Custom Rules".to_string())],
};
let formatted = ctx.format();
assert!(formatted.contains("# Additional Instructions (from RULES.md)"));
assert!(formatted.contains("# Custom Rules"));
}
#[test]
fn test_discover_project_md_hidden_over_agents() {
let dir = tempfile::tempdir().unwrap();
let dir_path = dir.path();
fs::write(dir_path.join(".tinyharness.md"), "# Hidden").unwrap();
fs::write(dir_path.join("AGENTS.md"), "# Agents").unwrap();
let result = discover_project_md(dir_path, &default_md_names());
assert!(result.is_some());
let (filename, _) = result.unwrap();
assert_eq!(filename, ".tinyharness.md");
}
}