summaryrefslogtreecommitdiffstats
path: root/src/agent.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/agent.rs')
-rw-r--r--src/agent.rs46
1 files changed, 37 insertions, 9 deletions
diff --git a/src/agent.rs b/src/agent.rs
index 77807ae..a8ad717 100644
--- a/src/agent.rs
+++ b/src/agent.rs
@@ -14,6 +14,12 @@ use crate::ui::{self, Approver, Renderer};
14/// Frozen for the life of the session. Anything session-varying added here 14/// Frozen for the life of the session. Anything session-varying added here
15/// invalidates the provider's prompt cache, so keep it boring. 15/// invalidates the provider's prompt cache, so keep it boring.
16pub fn system_prompt() -> Value { 16pub fn system_prompt() -> Value {
17 let cwd = std::env::current_dir().unwrap_or_default();
18 system_prompt_for(&cwd)
19}
20
21pub fn system_prompt_for(cwd_path: &std::path::Path) -> Value {
22 let cwd = cwd_path.display().to_string();
17 let mut text = String::from( 23 let mut text = String::from(
18 "You are agent686, a coding agent in a Unix terminal.\n\n\ 24 "You are agent686, a coding agent in a Unix terminal.\n\n\
19 Tools: read, write, edit, bash, grep, glob. Paths are relative to the working \ 25 Tools: read, write, edit, bash, grep, glob. Paths are relative to the working \
@@ -27,9 +33,6 @@ pub fn system_prompt() -> Value {
27 - Do not explain what you are about to do. Act, then report briefly.\n\n", 33 - Do not explain what you are about to do. Act, then report briefly.\n\n",
28 ); 34 );
29 35
30 let cwd = std::env::current_dir()
31 .map(|p| p.display().to_string())
32 .unwrap_or_default();
33 text.push_str(&format!( 36 text.push_str(&format!(
34 "Environment: cwd={cwd} · {} · {}", 37 "Environment: cwd={cwd} · {} · {}",
35 std::env::consts::OS, 38 std::env::consts::OS,
@@ -40,7 +43,7 @@ pub fn system_prompt() -> Value {
40 } 43 }
41 text.push('\n'); 44 text.push('\n');
42 45
43 if let Some((path, content)) = project_instructions() { 46 if let Some((path, content)) = project_instructions(cwd_path) {
44 if !content.trim().is_empty() { 47 if !content.trim().is_empty() {
45 text.push_str(&format!( 48 text.push_str(&format!(
46 "\n<project-instructions source=\"{}\">\n{}\n</project-instructions>\n", 49 "\n<project-instructions source=\"{}\">\n{}\n</project-instructions>\n",
@@ -54,18 +57,43 @@ pub fn system_prompt() -> Value {
54} 57}
55 58
56pub fn agents_md() -> Option<(PathBuf, SystemTime)> { 59pub fn agents_md() -> Option<(PathBuf, SystemTime)> {
57 let path = project_instructions().map(|(path, _)| path)?; 60 let cwd = std::env::current_dir().ok()?;
61 let path = project_instructions(&cwd).map(|(path, _)| path)?;
58 let modified = std::fs::metadata(&path).ok()?.modified().ok()?; 62 let modified = std::fs::metadata(&path).ok()?.modified().ok()?;
59 Some((path, modified)) 63 Some((path, modified))
60} 64}
61 65
62fn project_instructions() -> Option<(PathBuf, String)> { 66/// Instructions from the project only: stop at the git root, and do not walk
63 let mut dir = std::env::current_dir().ok()?; 67/// above it. A stray `AGENTS.md` in a shared parent is an instruction-injection
68/// surface, not a convenience.
69fn project_instructions(start: &std::path::Path) -> Option<(PathBuf, String)> {
70 let root = git_root(start);
71 let mut dir = start.to_path_buf();
64 loop { 72 loop {
65 let candidate = dir.join("AGENTS.md"); 73 let candidate = dir.join("AGENTS.md");
66 if candidate.is_file() { 74 if candidate.is_file() {
67 let content = std::fs::read_to_string(&candidate).ok()?; 75 if let Ok(content) = std::fs::read_to_string(&candidate) {
68 return Some((candidate, content)); 76 return Some((candidate, content));
77 }
78 // Unreadable or not UTF-8: keep looking upwards, do not give up.
79 }
80 // Outside a repository, only the working directory itself counts:
81 // walking up would pick files from unrelated parents.
82 let stop = match &root {
83 Some(root) => dir == *root,
84 None => true,
85 };
86 if stop || !dir.pop() {
87 return None;
88 }
89 }
90}
91
92fn git_root(start: &std::path::Path) -> Option<PathBuf> {
93 let mut dir = start.to_path_buf();
94 loop {
95 if dir.join(".git").exists() {
96 return Some(dir);
69 } 97 }
70 if !dir.pop() { 98 if !dir.pop() {
71 return None; 99 return None;