Merge pull request 'Guide models to read-only inspection' (#133) from feat/inspect-guidelines into main

This commit was merged in pull request #133.
This commit is contained in:
hermes committed 2026-10-06 13:14:44 +00:00
commit c29df05e0b
7 files changed
+70 -11

No files matched your search

+14 -1
View File
@@ -3,7 +3,12 @@
use std::path::{Path, PathBuf};
pub const DEFAULT_PROMPT: &str = "You are hx, a coding agent working in the user's repository. \
Use the tools to read, edit and run code. Read files before editing them. Keep answers short.";
Use the tools to read, edit and run code. Read files before editing them. Keep answers short.\n\n\
# Working rules\n\
- Inspect with the tools, not scripts: `read` for files, `rg` to search text, `fd` or `find` to list files, `ls`/`tree` for directories.\n\
- Never use node, python, perl, sed or awk to look at files or data; they need user approval every time. Read-only commands (ls cat head tail rg fd find grep wc jq sort, git status/log/diff/show) run without a prompt, also piped into each other.\n\
- One simple command per call: no `;`, `&&`, redirects, `$()`. Those need approval too.\n\
- Script only when the task needs it (build, test, edit), not to explore.";
const CONTEXT_FILES: &[&str] = &["AGENTS.md", "CLAUDE.md"];
@@ -190,6 +195,14 @@ pub fn system_prompt(base: Option<&str>, home: &Path, cwd: &Path) -> String {
mod tests {
use super::*;
#[test]
fn default_prompt_has_working_rules() {
assert!(
DEFAULT_PROMPT.contains("never use node") || DEFAULT_PROMPT.contains("Never use node")
);
assert!(DEFAULT_PROMPT.contains("`rg`"));
}
#[test]
fn prompt_assembly() {
let t = tempfile::tempdir().unwrap();
+22 -4
View File
@@ -9,10 +9,15 @@ pub fn gated(tool: &str) -> bool {
}
/// Read-only commands that run without a prompt (when not compound and no argument is a secret).
/// Not listed on purpose: find (-exec/-delete), env, xargs, sed, cargo, curl.
/// Pipes are fine when every segment is read-only. Not listed on purpose: env, xargs, sed, cargo, curl.
const READONLY: &[&str] = &[
"ls", "cat", "head", "tail", "wc", "grep", "rg", "fd", "pwd", "echo", "stat", "file", "du",
"df", "which", "tree", "diff",
"df", "which", "tree", "diff", "sort", "cut", "nl", "tr", "jq", "find",
];
/// `find` actions that run or write; `sort -o` writes a file.
const WRITING_FLAGS: &[&str] = &[
"-exec", "-execdir", "-ok", "-okdir", "-delete", "-fprint", "-fprint0", "-fprintf", "-fls",
"-o", "--output",
];
const READONLY_GIT: &[&str] = &[
"status",
@@ -44,13 +49,18 @@ pub fn secret(path: &str) -> bool {
}
fn readonly_cmd(cmd: &str) -> bool {
// `||` yields an empty segment, which is rejected
!cmd.is_empty() && cmd.split('|').all(|seg| readonly_simple(seg.trim()))
}
fn readonly_simple(cmd: &str) -> bool {
let w: Vec<&str> = cmd.split_whitespace().collect();
let ok = match w.as_slice() {
[] => false,
["git", sub, ..] => READONLY_GIT.contains(sub),
[c, ..] => READONLY.contains(c),
[c, args @ ..] => READONLY.contains(c) && !args.iter().any(|a| WRITING_FLAGS.contains(a)),
};
ok && !compound(cmd) && !w.iter().any(|a| secret(a))
ok && !compound(&cmd.replace('|', "")) && !w.iter().any(|a| secret(a))
}
/// Does this call need a yes? Writes and unknown commands do; reads of non-secret files do not.
@@ -265,6 +275,14 @@ mod tests {
assert!(!ask("bash", "command", "git status"));
assert!(!ask("bash", "command", "grep -rn foo src"));
assert!(ask("bash", "command", "cat .env") && ask("bash", "command", "cat ~/.ssh/id_rsa"));
assert!(!ask("bash", "command", "rg foo src | head -5"));
assert!(!ask("bash", "command", "find . -name '*.rs' | sort | head"));
assert!(!ask("bash", "command", "cat a.json | jq .x"));
assert!(
ask("bash", "command", "find . -exec rm {} +") && ask("bash", "command", "sort -o f f")
);
assert!(ask("bash", "command", "ls || rm x") && ask("bash", "command", "ls | sh"));
assert!(ask("bash", "command", "node -e 1") && ask("bash", "command", "python3 -c 1"));
assert!(ask("bash", "command", "cat a | rm x") && ask("bash", "command", "ls > f"));
assert!(ask("bash", "command", "git push") && ask("bash", "command", "find . -delete"));
assert!(ask("bash", "command", "cargo test") && ask("bash", "command", ""));
+28
View File
@@ -135,6 +135,34 @@ pub fn report(s: &Session) -> String {
call_b / 4
);
// bash calls that ask for approval, by first word: shows which habits break the read-only flow
let mut asks: HashMap<String, u32> = HashMap::new();
for m in msgs.iter().filter(|m| m.role == Role::Assistant) {
for b in &m.content {
if let ContentBlock::ToolUse { name, input, .. } = b
&& name == "bash"
&& crate::permit::needs_permit(name, input)
{
{
let c = input["command"].as_str().unwrap_or_default();
*asks
.entry(c.split_whitespace().next().unwrap_or("").into())
.or_default() += 1;
}
}
}
}
if !asks.is_empty() {
let mut v: Vec<_> = asks.into_iter().collect();
v.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0)));
let list: Vec<String> = v.iter().take(8).map(|(c, n)| format!("{c} {n}")).collect();
let _ = writeln!(
o,
"approval prompts from bash, by command: {}",
list.join(", ")
);
}
// carried cost of tool results on the current path
let mut names: HashMap<&str, &str> = HashMap::new();
for m in &msgs {
+1 -1
View File
@@ -316,7 +316,7 @@ impl Tool for Bash {
fn spec(&self) -> ToolSpec {
spec(
"bash",
"Run a shell command in the working directory. stdout and stderr are merged. Long output keeps the start and the end; the middle is cut and the full output saved to a file.",
"Run a shell command in the working directory. Read-only commands (ls cat head tail rg fd find grep wc jq sort, git status/log/diff/show, piped together) run at once; everything else, e.g. node, python, sed, cargo, asks the user first. To inspect files prefer read/rg/fd. stdout and stderr are merged. Long output keeps the start and the end; the middle is cut and the full output saved to a file.",
json!({"type":"object","properties":{
"command":{"type":"string"},
"timeout":{"type":"integer","description":"seconds, default 120"}},