From 3b1351018d3184caa5b5ca25cc13e5da1fc02229 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 15:41:46 +0100 Subject: 0.4.5: CLI validation, instruction scope, resume context, prompt hygiene - session ids validated; missing flag values and conflicting flags are usage errors - -- terminator; usage goes to stderr - doctor sends a real session header - sessions record their starting cwd and warn when resumed elsewhere - AGENTS.md stops at the git root; no parent-directory injection - typeahead flush, Ctrl-C during prompts, NO_COLOR/TERM=dumb --- src/agent.rs | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) (limited to 'src/agent.rs') 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}; /// Frozen for the life of the session. Anything session-varying added here /// invalidates the provider's prompt cache, so keep it boring. pub fn system_prompt() -> Value { + let cwd = std::env::current_dir().unwrap_or_default(); + system_prompt_for(&cwd) +} + +pub fn system_prompt_for(cwd_path: &std::path::Path) -> Value { + let cwd = cwd_path.display().to_string(); let mut text = String::from( "You are agent686, a coding agent in a Unix terminal.\n\n\ Tools: read, write, edit, bash, grep, glob. Paths are relative to the working \ @@ -27,9 +33,6 @@ pub fn system_prompt() -> Value { - Do not explain what you are about to do. Act, then report briefly.\n\n", ); - let cwd = std::env::current_dir() - .map(|p| p.display().to_string()) - .unwrap_or_default(); text.push_str(&format!( "Environment: cwd={cwd} · {} · {}", std::env::consts::OS, @@ -40,7 +43,7 @@ pub fn system_prompt() -> Value { } text.push('\n'); - if let Some((path, content)) = project_instructions() { + if let Some((path, content)) = project_instructions(cwd_path) { if !content.trim().is_empty() { text.push_str(&format!( "\n\n{}\n\n", @@ -54,18 +57,43 @@ pub fn system_prompt() -> Value { } pub fn agents_md() -> Option<(PathBuf, SystemTime)> { - let path = project_instructions().map(|(path, _)| path)?; + let cwd = std::env::current_dir().ok()?; + let path = project_instructions(&cwd).map(|(path, _)| path)?; let modified = std::fs::metadata(&path).ok()?.modified().ok()?; Some((path, modified)) } -fn project_instructions() -> Option<(PathBuf, String)> { - let mut dir = std::env::current_dir().ok()?; +/// Instructions from the project only: stop at the git root, and do not walk +/// above it. A stray `AGENTS.md` in a shared parent is an instruction-injection +/// surface, not a convenience. +fn project_instructions(start: &std::path::Path) -> Option<(PathBuf, String)> { + let root = git_root(start); + let mut dir = start.to_path_buf(); loop { let candidate = dir.join("AGENTS.md"); if candidate.is_file() { - let content = std::fs::read_to_string(&candidate).ok()?; - return Some((candidate, content)); + if let Ok(content) = std::fs::read_to_string(&candidate) { + return Some((candidate, content)); + } + // Unreadable or not UTF-8: keep looking upwards, do not give up. + } + // Outside a repository, only the working directory itself counts: + // walking up would pick files from unrelated parents. + let stop = match &root { + Some(root) => dir == *root, + None => true, + }; + if stop || !dir.pop() { + return None; + } + } +} + +fn git_root(start: &std::path::Path) -> Option { + let mut dir = start.to_path_buf(); + loop { + if dir.join(".git").exists() { + return Some(dir); } if !dir.pop() { return None; -- cgit v1.2.3