rpi-tools 0.27.0

Built-in agent tools (read/write/edit/bash/grep/find/ls) + ExecutionEnv, Rust port of pi harness tools
Documentation
//! Mirrors `packages/agent/src/harness/tools/write.ts` — the built-in `write`
//! `AgentTool`. Writes content to a file (creating parent dirs), serialized per
//! canonical path via the file-mutation-queue, with pre/post abort bracketing.
//!
//! Invariant (plan §5.4): abort does NOT unblock the queue — the caller still
//! waits for its turn, then the closure observes the token and returns
//! `Err(Aborted)`. The pre/post abort checks around `write_file` mirror the TS
//! `if (signal?.aborted) throw` lines.

use std::sync::Arc;

use async_trait::async_trait;
use futures::future::BoxFuture;
use futures::FutureExt;
use rpi_agent::agent_tool::AgentTool;
use rpi_agent::error::AgentError;
use rpi_agent::types::{AgentToolResult, TextContentOrImage, ToolResultPartial};
use rpi_ai::types::{ConstrainedSamplingConfig, ConstrainedStrictness, Tool};
use schemars::JsonSchema;
use serde::Deserialize;
use tokio_util::sync::CancellationToken;

use crate::env::FileContent;
use crate::error::{FileError, FileErrorCode};
use crate::file_io::{
    resolve_client_path, with_tool_mutation_queue, FileIOOperation, FileIORouter,
};
use crate::file_mutation_queue::MutatingEnv;
use crate::path_utils::resolve_tool_path;
use crate::tools::edit_diff::{
    generate_diff_string_budgeted, generate_unified_patch_budgeted, DIFF_TIME_BUDGET,
};
use crate::tools::tool_context::ExecutionToolContext;

/// Input for the write tool. Mirrors TS `WriteToolInput`.
#[derive(Debug, Clone, JsonSchema, Deserialize)]
pub struct WriteInput {
    /// Path to the file to write (relative or absolute).
    pub path: String,
    /// Content to write to the file.
    pub content: String,
}

/// The built-in `write` tool. Holds the mutating env (for the queue + writes).
pub struct WriteTool {
    schema: Tool,
    env: Arc<dyn MutatingEnv>,
    file_io: Option<Arc<FileIORouter>>,
}

impl WriteTool {
    fn schema() -> Tool {
        let params = schemars::schema_for!(WriteInput);
        Tool {
            name: "write".to_string(),
            description: "Write content to a file. Creates the file if it doesn't exist, \
                          overwrites if it does. Automatically creates parent directories."
                .to_string(),
            parameters: rpi_ai::types::Schema::new(
                serde_json::to_value(params).unwrap_or_default(),
            ),
            constrained_sampling: Some(ConstrainedSamplingConfig::JsonSchema {
                strict: ConstrainedStrictness::Prefer,
            }),
        }
    }
}

/// Build the `write` tool from a context. Requires a mutation-capable env.
/// Mirrors TS `createWriteTool`.
pub fn create_write_tool(context: &ExecutionToolContext) -> Arc<dyn AgentTool> {
    let env = context
        .mutating_env()
        .expect("write tool requires a mutation-capable ExecutionToolContext")
        .clone();
    Arc::new(WriteTool {
        schema: WriteTool::schema(),
        env,
        file_io: context.file_io().cloned(),
    })
}

#[async_trait]
impl AgentTool for WriteTool {
    fn schema(&self) -> &Tool {
        &self.schema
    }
    fn label(&self) -> &str {
        "write"
    }

