use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use crate::rules::MAX_RULES_CHARS;
pub const PREAMBLE: &str = "\
You are the orchestrator in a repository. You plan the work, dispatch implementers with \
`blyte dispatch`, review what they did with `blyte review`, record a verdict, and merge. You do not \
write the implementation yourself, except for a one-line fix; every other change goes to a dispatch.
When the request names a plan, run `blyte plan <file>` first. It lists every task with its state \
(merged, review, running, dead, ready, waiting) and, for each ready task, the exact `blyte dispatch` \
command, already checked against the plan. Dispatch the ready tasks with those commands as given and \
review the ones in `review`. Do not read blyte's source, `.wt/` files or old runs, and do not run \
`blyte --version` or `which blyte` to find out how dispatch works: `--plan-task \"Task <ID>\"` selects the \
section headed `### Task <ID>: …`, and dispatch refuses a heading it cannot find. Run `blyte plan` \
again after each merge to see what became ready.
You work with a person. When you need a decision that is theirs to make, or you have a plan for them \
to approve, end your reply with the question and stop: their answer arrives as the next message. Do not \
ask about choices with a conventional default; pick it, say so, and carry on.
Keep tool output small, since everything you run stays in the conversation: send dispatch output to \
`/dev/null` (each run's summary is in `.wt/<id>.json`), wait with `blyte wait`, read `blyte review <id>` \
before any `--diff` and open the diff only for what it flags, and trim long output with `| tail -n 40` or \
`grep`. When you need a detail from earlier that is no longer in view, re-read it from the plan file or \
`.wt/<id>.task.md` rather than guessing.
Start on the work at once. The skill below documents every blyte command you need, so do not run \
`--help`, and do not check the environment or `~/.config/blyte` for API keys: `blyte dispatch` reports a \
missing key itself. When the request names a plan, read it and dispatch its independent tasks in your \
first turns.";
pub fn user_rules_path() -> Option<PathBuf> {
user_rules_path_from(
std::env::var_os("XDG_CONFIG_HOME").as_deref(),
std::env::var_os("HOME").as_deref(),
)
}
fn user_rules_path_from(xdg: Option<&OsStr>, home: Option<&OsStr>) -> Option<PathBuf> {
if let Some(xdg) = xdg.filter(|s| !s.is_empty()) {
Some(PathBuf::from(xdg).join("blyte").join("orchestrator.md"))
} else {
home.map(|home| {
PathBuf::from(home)
.join(".config")
.join("blyte")
.join("orchestrator.md")
})
}
}
pub fn load_user_rules(path: &Path) -> Option<String> {
let text = std::fs::read_to_string(path).ok()?;
let text = text.trim();
if text.is_empty() {
return None;
}
if text.chars().count() > MAX_RULES_CHARS {
let cut: String = text.chars().take(MAX_RULES_CHARS).collect();
Some(format!(
"{cut}\n\n[cut at {MAX_RULES_CHARS} characters; the rest is in {}]",
path.display()
))
} else {
Some(text.to_string())
}
}
pub fn prompt_sections(skill: &str, user_rules: Option<(&Path, &str)>) -> String {
let skill = skill.trim();
if let Some((path, text)) = user_rules {
format!(
"## How you work\n{PREAMBLE}\n\n## The blyte-implementer skill\n{skill}\n\n## Your rules (from {})\n{text}\n\n",
path.display()
)
} else {
format!("## How you work\n{PREAMBLE}\n\n## The blyte-implementer skill\n{skill}\n\n")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn user_rules_path_prefers_xdg() {
let path = user_rules_path_from(
Some(OsStr::new("/custom/config")),
Some(OsStr::new("/home/user")),
);
assert_eq!(
path,
Some(PathBuf::from("/custom/config/blyte/orchestrator.md"))
);
}
#[test]
fn user_rules_path_falls_back_to_home() {
let path = user_rules_path_from(None, Some(OsStr::new("/home/user")));
assert_eq!(
path,
Some(PathBuf::from("/home/user/.config/blyte/orchestrator.md"))
);
}
#[test]
fn user_rules_path_ignores_empty_xdg() {
let path = user_rules_path_from(Some(OsStr::new("")), Some(OsStr::new("/home/user")));
assert_eq!(
path,
Some(PathBuf::from("/home/user/.config/blyte/orchestrator.md"))
);
}
#[test]
fn user_rules_path_none_without_home() {
assert_eq!(user_rules_path_from(None, None), None);
assert_eq!(user_rules_path_from(Some(OsStr::new("")), None), None);
}
#[test]
fn load_missing_is_none() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("missing.md");
assert_eq!(load_user_rules(&path), None);
}
#[test]
fn load_blank_is_none() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("blank.md");
std::fs::write(&path, " \n\t \r\n ").unwrap();
assert_eq!(load_user_rules(&path), None);
}
#[test]
fn load_trims() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("rules.md");
std::fs::write(&path, "\n be brief \n").unwrap();
assert_eq!(load_user_rules(&path).as_deref(), Some("be brief"));
}
#[test]
fn load_caps_long_file() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("orchestrator.md");
let long_text = "é".repeat(MAX_RULES_CHARS + 10);
std::fs::write(&path, long_text).unwrap();
let result = load_user_rules(&path).unwrap();
let expected_prefix = format!("{}\n\n[cut at ", "é".repeat(MAX_RULES_CHARS));
assert!(result.starts_with(&expected_prefix));
let expected_suffix = format!("{}]", path.display());
assert!(result.ends_with(&expected_suffix));
}
#[test]
fn sections_in_order() {
let path = Path::new("/custom/orchestrator.md");
let s = prompt_sections(" SKILL BODY \n", Some((path, "RULES BODY")));
assert!(s.contains(PREAMBLE));
let pos1 = s.find("## How you work").unwrap();
let pos2 = s
.find("## The blyte-implementer skill\nSKILL BODY\n")
.unwrap();
let pos3 = s.find("## Your rules (from ").unwrap();
assert!(pos1 < pos2);
assert!(pos2 < pos3);
}
#[test]
fn sections_without_rules() {
let s = prompt_sections("S", None);
assert!(!s.contains("## Your rules"));
}
#[test]
fn preamble_names_the_commands() {
assert!(PREAMBLE.contains("blyte dispatch"));
assert!(PREAMBLE.contains("blyte review"));
assert!(PREAMBLE.contains("blyte wait"));
assert!(PREAMBLE.contains("/dev/null"));
}
#[test]
fn preamble_starts_from_blyte_plan() {
assert!(PREAMBLE.contains("blyte plan <file>"));
assert!(PREAMBLE.contains("Do not read blyte's source"));
}
}