Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(host-core): align FILE_NOT_FOUND tests and Edit mapping
ZxlDragonDoctor102a536c965b
diff --git a/crates/host-core/src/tools/mod.rs b/crates/host-core/src/tools/mod.rsindex 2b4fb13b3..27592bc54 100644--- a/crates/host-core/src/tools/mod.rs+++ b/crates/host-core/src/tools/mod.rs@@ -1,4 +1,4 @@-use anyhow::{anyhow, Result};+use anyhow::{anyhow, Result};use ignore::WalkBuilder;use regex::RegexBuilder;use serde::{Deserialize, Serialize};@@ -1310,12 +1310,6 @@ fn tool_write(std::fs::create_dir_all(parent).map_err(|e| ("TOOL_FAILED".into(), format!("mkdir failed: {e}")))?;}- if !resolved.parent().map(|p| p.exists()).unwrap_or(false) {- return Err((- "FILE_NOT_FOUND".into(),- format!("File not found: {path} (parent directory is missing)"),- ));- }std::fs::write(&resolved, &content).map_err(|e| {if e.kind() == std::io::ErrorKind::NotFound {("FILE_NOT_FOUND".into(), format!("File not found: {path}"))@@ -1363,8 +1357,13 @@ fn tool_edit(let (resolved, root_kind) =resolve_tool_path_with_external(root, scratch, path, allow_external_paths).map_err(|e| hashline::ToolError::new(e.clone(), e))?;- let live = std::fs::read(&resolved)- .map_err(|e| hashline::ToolError::new("TOOL_FAILED", format!("read failed: {e}")))?;+ let live = std::fs::read(&resolved).map_err(|e| {+ if e.kind() == std::io::ErrorKind::NotFound {+ hashline::ToolError::new("FILE_NOT_FOUND", format!("File not found: {path}"))+ } else {+ hashline::ToolError::new("TOOL_FAILED", format!("read failed: {e}"))+ }+ })?;let display = display_tool_path(root_kind, root, &resolved);let canonical = hashline::canonical_key(&resolved);let (file, success) = hashline::apply_edit(@@ -4102,12 +4101,18 @@ mod tests {.await;assert!(!result.ok, "read of missing file must fail");assert_eq!(result.error_code.as_deref(), Some("FILE_NOT_FOUND"));- let msg = result.content["message"].as_str().unwrap_or_default();- assert!(msg.contains("no-such-file.txt"), "diagnostic should name the path: {msg}");+ // execute_tool_with_path_access serializes tool errors under content["error"]+ let msg = result.content["error"].as_str().unwrap_or_default();+ assert!(+ msg.contains("no-such-file.txt"),+ "diagnostic should name the path: {msg}"+ );}#[tokio::test]- async fn write_missing_parent_reports_file_not_found() {+ async fn write_creates_missing_parent_directories() {+ // Write intentionally creates missing parent dirs (create_dir_all),+ // so a missing parent is not FILE_NOT_FOUND.let dir = tempfile::tempdir().unwrap();let result = execute_tool(Some(dir.path()),@@ -4117,9 +4122,36 @@ mod tests {5_000,).await;- assert!(!result.ok, "write under missing parent must fail");+ assert!(+ result.ok,+ "write under missing parent should create dirs: {:?}",+ result.content+ );+ assert!(dir.path().join("no/such/dir/file.txt").is_file());+ }++ #[tokio::test]+ async fn edit_missing_file_reports_file_not_found() {+ let dir = tempfile::tempdir().unwrap();+ let result = execute_tool(+ Some(dir.path()),+ None,+ "Edit",+ &serde_json::json!({+ "path": "no-such-file.txt",+ "tag": "abcd",+ "ops": "..."+ }),+ 5_000,+ )+ .await;+ assert!(!result.ok, "edit of missing file must fail");assert_eq!(result.error_code.as_deref(), Some("FILE_NOT_FOUND"));- let msg = result.content["message"].as_str().unwrap_or_default();- assert!(msg.contains("no/such/dir/file.txt"), "diagnostic should name the path: {msg}");+ let msg = result.content["error"].as_str().unwrap_or_default();+ assert!(+ msg.contains("no-such-file.txt"),+ "diagnostic should name the path: {msg}"+ );}}+diff --git a/packages/shared/src/errors.ts b/packages/shared/src/errors.tsindex ef1ad4007..78c27a6ae 100644--- a/packages/shared/src/errors.ts+++ b/packages/shared/src/errors.ts@@ -1,4 +1,4 @@-export type AppError = {+export type AppError = {code: string;message: string;details?: unknown;@@ -66,7 +66,7 @@ export const ErrorCodes = {FILE_NOT_FOUND: "FILE_NOT_FOUND",/*** The mutation recovery guard stopped the turn after repeated same-path Edit- * or patch-command failures (spec 18-line-anchored-edit-contract 搂9.3). Retriable: the user may continue.+ * or patch-command failures (spec 18-line-anchored-edit-contract §9.3). Retriable: the user may continue.*/MUTATION_RETRY_BUDGET_EXHAUSTED: "MUTATION_RETRY_BUDGET_EXHAUSTED",PROCESS_RESOURCE_EXHAUSTED: "PROCESS_RESOURCE_EXHAUSTED",@@ -131,3 +131,4 @@ export const ErrorCodes = {PLUGIN_INVALID: "PLUGIN_INVALID",PLUGIN_LOAD_FAILED: "PLUGIN_LOAD_FAILED",} as const;+