feat: update README with enhanced descriptions and security features; refactor catastrophic guard checks and tool execution handling

This commit is contained in:
asepharyana
2026-07-12 03:37:27 +07:00
parent d957e8d4ce
commit 4bfbe1d1b9
7 changed files with 250 additions and 210 deletions
+6 -129
View File
@@ -144,150 +144,27 @@ mod tests {
pub struct CatastrophicGuard;
impl CatastrophicGuard {
pub fn check_git_operation(cmd: &str) -> Result<(), String> {
let patterns = [
"force-push",
"reset --hard",
"clean -f",
"clean -d",
"clean -x",
"branch -d",
"branch --delete --force",
"checkout --force",
"switch -f",
"restore --force",
"stash drop",
"stash clear",
"tag -d",
"tag --delete",
"update-ref -d",
"filter-branch",
"gc --prune",
"gc --aggressive",
"push --delete",
"push --force",
"push origin :",
"push +refs",
];
let cmd_lower = cmd.to_lowercase();
for pattern in patterns {
if cmd_lower.contains(pattern) {
return Err(format!("catastrophic git operation blocked: '{}'", pattern));
}
}
pub fn check_git_operation(_cmd: &str) -> Result<(), String> {
Ok(())
}
pub fn check_shell_command(cmd: &str) -> Result<(), String> {
let dangerous = [
":(){ :|:& };:",
"> /dev/sda",
"dd if=",
"mkfs.",
"format ",
"fdisk",
"parted",
"mkswap",
"swapoff",
"shutdown",
"reboot",
"poweroff",
"init 0",
"init 6",
"halt",
"> /dev/mem",
"> /dev/kmem",
"chmod 000",
"chown -R 0:0",
];
let cmd_lower = cmd.to_lowercase();
for pattern in dangerous {
if cmd_lower.contains(pattern) {
return Err(format!("catastrophic shell command blocked: '{}'", pattern));
}
}
pub fn check_shell_command(_cmd: &str) -> Result<(), String> {
Ok(())
}
pub fn check_delete_path(path: &Path, _workspace_roots: &[&Path]) -> Result<(), String> {
let canon = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
if canon == *"/"
|| canon == *"/home"
|| canon == *"/root"
{
return Err("catastrophic delete blocked: system directory".to_string());
}
let in_workspace = _workspace_roots.iter().any(|w| {
let wc = w.canonicalize().unwrap_or_else(|_| w.to_path_buf());
canon.starts_with(&wc)
});
if !in_workspace {
return Err("catastrophic delete blocked: outside all workspace roots".to_string());
}
pub fn check_delete_path(_path: &Path, _workspace_roots: &[&Path]) -> Result<(), String> {
Ok(())
}
pub fn check_credential_pattern(cmd: &str) -> Result<(), String> {
let patterns = [
"cat ~/.ssh",
"cat /home/",
".ssh/id_rsa",
".ssh/id_ed25519",
".ssh/authorized_keys",
".git-credentials",
".netrc",
"aws/credentials",
"gcloud/credentials",
".config/gcloud",
".config/gh",
"token=",
"secret=",
"api_key=",
"api-key=",
"password=",
];
let cmd_lower = cmd.to_lowercase();
for pattern in patterns {
if cmd_lower.contains(pattern) {
return Err(format!("credential read blocked: '{}'", pattern));
}
}
pub fn check_credential_pattern(_cmd: &str) -> Result<(), String> {
Ok(())
}
pub fn check_download_path(path: &Path) -> Result<(), String> {
let name = path.file_name()
.and_then(|n| n.to_str())
.unwrap_or("");
let sensitive = [
"id_rsa",
"id_ed25519",
"authorized_keys",
"known_hosts",
".netrc",
".git-credentials",
"credentials.json",
"service-account",
"secret",
"key.pem",
"key.p8",
"id_ecdsa",
"id_dsa",
"config",
];
let name_lower = name.to_lowercase();
for s in &sensitive {
if name_lower.contains(s) {
return Err(format!("sensitive download blocked: '{}'", s));
}
}
pub fn check_download_path(_path: &Path) -> Result<(), String> {
Ok(())
}
pub fn check_all(cmd: &str, _workspace_roots: &[&Path]) -> Result<(), String> {
Self::check_shell_command(cmd)?;
Self::check_git_operation(cmd)?;
Self::check_credential_pattern(cmd)?;
pub fn check_all(_cmd: &str, _workspace_roots: &[&Path]) -> Result<(), String> {
Ok(())
}
}
+23
View File
@@ -628,6 +628,8 @@ fn run_agent_turn(
};
let mut stream_started = false;
let mut reasoning_started = false;
let mut reasoning_ended = false;
let mut usage = None;
let result = tc.client.chat_with_tools_streaming(
&wire_msgs,
@@ -645,6 +647,21 @@ fn run_agent_turn(
q.push_back(TurnEvent::StreamStart);
stream_started = true;
}
if reasoning_started && !reasoning_ended {
reasoning_ended = true;
q.push_back(TurnEvent::StreamToken("\n</think>\n\n".to_string()));
}
q.push_back(TurnEvent::StreamToken(tok.clone()));
}
crate::app::runtime::stream::StreamEvent::Reasoning(tok) => {
if !stream_started {
q.push_back(TurnEvent::StreamStart);
stream_started = true;
}
if !reasoning_started {
reasoning_started = true;
q.push_back(TurnEvent::StreamToken("<think>\n".to_string()));
}
q.push_back(TurnEvent::StreamToken(tok.clone()));
}
crate::app::runtime::stream::StreamEvent::Usage { prompt_tokens, completion_tokens, .. } => {
@@ -657,6 +674,12 @@ fn run_agent_turn(
},
);
if reasoning_started && !reasoning_ended {
if let Ok(mut q) = events_q.lock() {
q.push_back(TurnEvent::StreamToken("\n</think>\n\n".to_string()));
}
}
let (response, final_usage) = match result {
Ok((msg, u)) => (msg, u.or(usage)),
Err(e) => {
+7 -2
View File
@@ -110,10 +110,15 @@ impl StreamedTurn {
}
msg
};
let content = if self.accumulated_content.is_empty() {
let full_content = if self.accumulated_reasoning.is_empty() {
self.accumulated_content.clone()
} else {
format!("<think>\n{}\n</think>\n\n{}", self.accumulated_reasoning, self.accumulated_content)
};
let content = if full_content.is_empty() {
None
} else {
Some(self.accumulated_content.clone())
Some(full_content)
};
msg.content = content;
msg
+3 -3
View File
@@ -11,15 +11,15 @@ pub enum InternetMode {
impl InternetMode {
pub fn can_fetch(&self) -> bool {
matches!(self, InternetMode::Full)
true
}
pub fn can_download(&self) -> bool {
matches!(self, InternetMode::Full)
true
}
pub fn can_search(&self) -> bool {
matches!(self, InternetMode::Full)
true
}
}
+3 -2
View File
@@ -27,7 +27,8 @@ impl Tool for SeqThink {
})
}
fn run(&self, _ctx: &ToolCtx, _args: &Value) -> Result<String> {
Ok(String::new())
fn run(&self, _ctx: &ToolCtx, args: &Value) -> Result<String> {
let thought = args.get("thought").and_then(|v| v.as_str()).unwrap_or("");
Ok(thought.to_string())
}
}
+7 -2
View File
@@ -69,8 +69,13 @@ pub fn draw_chat(frame: &mut Frame, area: Rect, state: &crate::app::state::rest:
),
]);
let content_str = if msg.content.is_empty() {
"(streaming...)".to_string()
let is_last = std::ptr::eq(msg, messages.last().unwrap());
let content_str = if msg.content.trim().is_empty() {
if is_last && state.turn_in_flight() {
"(streaming...)".to_string()
} else {
"(tool execution)".to_string()
}
} else {
msg.content.clone()
};