docs: tambah doc comment, logging, dan inline comments di semua 255 file

Meliputi:
- File-level //! doc comment: tujuan file, alur kerja, komponen utama
- Function-level /// doc comment: apa, parameter, return, flow, edge cases
- Struct/enum/trait /// doc comment: peran, field docs
- Tracing logging (tracing::info!/debug!/trace!/warn!/error!) di setiap fungsi
- Inline comments untuk variable dan branching logic penting
- Seluruh 8 crates di workspace: zesdex-backend, zesdex-cms, zesdex-entities,
  zesdex-iam, zesdex-infra, zesdex-ipc, zesdex-middleware, zesdex-utils
- Build: 0 errors, 242/242 tests passed
This commit is contained in:
asepharyana
2026-07-19 17:05:47 +07:00
parent 6680795ce7
commit 5aaedbf787
255 changed files with 5666 additions and 743 deletions
+17 -4
View File
@@ -1,10 +1,16 @@
//! Bash-shell execution tool with safety filters and optional timeout.
//!
//! This module implements the `bash` tool, which runs a shell command via
//! `bash -c <command>`. It supports foreground and background execution,
//! configurable timeouts, and destructive-git-operation gating via
//! `shell_filter::git::check_git_destructive`.
use super::Tool;
use super::ToolCtx;
use anyhow::{anyhow, Result};
use serde_json::{json, Value};
use std::process::Command;
use std::time::Duration;
use tracing;
/// Tool that runs `bash -c <command>`, optionally in the background, with safety
/// filters applied before spawning.
@@ -68,8 +74,9 @@ impl Tool for Bash {
let timeout_ms = args
.get("timeout")
.and_then(serde_json::Value::as_u64)
.unwrap_or(120_000)
.min(600_000);
.unwrap_or(120_000) // default: 2 minutes
.min(600_000); // max: 10 minutes
tracing::debug!(cmd_len = cmd.len(), timeout = timeout_ms, "Bash::run invoked");
// Only gate destructive git operations; credential reads are allowed
// locally since the AI needs access, and the real threat is committing
// secrets to a public repo (handled by git pre-commit hooks / user).
@@ -80,9 +87,11 @@ impl Tool for Bash {
.and_then(serde_json::Value::as_bool)
.unwrap_or(false);
if run_in_background {
tracing::debug!("spawning background bash job");
let job = crate::app::bgbash::job::spawn_bash_job(cmd);
return Ok(format!("Background job: {}", job.id));
}
tracing::debug!("spawning foreground bash -c");
let mut child = Command::new("bash")
.arg("-c")
.arg(&cmd)
@@ -90,7 +99,7 @@ impl Tool for Bash {
.stderr(std::process::Stdio::piped())
.spawn()
.map_err(|e| anyhow!("failed to spawn bash: {e}"))?;
let start = std::time::Instant::now();
let start = std::time::Instant::now(); // used for timeout check and elapsed reporting
let timeout = Duration::from_millis(timeout_ms);
loop {
match child.try_wait() {
@@ -108,12 +117,14 @@ impl Tool for Bash {
};
let trimmed = combined.trim().to_string();
if status.success() {
tracing::debug!(elapsed_secs = elapsed, "bash command succeeded");
return Ok(if trimmed.is_empty() {
format!("Command completed in {elapsed:.2}s (exit code 0)")
} else {
format!("{trimmed}\n\nExit code: 0 ({elapsed:.2}s)")
});
}
tracing::debug!(elapsed_secs = elapsed, exit_code = status.code().unwrap_or(-1), "bash command finished with non-zero exit");
return Ok(format!(
"{}\n\nExit code: {} ({:.2}s)",
trimmed,
@@ -123,13 +134,15 @@ impl Tool for Bash {
}
Ok(None) => {
if start.elapsed() > timeout {
tracing::warn!(timeout_ms = timeout_ms, "bash command timed out, killing");
let _ = child.kill();
let _ = child.wait();
anyhow::bail!("command timed out after {timeout_ms}ms");
}
std::thread::sleep(Duration::from_millis(10));
std::thread::sleep(Duration::from_millis(10)); // small sleep to avoid busy-wait
}
Err(e) => {
tracing::error!(error = %e, "bash command wait failed");
anyhow::bail!("failed to wait for command: {e}");
}
}