    async fn execute(
        &self,
        _tool_call_id: &str,
        params: serde_json::Value,
        signal: CancellationToken,
        _on_update: Arc<dyn Fn(ToolResultPartial) + Send + Sync>,
    ) -> Result<AgentToolResult, AgentError> {
        let input: WriteInput = serde_json::from_value(params)
            .map_err(|e| AgentError::Validation(format!("write input invalid: {e}")))?;
        let path_for_msg = input.path.clone();
        let content = input.content;

        // Freeze authority before any local path resolution or queue access.
        let client = match &self.file_io {
            Some(router) => router
                .select(FileIOOperation::Write)
                .await
                .map_err(|e| file_err_to_agent(e.into()))?,
            None => None,
        };
        let abs = if client.is_some() {
            resolve_client_path(self.env.as_env().cwd(), &path_for_msg)
                .map_err(|e| file_err_to_agent(e.into()))?
        } else {
            resolve_tool_path(self.env.as_env(), &path_for_msg, Some(&signal))
                .await
                .map_err(file_err_to_agent)?
        };
        let abs_str = abs;
        let client_selected = client.is_some();

        // The pre-image read and the write run in the *same* per-path mutation
        // queue: the diff must describe exactly the bytes this call replaced,
        // not a racing snapshot.
        let env = self.env.clone();
        let env_ref = env.clone();
        let abs_for_closure = abs_str.clone();
        let write_content = content.clone();
        let pre_image: Result<PreImage, FileError> = with_tool_mutation_queue(
            if client_selected {
                self.file_io.as_deref()
            } else {
                None
            },
            &*env_ref,
            &abs_str,
            &signal,
            move |cancel: &CancellationToken| -> BoxFuture<'_, Result<PreImage, FileError>> {
                let env = env.clone();
                let client = client.clone();
                let abs_str = abs_for_closure.clone();
                let content = write_content.clone();
                async move {
                    check_abort(cancel)?;
                    // Absent file = empty pre-image (a real "create" diff).
                    // A read that fails for any other reason is surfaced as
                    // `diff_skipped` in details; the write itself proceeds
                    // (the pre-image is metadata, not part of the operation).
                    let pre = if client.is_some() {
                        // Client-owned files: the write transport has no
                        // read-pre-image contract yet, so report the diff as
                        // unavailable instead of paying an extra round-trip
                        // or fabricating one from the new content.
                        PreImage {
                            text: None,
                            read_error: Some("client_backend".to_string()),
                        }
                    } else {
                        match env.as_env().file_info(&abs_str, Some(cancel)).await {
                            Ok(info) if info.size as usize <= MAX_DIFF_INPUT_BYTES => {
                                match env.as_env().read_text_file(&abs_str, Some(cancel)).await {
                                    Ok(text) => PreImage {
                                        text: Some(text),
                                        read_error: None,
                                    },
                                    Err(e) => PreImage {
                                        text: None,
                                        read_error: Some(e.code.as_str().to_string()),
                                    },
                                }
                            }
                            Ok(_) => PreImage {
                                text: None,
                                read_error: Some("content_too_large".to_string()),
                            },
                            Err(e) if e.code == FileErrorCode::NotFound => PreImage {
                                text: None,
                                read_error: None,
                            },
                            Err(e) => PreImage {
                                text: None,
                                read_error: Some(e.code.as_str().to_string()),
                            },
                        }
                    };
                    check_abort(cancel)?;
                    if let Some(client) = &client {
                        client
                            .write_text(&abs_str, content, cancel)
                            .await
                            .map_err(FileError::from)?;
                    } else {
                        env.as_env()
                            .write_file(&abs_str, FileContent::Text(content), Some(cancel))
                            .await?;
                    }
                    check_abort(cancel)?;
                    Ok(pre)
                }
                .boxed()
            },
        )
        .await;

