Skip to main content

aft/bash_rewrite/
mod.rs

1//! Bash command rewriter for hoisted bash.
2//!
3//! When the agent calls `bash("grep -n foo src/")`, the rewriter detects this
4//! pattern, dispatches internally to AFT's `grep` command, and returns the
5//! result with a footer hint nudging the agent to use the `grep` tool directly.
6
7pub mod dispatch;
8pub mod footer;
9pub mod parser;
10pub mod rules;
11
12use crate::context::AppContext;
13use crate::protocol::Response;
14use crate::sandbox_spawn::{native_sandbox_enforced, AuthenticatedPrincipal};
15
16/// A `RewriteRule` matches a specific bash invocation pattern and dispatches
17/// internally to an AFT tool.
18pub trait RewriteRule: Send + Sync {
19    fn name(&self) -> &'static str;
20    fn matches(&self, command: &str) -> bool;
21    fn rewrite(
22        &self,
23        command: &str,
24        session_id: Option<&str>,
25        ctx: &AppContext,
26    ) -> Result<Response, String>;
27}
28
29/// Try to rewrite a bash command into an internal AFT tool call.
30/// Returns `Some(response)` if rewritten and `None` when no rule matched or the
31/// command must execute inside the native sandbox.
32pub fn try_rewrite(
33    command: &str,
34    session_id: Option<&str>,
35    ctx: &AppContext,
36    principal: &AuthenticatedPrincipal,
37) -> Option<Response> {
38    if native_sandbox_enforced(ctx, principal) {
39        return None;
40    }
41    dispatch::dispatch(command, session_id, ctx)
42}