Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
feat(skills): bound a package resource separately from the document
vastsa39eb46d84f4b
diff --git a/crates/host-core/src/config_sync/engine.rs b/crates/host-core/src/config_sync/engine.rsindex 57110bcbd..ac7c2280d 100644--- a/crates/host-core/src/config_sync/engine.rs+++ b/crates/host-core/src/config_sync/engine.rs@@ -1242,6 +1242,12 @@ mod tests {Ok(())}+ /// One packaged resource: above the 128 KiB document cap that used to bound+ /// every resource, well below the resource cap that bounds it now.+ fn portable_skill_resource() -> Vec<u8> {+ vec![b'x'; 256 * 1024]+ }+#[tokio::test]async fn two_devices_sync_credentials_capabilities_and_disjoint_edit() -> Result<()> {if run_in_isolated_process(@@ -1303,15 +1309,24 @@ mod tests {enabled: Some(true),..McpServerInput::default()})?;- state.user_skills.create(UserSkillInput {+ let portable_skill = state.user_skills.create(UserSkillInput {id: Some("portable-skill".into()),name: Some("Portable Skill".into()),level: Some("project".into()),project_path: Some(project_a.to_string_lossy().into_owned()),body: Some("Use the portable project workflow.".into()),enabled: Some(true),+ shape: Some("dir".into()),..UserSkillInput::default()})?;+ // A package resource larger than the 128 KiB document cap must+ // survive capture, upload, read-back, and apply.+ state.user_skills.write_package_files(+ &portable_skill.id,+ CapabilityLevel::Project,+ Some(&project_a.to_string_lossy()),+ &[("data/fonts.csv".into(), portable_skill_resource())],+ )?;provider.id};@@ -1463,6 +1478,21 @@ mod tests {.list(CapabilityLevel::Project, Some(&project_b.to_string_lossy()),)?.iter().any(|record| record.id == "portable-skill"));+ let received_skill = state+ .user_skills+ .list(CapabilityLevel::Project, Some(&project_b.to_string_lossy()))?+ .into_iter()+ .find(|record| record.id == "portable-skill")+ .expect("portable skill imported");+ assert_eq!(+ state.user_skills.package_files(+ &received_skill.id,+ CapabilityLevel::Project,+ Some(&project_b.to_string_lossy()),+ )?,+ vec![("data/fonts.csv".into(), portable_skill_resource())],+ "packaged resource did not survive the sync round trip"+ );assert_eq!(state.db.get_setting("app")?.and_then(|value| {valuediff --git a/crates/host-core/src/config_sync/engine_remote.rs b/crates/host-core/src/config_sync/engine_remote.rsindex 64aa00e28..126ccd750 100644--- a/crates/host-core/src/config_sync/engine_remote.rs+++ b/crates/host-core/src/config_sync/engine_remote.rs@@ -1,4 +1,9 @@use super::*;+/// Ceiling for one resource object read back from a revision. A resource is+/// produced by the skill packager, so the receiver must accept exactly what the+/// packager accepts: a smaller receiver bound would turn an accepted upload+/// into a failed download.+const MAX_REMOTE_RESOURCE_BYTES: usize = crate::user_skills::MAX_SKILL_RESOURCE_BYTES;pub(super) fn remote_prefix(config: &StoredConfig) -> String {format!("vault/{}/", config.vault_id)@@ -307,7 +312,7 @@ pub(super) async fn read_remote_revision(&config.vault_id,&ciphertext,)?;- if bytes.len() > 128 * 1024 {+ if bytes.len() > MAX_REMOTE_RESOURCE_BYTES {bail!("CONFIG_SYNC_LIMIT_EXCEEDED: remote resource is too large");}resources.insert(object_id, bytes);diff --git a/crates/host-core/src/user_skills.rs b/crates/host-core/src/user_skills.rsindex aada6e248..b6becf15f 100644--- a/crates/host-core/src/user_skills.rs+++ b/crates/host-core/src/user_skills.rs@@ -13,9 +13,16 @@ use std::path::{Path, PathBuf};use uuid::Uuid;const MAX_SKILLS: usize = 128;+/// Cap on a skill document (`SKILL.md`) and on any document derived from it.+/// A document can end up in a prompt, so it stays small; sibling package+/// resources are bounded separately by [`MAX_SKILL_RESOURCE_BYTES`].pub const MAX_SKILL_BYTES: usize = 128 * 1024;-pub const MAX_SKILL_PACKAGE_FILES: usize = 64;-pub const MAX_SKILL_PACKAGE_BYTES: usize = 512 * 1024;+/// Cap on one sibling resource inside a directory-shaped skill package.+/// Resources are data and scripts that are read on demand and never become+/// prompt content, so a package may carry files well above the document cap.+pub const MAX_SKILL_RESOURCE_BYTES: usize = 2 * 1024 * 1024;+pub const MAX_SKILL_PACKAGE_FILES: usize = 256;+pub const MAX_SKILL_PACKAGE_BYTES: usize = 16 * 1024 * 1024;const MAX_NAME_CHARS: usize = 120;const MAX_DESCRIPTION_CHARS: usize = 400;const SKILL_KIND: &str = "skills";@@ -528,8 +535,14 @@ fn collect_package_files(continue;}let relative = package_relative_path(root, &path)?;+ // Reject an oversized resource from its metadata before reading it: a+ // multi-gigabyte file in a skill directory must not be pulled into+ // memory only to be refused.+ if metadata.len() > MAX_SKILL_RESOURCE_BYTES as u64 {+ bail!("SKILL_LIMIT_EXCEEDED: skill package resource is too large");+ }let bytes = fs::read(&path)?;- if bytes.len() > MAX_SKILL_BYTES {+ if bytes.len() > MAX_SKILL_RESOURCE_BYTES {bail!("SKILL_LIMIT_EXCEEDED: skill package resource is too large");}*total = total.saturating_add(bytes.len());@@ -1244,7 +1257,7 @@ impl UserSkillRegistry {if !seen.insert(relative.clone()) {bail!("SKILL_INVALID: skill package contains duplicate paths");}- if bytes.len() > MAX_SKILL_BYTES {+ if bytes.len() > MAX_SKILL_RESOURCE_BYTES {bail!("SKILL_LIMIT_EXCEEDED: skill package resource is too large");}ensure_no_symlink_path(&root, &target)?;diff --git a/crates/host-core/src/user_skills/package_tests.rs b/crates/host-core/src/user_skills/package_tests.rsnew file mode 100644index 000000000..ab8e690f6--- /dev/null+++ b/crates/host-core/src/user_skills/package_tests.rs@@ -0,0 +1,187 @@+use super::*;+use tempfile::tempdir;++/// Builds a directory-shaped skill and returns its registry, id, and on-disk+/// package root, so package-limit tests only state what they assert.+fn packaged_skill(app: &tempfile::TempDir) -> (UserSkillRegistry, String, PathBuf) {+ let project = app.path().join("project");+ fs::create_dir_all(&project).unwrap();+ let mut registry = UserSkillRegistry::new(app.path());+ let record = registry+ .create(UserSkillInput {+ id: Some("packaged".into()),+ name: Some("Packaged".into()),+ level: Some("project".into()),+ project_path: Some(project.to_str().unwrap().into()),+ description: Some("Check the relevant files".into()),+ body: Some("Do the thing.".into()),+ shape: Some("dir".into()),+ ..UserSkillInput::default()+ })+ .unwrap();+ let root = PathBuf::from(&record.path).parent().unwrap().to_path_buf();+ (registry, record.id, root)+}++#[test]+fn directory_skill_package_accepts_resources_above_the_document_limit() {+ let app = tempdir().unwrap();+ let (mut registry, id, _) = packaged_skill(&app);+ let project = app.path().join("project");+ let project_path = Some(project.to_str().unwrap());+ // Eight times the document cap, still inside the per-resource cap.+ let big = vec![b'x'; MAX_SKILL_BYTES * 8];+ assert!(big.len() <= MAX_SKILL_RESOURCE_BYTES);+ registry+ .write_package_files(+ &id,+ CapabilityLevel::Project,+ project_path,+ &[("data/fonts.csv".into(), big.clone())],+ )+ .unwrap();+ let files = registry+ .package_files(&id, CapabilityLevel::Project, project_path)+ .unwrap();+ assert_eq!(files, vec![("data/fonts.csv".into(), big)]);+}++/// The package a real design-reference skill needs: 128 resources, about+/// 3.8 MiB in total, and one 808 KiB data file. It is over every bound the+/// packager used to carry — 64 resources, 512 KiB per package, 128 KiB per+/// resource — so this test fails if any of them comes back.+#[test]+fn directory_skill_package_accepts_a_data_shaped_package() {+ const LARGEST: usize = 808 * 1024;+ const SMALLEST: usize = 24 * 1024;+ const SMALLEST_COUNT: usize = 127;+ let app = tempdir().unwrap();+ let (mut registry, id, root) = packaged_skill(&app);+ let project = app.path().join("project");+ let project_path = Some(project.to_str().unwrap());+ fs::create_dir_all(root.join("data")).unwrap();+ for index in 0..SMALLEST_COUNT {+ // Sparse files: capture checks metadata length before reading bytes.+ fs::File::create(root.join(format!("data/{index}.csv")))+ .unwrap()+ .set_len(SMALLEST as u64)+ .unwrap();+ }+ fs::File::create(root.join("data/fonts.csv"))+ .unwrap()+ .set_len(LARGEST as u64)+ .unwrap();+ let expected_total = LARGEST + SMALLEST * SMALLEST_COUNT;+ let files = registry+ .package_files(&id, CapabilityLevel::Project, project_path)+ .unwrap();+ assert!(+ LARGEST > MAX_SKILL_BYTES && files.len() > 64,+ "fixture must exceed the document and file-count bounds this replaces"+ );+ assert_eq!(files.len(), SMALLEST_COUNT + 1);+ assert_eq!(+ files.iter().map(|(_, bytes)| bytes.len()).sum::<usize>(),+ expected_total+ );+}++#[test]+fn directory_skill_package_rejects_a_resource_above_the_resource_limit() {+ let app = tempdir().unwrap();+ let (mut registry, id, root) = packaged_skill(&app);+ let project = app.path().join("project");+ let project_path = Some(project.to_str().unwrap());+ let error = registry+ .write_package_files(+ &id,+ CapabilityLevel::Project,+ project_path,+ &[(+ "data/huge.json".into(),+ vec![b'x'; MAX_SKILL_RESOURCE_BYTES + 1],+ )],+ )+ .unwrap_err()+ .to_string();+ assert!(error.contains("SKILL_LIMIT_EXCEEDED"), "error = {error}");+ // A resource already on disk is refused on capture at the same bound.+ fs::create_dir_all(root.join("data")).unwrap();+ fs::write(+ root.join("data/huge.json"),+ vec![b'x'; MAX_SKILL_RESOURCE_BYTES + 1],+ )+ .unwrap();+ let error = registry+ .package_files(&id, CapabilityLevel::Project, project_path)+ .unwrap_err()+ .to_string();+ assert!(error.contains("SKILL_LIMIT_EXCEEDED"), "error = {error}");+}++#[test]+fn directory_skill_package_rejects_more_files_than_the_limit() {+ let app = tempdir().unwrap();+ let (mut registry, id, root) = packaged_skill(&app);+ let project = app.path().join("project");+ let project_path = Some(project.to_str().unwrap());+ let files = (0..=MAX_SKILL_PACKAGE_FILES)+ .map(|index| (format!("data/{index}.txt"), b"x".to_vec()))+ .collect::<Vec<_>>();+ let error = registry+ .write_package_files(&id, CapabilityLevel::Project, project_path, &files)+ .unwrap_err()+ .to_string();+ assert!(error.contains("SKILL_LIMIT_EXCEEDED"), "error = {error}");+ // Capture refuses the same package found on disk.+ fs::create_dir_all(root.join("data")).unwrap();+ for (relative, bytes) in &files {+ fs::write(root.join(relative), bytes).unwrap();+ }+ let error = registry+ .package_files(&id, CapabilityLevel::Project, project_path)+ .unwrap_err()+ .to_string();+ assert!(error.contains("SKILL_LIMIT_EXCEEDED"), "error = {error}");+}++#[test]+fn directory_skill_package_rejects_a_package_above_the_total_limit() {+ let app = tempdir().unwrap();+ let (mut registry, id, root) = packaged_skill(&app);+ let project = app.path().join("project");+ let project_path = Some(project.to_str().unwrap());+ let count = MAX_SKILL_PACKAGE_BYTES / MAX_SKILL_RESOURCE_BYTES + 1;+ assert!(+ count <= MAX_SKILL_PACKAGE_FILES,+ "fixture must fit the file cap"+ );+ fs::create_dir_all(root.join("data")).unwrap();+ for index in 0..count {+ // Sparse files: the cap is checked against metadata length first.+ let file = fs::File::create(root.join(format!("data/{index}.bin"))).unwrap();+ file.set_len(MAX_SKILL_RESOURCE_BYTES as u64).unwrap();+ }+ let error = registry+ .package_files(&id, CapabilityLevel::Project, project_path)+ .unwrap_err()+ .to_string();+ assert!(error.contains("SKILL_LIMIT_EXCEEDED"), "error = {error}");+}++#[test]+fn skill_document_limit_still_matches_the_prompt_sized_cap() {+ let app = tempdir().unwrap();+ let project = app.path().join("project");+ fs::create_dir_all(&project).unwrap();+ let mut registry = UserSkillRegistry::new(app.path());+ let payload = UserSkillInput {+ name: Some("Too big".into()),+ level: Some("project".into()),+ project_path: Some(project.to_str().unwrap().into()),+ body: Some("x".repeat(MAX_SKILL_BYTES)),+ ..UserSkillInput::default()+ };+ let error = registry.create(payload).unwrap_err().to_string();+ assert!(error.contains("document exceeds"), "error = {error}");+}