Craft Anything: understand how any software works, and craft your own.
Rust
List an edited-in-place project's own files (#996)
BFlat31f6ae653973
diff --git a/crates/engine/src/tools/build_tools.rs b/crates/engine/src/tools/build_tools.rsindex e7e557b7..87866e48 100644--- a/crates/engine/src/tools/build_tools.rs+++ b/crates/engine/src/tools/build_tools.rs@@ -42,7 +42,7 @@ pub fn specs() -> Vec<ToolSpec> {spec("build_files_list","List project files",- "List files and folders in the project being built (default: app/), a few levels deep. Paths are relative to the project folder; build output and dependencies (node_modules, target, dist…) are skipped.",+ "List files and folders in the app: app/ usually, or the project folder when the app is edited in place. A few levels deep; paths are relative to the project folder; build output and dependencies (node_modules, target, vendor, dist…) are skipped.","Shows the files being built.",Effects::READ_ONLY,schema_of::<ListParams>,@@ -99,7 +99,8 @@ pub fn specs() -> Vec<ToolSpec> {#[derive(Debug, Deserialize, JsonSchema)]#[serde(deny_unknown_fields)]pub struct ListParams {- /// A folder relative to the project (default `app`).+ /// A folder relative to the project (default: the app folder; for a project edited in+ /// place, the project folder itself).#[serde(default)]pub path: Option<String>,/// How many levels down (default 4, at most 8).@@ -207,7 +208,9 @@ fn in_project(ws: &Workspace, rel: &str) -> Result<PathBuf, ToolError> {}fn shown(ws: &Workspace, p: &Path) -> String {- p.strip_prefix(&ws.root).unwrap_or(p).to_string_lossy().replace('\\', "/")+ let root = resolve(&ws.root, ".").unwrap_or_else(|| ws.root.clone());+ let rel = p.strip_prefix(&root).unwrap_or(p);+ if rel.as_os_str().is_empty() { ".".to_string() } else { rel.to_string_lossy().replace('\\', "/") }}fn refused(why: String) -> ToolError {@@ -217,9 +220,13 @@ fn refused(why: String) -> ToolError {fn build_files_list(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, ToolError> {let p: ListParams = parse_params(params)?;let ws = workspace(ctx)?;- let root = in_project(&ws, p.path.as_deref().unwrap_or("app"))?;+ let default = if ws.manifest.app_in_root { "." } else { "app" };+ let root = in_project(&ws, p.path.as_deref().unwrap_or(default))?;+ // Our records are hidden from a listing of the project, but a listing of one of them (the+ // builder is told to read capture/ and reference/) shows what is in it.+ let listing_own = ws.is_own_record(&root);let depth = p.depth.unwrap_or(4).min(8);- const SKIP: [&str; 8] = ["node_modules", "target", "dist", "build", ".git", "Library", "Temp", "Intermediate"];+ const SKIP: [&str; 9] = ["node_modules", "target", "vendor", "dist", "build", ".git", "Library", "Temp", "Intermediate"];let mut entries = Vec::new();let mut stack = vec![(root.clone(), 0usize)];let mut truncated = false;@@ -232,6 +239,11 @@ fn build_files_list(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Toolbreak;}let path = e.path();+ // Craft Anything's own records are not the app, even when the app is the project+ // folder; the brief names them and the builder must leave them alone.+ if !listing_own && ws.is_own_record(&path) {+ continue;+ }let name = e.file_name().to_string_lossy().into_owned();// A link is listed but never followed: it may lead out of the project.if e.file_type().is_ok_and(|t| t.is_symlink()) {@@ -377,7 +389,7 @@ fn build_command_run(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Toomod tests {use super::*;use crate::inbox::InboxDir;- use crate::workspace::TargetRef;+ use crate::workspace::{ExistingFiles, TargetRef};use crate::{CallOptions, Caller, Consent, Engine};use std::sync::Arc;@@ -402,6 +414,7 @@ mod tests {call(&e, "build_file_edit", json!({"path": "app/src/main.rs", "old_text": "\"hi\"", "new_text": "\"hello\""})).unwrap();assert!(std::fs::read_to_string(ws.app_dir().join("src/main.rs")).unwrap().contains("hello"));let list = call(&e, "build_files_list", json!({})).unwrap();+ assert_eq!(list["root"], "app");assert!(list["entries"].to_string().contains("app/src/main.rs"), "{list}");let page = call(&e, "build_file_read", json!({"path": "app/src/main.rs", "offset": 1, "limit": 1})).unwrap();assert_eq!(page["text"], " println!(\"hello\");\n");@@ -412,6 +425,87 @@ mod tests {assert!(call(&e, "build_file_write", json!({"path": "../outside.txt", "content": "x"})).is_err());}+ /// "Work on my project" keeps the app in the project root: the default listing must be that+ /// root, not a missing or nested `app/`, and not Craft Anything's own records.+ #[test]+ fn a_project_edited_in_place_is_listed_from_its_own_folder() {+ let d = tempfile::tempdir().unwrap();+ std::fs::write(d.path().join("main.py"), "print('hi')\n").unwrap();+ std::fs::create_dir_all(d.path().join("src")).unwrap();+ std::fs::write(d.path().join("src/app.py"), "x = 1\n").unwrap();+ // A nested app/ would be the wrong default: the app itself is the root.+ std::fs::create_dir_all(d.path().join("app")).unwrap();+ std::fs::write(d.path().join("app/other.txt"), "not the app\n").unwrap();+ let mut s = crate::testing::session();+ let ws = Workspace::create_at(d.path(), "Mine", "Add a page", TargetRef::None, Some(ExistingFiles::Edit)).unwrap();+ // checks/ isn't a Craft Anything record the builder must avoid: it keeps its own notes there.+ std::fs::create_dir_all(d.path().join("checks")).unwrap();+ std::fs::write(d.path().join("checks/notes.md"), "note\n").unwrap();+ // capture/ is an own record in the root listing, but the brief tells the builder to read it.+ std::fs::create_dir_all(d.path().join("capture/pages")).unwrap();+ std::fs::write(d.path().join("capture/README.md"), "evidence\n").unwrap();+ std::fs::write(d.path().join("capture/pages/index.html"), "<p>evidence</p>\n").unwrap();+ // A dependency tree bigger than the entry cap must not crowd out the app's own files.+ std::fs::create_dir_all(d.path().join("vendor")).unwrap();+ for i in 0..MAX_ENTRIES + 10 {+ std::fs::write(d.path().join("vendor").join(format!("f{i:04}.txt")), b"x").unwrap();+ }+ s.ext.insert(Arc::new(InboxDir(ws.inbox_dir())));+ let e = Engine::new(s);++ let list = call(&e, "build_files_list", json!({})).unwrap();+ assert_eq!(list["root"], ".");+ let entries = list["entries"].to_string();+ assert!(entries.contains("main.py") && entries.contains("src/app.py"), "{list}");+ assert!(entries.contains("app/other.txt"), "a real nested folder is still part of the root: {list}");+ assert!(entries.contains("checks/notes.md"), "the builder's own check notes stay visible: {list}");+ assert!(!entries.contains("craft.json"), "Craft Anything's own records are not app files: {list}");+ assert!(!entries.contains("capture/"), "capture stays out of the default root listing: {list}");+ assert!(entries.contains("vendor/"), "the dependency folder is listed but not walked: {list}");+ assert_eq!(list["truncated"], false, "vendor must not consume the entry cap: {list}");++ // The brief tells the builder to read capture/, so an explicit listing is allowed.+ let capture = call(&e, "build_files_list", json!({"path": "capture"})).unwrap();+ assert_eq!(capture["root"], "capture");+ assert!(capture["entries"].to_string().contains("capture/README.md"), "{capture}");+ assert!(capture["entries"].to_string().contains("capture/pages/"), "{capture}");+ let pages = call(&e, "build_files_list", json!({"path": "capture/pages"})).unwrap();+ assert_eq!(pages["root"], "capture/pages");+ assert!(pages["entries"].to_string().contains("capture/pages/index.html"), "{pages}");+ std::fs::create_dir_all(d.path().join("reference")).unwrap();+ std::fs::write(d.path().join("reference/look.png"), "x").unwrap();+ let reference = call(&e, "build_files_list", json!({"path": "reference"})).unwrap();+ assert!(reference["entries"].to_string().contains("reference/look.png"), "{reference}");++ // An explicit nested folder is still listed by that path.+ let nested = call(&e, "build_files_list", json!({"path": "app"})).unwrap();+ assert_eq!(nested["root"], "app");+ assert!(nested["entries"].to_string().contains("app/other.txt"), "{nested}");+ }++ /// A project path with `..` must not make listing paths absolute or hide an explicit capture.+ #[test]+ fn an_explicit_capture_listing_handles_a_project_path_with_dot_dot() {+ let base = tempfile::tempdir().unwrap();+ let project = base.path().join("project");+ std::fs::create_dir_all(&project).unwrap();+ let dotted = project.join("..").join("project");+ let mut s = crate::testing::session();+ let ws = Workspace::create_at(&dotted, "Mine", "Look at the evidence", TargetRef::None, None).unwrap();+ std::fs::create_dir_all(ws.capture_dir()).unwrap();+ std::fs::write(ws.capture_dir().join("README.md"), "evidence\n").unwrap();+ assert!(ws.is_own_record(&ws.capture_dir().join("README.md")), "capture is an own record");+ s.ext.insert(Arc::new(InboxDir(ws.inbox_dir())));+ let e = Engine::new(s);++ let capture = call(&e, "build_files_list", json!({"path": "capture"})).unwrap();+ assert_eq!(capture["root"], "capture");+ let entries = capture["entries"].to_string();+ assert!(entries.contains("capture/README.md"), "{capture}");+ let absolute = base.path().to_string_lossy().into_owned();+ assert!(!entries.contains(&absolute), "paths stay relative: {capture}");+ }+/// A link to a folder; false where this system won't let a test make one.fn link_dir(target: &Path, link: &Path) -> bool {#[cfg(unix)]diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex e35df549..5ca6ab26 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -871,6 +871,9 @@ Gaps found while using the app for real projects.- [x] Leaving "Work on my project" forgets a folder it would edit in place and drops a folderdialog that ends later, so a version made on Make can't edit that project, and an addressin its request isn't read as an original to recreate (review of #686) — claimed: xerenon, branch bugfix/r2.9-own-project-review; PR #750+- [x] `build_files_list` lists an edited-in-place project from the project folder itself, not a+ missing or nested `app/`, and leaves Craft Anything's own records out+ — claimed: AlexJoaquimPereira, branch `bugfix/r2.9-build-files-in-place`; commit 1ffeb40- [x] Choose Claude Code's folder (`CLAUDE_CONFIG_DIR`) per AI, so one computer can build withtwo Claude accounts; an app opened from the Dock never sees the shell's variable. Settings →AI (a typed folder, and Add another) and `ai folder`; live-checked from the CLI with an empty