        let pre = pre_image.map_err(file_err_to_agent)?;
        // Diff generation is CPU-bound (two Myers passes). Run it on the
        // blocking pool so a large write never occupies an async worker, and
        // give each pass a hard wall-clock budget (spawn_blocking alone does
        // not bound runtime): over budget the result is an explicit
        // `diff_skipped: timeout`, never a suboptimal/fabricated diff.
        check_abort(&signal).map_err(file_err_to_agent)?;
        let path_for_diff = path_for_msg.clone();
        let diff_cancel = signal.clone();
        let pre_text = pre.text.clone();
        let pre_read_error = pre.read_error.clone();
        let new_text = content.clone();
        let details = tokio::task::spawn_blocking(move || {
            let pre = PreImage {
                text: pre_text,
                read_error: pre_read_error,
            };
            write_diff_details(&path_for_diff, &pre, &new_text, &diff_cancel)
        })
        .await
        .map_err(|e| AgentError::Tool(format!("write diff task failed: {e}")))?
        .map_err(file_err_to_agent)?;

        Ok(AgentToolResult {
            content: vec![TextContentOrImage::text(format!(
                "Successfully wrote to {path_for_msg}"
            ))],
            details,
            ..Default::default()
        })
    }
}

/// Pre-write snapshot used to build a real diff for the host.
struct PreImage {
    /// `None` = the file did not exist (a genuine create diff from "").
    text: Option<String>,
    /// Why the pre-image could not be read (non-NotFound). Hosts receive
    /// `diff_skipped` instead of a fabricated diff.
    read_error: Option<String>,
}

/// Cap for old+new content fed to the differ. Larger writes still succeed;
/// only `details.diff` is omitted (`diff_skipped: content_too_large`).
const MAX_DIFF_INPUT_BYTES: usize = 2 * 1024 * 1024;

/// Build the `details` payload. Mirrors the edit tool's shape (`diff` is the
/// display diff with line-number prefixes, `patch` is a unified patch) so hosts
/// render write rows with the same diff card as edit rows.
///
/// Budgeted + cancellable: both Myers passes get [`DIFF_TIME_BUDGET`]; a
/// budget violation becomes `diff_skipped: "timeout"`. Cancellation before or
/// between passes propagates as `Err(Aborted)`.
fn write_diff_details(
    path: &str,
    pre: &PreImage,
    new: &str,
    cancel: &CancellationToken,
) -> Result<serde_json::Value, FileError> {
    check_abort(cancel)?;
    if let Some(reason) = &pre.read_error {
        return Ok(serde_json::json!({ "diff_skipped": reason }));
    }
    let old = pre.text.as_deref().unwrap_or("");
    if old.len().saturating_add(new.len()) > MAX_DIFF_INPUT_BYTES {
        return Ok(serde_json::json!({ "diff_skipped": "content_too_large" }));
    }
    let Some(diff) = generate_diff_string_budgeted(old, new, 4, DIFF_TIME_BUDGET) else {
        return Ok(serde_json::json!({ "diff_skipped": "timeout" }));
    };
    check_abort(cancel)?;
    let Some(patch) = generate_unified_patch_budgeted(path, old, new, DIFF_TIME_BUDGET) else {
        return Ok(serde_json::json!({ "diff_skipped": "timeout" }));
    };
    check_abort(cancel)?;
    Ok(serde_json::json!({
        "diff": diff.diff,
        "patch": patch,
        "first_changed_line": diff.first_changed_line,
    }))
}

/// Check the cancel token, returning `Err(FileError::aborted())` when set.
/// Mirrors the TS `if (signal?.aborted) throw new Error("Operation aborted")`.
fn check_abort(cancel: &CancellationToken) -> Result<(), FileError> {
    if cancel.is_cancelled() {
        return Err(FileError::new(FileErrorCode::Aborted, "Operation aborted"));
    }
    Ok(())
}

fn file_err_to_agent(e: FileError) -> AgentError {
    AgentError::Tool(e.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn write_diff_details_aborts_before_compute_when_cancelled() {
        let cancel = CancellationToken::new();
        cancel.cancel();
        let pre = PreImage {
            text: Some("old\n".to_string()),
            read_error: None,
        };
        let err = write_diff_details("note.txt", &pre, "new\n", &cancel)
            .expect_err("cancelled diff must abort");
        assert_eq!(err.code, FileErrorCode::Aborted);
    }
}