1use std::path::{Path, PathBuf};
11
12pub const RULE_FILES: [&str; 3] = [".blyte/rules.md", ".claude/agy-rules.md", "AGENTS.md"];
14
15pub const MAX_RULES_CHARS: usize = 12_000;
17
18#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct RepoRules {
21 pub source: PathBuf,
22 pub text: String,
23}
24
25fn main_checkout(root: &Path) -> Option<PathBuf> {
27 let out = std::process::Command::new("git")
28 .args(["rev-parse", "--path-format=absolute", "--git-common-dir"])
29 .current_dir(root)
30 .output()
31 .ok()?;
32 if !out.status.success() {
33 return None;
34 }
35 let common = PathBuf::from(String::from_utf8_lossy(&out.stdout).trim());
36 let main = common.parent()?.to_path_buf();
37 let same = main.canonicalize().ok()? == root.canonicalize().ok()?;
38 (!same).then_some(main)
39}
40
41pub fn load(root: &Path) -> Option<RepoRules> {
43 let mut roots = vec![root.to_path_buf()];
44 roots.extend(main_checkout(root));
45 for name in RULE_FILES {
46 for base in &roots {
47 let path = base.join(name);
48 let Ok(text) = std::fs::read_to_string(&path) else {
49 continue;
50 };
51 let text = text.trim();
52 if text.is_empty() {
53 continue;
54 }
55 let text = if text.chars().count() > MAX_RULES_CHARS {
56 let cut: String = text.chars().take(MAX_RULES_CHARS).collect();
57 format!("{cut}\n\n[cut at {MAX_RULES_CHARS} characters; the rest is in {name}]")
58 } else {
59 text.to_string()
60 };
61 return Some(RepoRules { source: path, text });
62 }
63 }
64 None
65}
66
67pub fn prompt_section(root: &Path) -> String {
69 match load(root) {
70 Some(r) => format!(
71 "## Repository rules\nThe repository's own rules for agents, from `{}`. Follow them \
72 unless the task says otherwise; where they name a check command, it is the same \
73 check that decides completion.\n\n{}\n\n",
74 r.source.display(),
75 r.text
76 ),
77 None => String::new(),
78 }
79}