Files
buzz/crates/buzz-agent/src/hints.rs
Matt Toohey 8216db1c71 chore(toolchain): bump Rust to 1.98.1 for macOS 27 proc-macro loading (#8598)
## Summary

Bump the pinned Rust toolchain from 1.95.0 to 1.98.1 so release builds
work on macOS 27 hosts.

Rust ≤ 1.97 strips Apple-target artifacts with its bundled
`llvm-objcopy`, which leaves the Mach-O `LC_SYMTAB` string pool
(`__LINKEDIT`) only 4-byte aligned. macOS 27's dyld refuses to `dlopen`
such images (`mis-aligned LINKEDIT string pool`). Because cargo applies
the release profile's default `strip = "debuginfo"` to proc-macro dylibs
too, `cargo build --release` of any crate that uses sqlx macros
(`buzz-db`, `buzz-relay`, `buzz-admin`, …) fails on macOS 27: rustc
cannot load `libsqlx_macros-*.dylib`. Dev builds pass because the dev
profile does not strip, which is why this only shows up as a
release/packaging failure.

The fix (llvm/llvm-project#203680, pulled into rustc by
rust-lang/rust#158410 for rust-lang/rust#157750) first shipped in Rust
1.98.0 and was never backported to 1.97.x. `block/buzz-app` pinned
1.98.1 for the same failure in block/buzz-app#529. With this bump the
`CARGO_PROFILE_RELEASE_BUILD_OVERRIDE_STRIP=none` / custom-linker
workarounds people have been using locally are no longer needed.

### What changes

Two commits: the toolchain bump (with the desktop clippy fixes), then
the workspace clippy fixes.

| Location | Change |
|---|---|
| `rust-toolchain.toml` | `channel = "1.95.0"` → `"1.98.1"` (profile
unchanged) |
| `Dockerfile`, `Dockerfile.push-gateway` | `ARG RUST_VERSION=1.95` →
`1.98` |
| `Dockerfile.sprig` | `rust:1.98-alpine3.22` with the manifest-list
digest verified on Docker Hub (shared by the `1.98` and `1.98.1` tags) |
| `.github/workflows/release.yml` | `dtolnay/rust-toolchain` pinned to
the tip of its `1.98.1` branch (`18f04874`) for the Windows desktop
release job |
| `benchmarks/harbor-buzz-orchestra` | `RUST_IMAGE = rust:1.98-alpine` |
| `_ci-rust.yml`, `windows-canary.yml`, `app_state_tests.rs` | stale
"1.95" comments reworded; the `unknown_lints` allow stays with a
version-agnostic rationale |
| `desktop/src-tauri` | lints clippy 1.98 newly raises under `-D
warnings`: `chunks_exact_to_as_chunks` at five fixed-width PCM/UTF-16
decoders, `needless_borrows_for_generic_args` on the `Copy` `norm`
closure in `config_bridge/reader.rs`, and a redundant `Engine as _`
import in `hpke_key_backup_tests.rs` that `use super::*` already
provides |
| Workspace crates (9) | the 30 lints clippy 1.98 newly raises in `cargo
clippy --workspace --all-targets -- -D warnings`, listed below |

Workspace lint fixes:

| Lint | Sites | Fix |
|---|---|---|
| `result_large_err` (20) | `buzz-relay` `api/git/transport.rs` (8),
`api/git/settings.rs` (6), `api/gifs.rs`, `api/workflows.rs`,
`api/profiles.rs`; `buzz-push-gateway` `verify_installation_assertion` |
Clippy 1.98 now checks `async fn`s, so every `async fn … -> Result<_,
axum::response::Response>` trips it. These follow the existing allow
pattern in `api/media.rs` and `nip_fi_http.rs` ("Response is the natural
error type for axum handlers"): module-level in the two git files,
per-function elsewhere. Boxing `Response` would touch every handler for
no runtime benefit. |
| `chunks_exact_to_as_chunks` (3) | `buzz-media` MP4 brand scan;
`buzz-voice` npy f32 reader and stereo test fixture | `chunks_exact(N)`
→ `as_chunks::<N>().0` |
| `question_mark` (3) | `buzz-acp` queue flush fallback and
slash-command mention skip; `buzz-agent` `find_git_root` | `match … {
None => return None }` → `?` |
| `useless_format` (2) | `buzz-auth` nip_fi startup test policies |
`format!("…")` → `"…".to_string()` |
| `useless_borrows_in_formatting` (1) | `buzz-pairing-cli` secret print
| drop the borrow |
| `manual_noop_waker` (1) | `buzz-sdk` broker test `block_on` |
`Waker::noop()` (stable since 1.85) |

All `as_chunks` rewrites keep `chunks_exact`'s remainder-dropping
semantics, and `as_chunks` is stable since 1.88, which is the MSRV.

One change in `api/git/transport.rs` is not a lint fix. Staging that
file trips the pre-commit secret scanner on its existing test-only ES256
fixture (`GIT_TEST_EC_PEM`), and the scanner only honours a suppression
marker on the flagged line. So the `\`-continued string literal is
rewritten as a byte-identical `concat!` whose `BEGIN` line carries
`sadscan:disable kingfisher.privkey.2`.

Left as is on purpose: `Cargo.toml` `rust-version = "1.88.0"` (MSRV
floor, a separate concern), the README/CONTRIBUTING "1.88+" text, Hermit
(no Rust packages there), the lockfiles, and the `(1.95.0)` comment in
`scripts/start-isolated-test-relay.sh` (the pre-commit secret scanner
flags that file's pre-existing dev-only `DATABASE_URL` whenever it is
staged).

### Related issue
Fixes #8074

### Testing
All under rustc/clippy 1.98.1 on a macOS 27 host (Darwin 27.0.0,
aarch64), with no `RUSTFLAGS` / `SDKROOT` / strip overrides in the
environment:

- `cargo build --release -p buzz-db` (the #8074 repro: the release
profile strips the proc-macro dylib): **builds**. The resulting
`libsqlx_macros-*.dylib` (14 MB) has `LC_SYMTAB stroff % 8 == 0` per
`otool -l`. The same build on 1.95.0 died with `mis-aligned LINKEDIT
string pool`.
- `just clippy` (workspace, `-D warnings`) and `cargo fmt --check`
(workspace and `desktop/src-tauri`): pass
- `just desktop-tauri-clippy` (default and `mesh-llm` feature sets):
pass
- `just test-unit`: every crate passes. The `buzz-acp` binary failed
only under pre-push lane contention; rerun solo it is 1060 passed, 0
failed.
- `just desktop-tauri-test`: 3422 passed, 0 failed
- After the lint fixes: lib/bin unit tests for `buzz-acp`, `buzz-agent`,
`buzz-auth`, `buzz-media`, `buzz-voice`, `buzz-sdk`, `buzz-pairing-cli`,
and `buzz-relay`'s `api::git::transport` tests, including the
Postgres-backed
`enforce_mode_git_pack_routes_missing_assertion_exact_bytes`, which
parses the rewritten PEM: pass

`cfg(windows)` code was not linted locally (no Windows target
installed). CI's Windows Rust job runs workspace clippy for
`x86_64-pc-windows-msvc`, and that passes on this branch. CI also builds
the relay, push-gateway and sprig images on the new `rust:1.98` bases
for amd64 and arm64.

No runtime behavior changes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Matt Toohey <contact@matttoohey.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-27 08:13:38 +11:00

724 lines
25 KiB
Rust

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use crate::mcp::truncate_at_boundary;
const MAX_HINTS_BYTES: usize = 128 * 1024;
pub const MAX_SKILL_BODY_BYTES: usize = 32 * 1024;
const SKILL_DIRS: &[&str] = &[".agents/skills", ".goose/skills", ".claude/skills"];
fn home_dir() -> Option<PathBuf> {
std::env::var("HOME").ok().map(PathBuf::from)
}
#[derive(Clone)]
pub struct SkillEntry {
pub name: String,
pub description: String,
/// Absolute path to the SKILL.md file; used by `load_skill` to read on demand.
pub path: PathBuf,
/// Absolute paths to every non-SKILL.md file in the skill directory tree.
/// Pre-enumerated at discovery time so `load_skill` can match by relative path
/// without doing arbitrary filesystem lookups at call time.
pub supporting_files: Vec<PathBuf>,
}
/// Handles both normal repos (`.git/` dir) and worktrees (`.git` file).
fn find_git_root(start: &Path) -> Option<PathBuf> {
let mut current = start.to_path_buf();
loop {
if current.join(".git").exists() {
return Some(current);
}
current = current.parent()?.to_path_buf();
}
}
fn load_hint_files_impl(cwd: &Path, home: Option<&Path>) -> String {
let mut chain = match find_git_root(cwd) {
Some(root) => {
let mut c: Vec<PathBuf> = cwd
.ancestors()
.take_while(|a| a.starts_with(&root))
.map(|a| a.to_path_buf())
.collect();
// ancestors() yields cwd first, root last — reverse for root→cwd.
c.reverse();
c
}
None => vec![cwd.to_path_buf()],
};
// Prepend ~/AGENTS.md as global layer, unless ~ is already in the chain.
if let Some(home) = home {
if !chain.iter().any(|d| d == home) {
chain.insert(0, home.to_path_buf());
}
}
let mut result = String::new();
for dir in &chain {
let path = dir.join("AGENTS.md");
let Ok(content) = std::fs::read_to_string(&path) else {
continue;
};
if !result.is_empty() {
result.push_str("\n\n");
}
let remaining = MAX_HINTS_BYTES.saturating_sub(result.len());
if remaining == 0 {
break;
}
if content.len() <= remaining {
result.push_str(&content);
} else {
let truncated = truncate_at_boundary(&content, remaining);
result.push_str(truncated);
break;
}
}
result
}
fn parse_skill_frontmatter(content: &str) -> Option<(String, String)> {
// Must start with `---`
let rest = content.strip_prefix("---\n")?;
// Find the closing `---`
let close_pos = rest.find("\n---")?;
let yaml_block = &rest[..close_pos];
let map: HashMap<String, serde_yaml::Value> = serde_yaml::from_str(yaml_block).ok()?;
let name = map
.get("name")
.and_then(|v| v.as_str())
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_string)?;
let description = map
.get("description")
.and_then(|v| v.as_str())
.map(str::trim)
.unwrap_or("")
.to_string();
Some((name, description))
}
fn scan_skill_dir(dir: &Path, seen: &mut HashSet<String>, skills: &mut Vec<SkillEntry>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
let mut subdirs: Vec<PathBuf> = entries
.filter_map(|e| e.ok())
// Use std::fs::metadata (follows symlinks) rather than DirEntry::file_type
// (which returns FileType::Symlink for symlinks, causing is_dir() to return
// false even when the symlink target is a directory).
.filter(|e| {
std::fs::metadata(e.path())
.map(|m| m.is_dir())
.unwrap_or(false)
})
.map(|e| e.path())
.collect();
subdirs.sort();
for subdir in subdirs {
let skill_md = subdir.join("SKILL.md");
let Ok(content) = std::fs::read_to_string(&skill_md) else {
continue;
};
let Some((name, description)) = parse_skill_frontmatter(&content) else {
continue;
};
if seen.contains(&name) {
continue;
}
seen.insert(name.clone());
// Collect supporting files: every non-SKILL.md file in the skill dir tree.
// Don't descend into subdirs that themselves have a SKILL.md — those are
// separate skills with their own entries.
let supporting_files = collect_supporting_files(&subdir);
skills.push(SkillEntry {
name,
description,
path: skill_md,
supporting_files,
});
}
}
/// Walk `skill_dir` recursively and return the absolute path of every file
/// that is not `SKILL.md`. Subdirectories that contain their own `SKILL.md`
/// are treated as separate skills and are not descended into.
fn collect_supporting_files(skill_dir: &Path) -> Vec<PathBuf> {
let mut result = Vec::new();
let mut visited_dirs = HashSet::new();
collect_supporting_files_impl(skill_dir, &mut result, &mut visited_dirs);
result.sort();
result
}
fn collect_supporting_files_impl(
current: &Path,
out: &mut Vec<PathBuf>,
visited_dirs: &mut HashSet<PathBuf>,
) {
let Ok(canonical_current) = current.canonicalize() else {
return;
};
if !visited_dirs.insert(canonical_current) {
return;
}
let Ok(entries) = std::fs::read_dir(current) else {
return;
};
let mut items: Vec<_> = entries.filter_map(|e| e.ok()).collect();
items.sort_by_key(|e| e.path());
for entry in items {
let path = entry.path();
// Use std::fs::metadata (follows symlinks) so symlinked subdirs and files
// inside a skill directory are handled correctly.
let ft = match std::fs::metadata(&path) {
Ok(m) => m,
Err(_) => continue,
};
if ft.is_dir() {
// Don't descend into subdirs that are themselves skills.
if path.join("SKILL.md").is_file() {
continue;
}
collect_supporting_files_impl(&path, out, visited_dirs);
} else if ft.is_file() && path.file_name().and_then(|n| n.to_str()) != Some("SKILL.md") {
out.push(path);
}
}
}
fn discover_skills_impl(cwd: &Path, home: Option<&Path>) -> Vec<SkillEntry> {
let mut seen = HashSet::new();
let mut skills = Vec::new();
for dir_suffix in SKILL_DIRS {
scan_skill_dir(&cwd.join(dir_suffix), &mut seen, &mut skills);
}
if let Some(home) = home {
scan_skill_dir(&home.join(".agents/skills"), &mut seen, &mut skills);
}
skills
}
pub fn build_hints_section(cwd: &Path) -> (String, Vec<SkillEntry>) {
build_hints_section_impl(cwd, home_dir().as_deref())
}
fn build_hints_section_impl(cwd: &Path, home: Option<&Path>) -> (String, Vec<SkillEntry>) {
let hints_text = load_hint_files_impl(cwd, home);
let skills = discover_skills_impl(cwd, home);
if hints_text.is_empty() && skills.is_empty() {
return (String::new(), skills);
}
let mut out = String::from("# Additional Instructions\n");
if !hints_text.is_empty() {
out.push_str("\n## Project Hints\n");
out.push_str(&hints_text);
out.push('\n');
}
if !skills.is_empty() {
out.push_str("\n## Available Skills\n");
for skill in &skills {
out.push_str(&format!("- {}: {}\n", skill.name, skill.description));
}
out.push_str(
"\nUse the `load_skill` tool to read the full content of a skill before using it.\n",
);
}
(out, skills)
}
/// Strip the YAML frontmatter block from a skill file's content and return
/// the body. If no valid frontmatter is found, returns the content unchanged.
pub(crate) fn strip_frontmatter(content: &str) -> &str {
let Some(rest) = content.strip_prefix("---\n") else {
return content;
};
let Some(close_pos) = rest.find("\n---") else {
return content;
};
let after = &rest[close_pos + 4..]; // skip "\n---"
after.strip_prefix('\n').unwrap_or(after)
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn find_git_root_normal_repo() {
let tmp = TempDir::new().unwrap();
let root = tmp.path();
std::fs::create_dir(root.join(".git")).unwrap();
assert_eq!(find_git_root(root), Some(root.to_path_buf()));
}
#[test]
fn find_git_root_worktree() {
let tmp = TempDir::new().unwrap();
let root = tmp.path();
// .git as a file (worktree)
std::fs::write(root.join(".git"), "gitdir: ../main/.git/worktrees/wt").unwrap();
assert_eq!(find_git_root(root), Some(root.to_path_buf()));
}
#[test]
fn find_git_root_none() {
let tmp = TempDir::new().unwrap();
// No .git anywhere under tmp
let result = find_git_root(tmp.path());
// In a CI environment the test itself may live inside a real git repo,
// so only assert None when tmp is truly isolated (not a subpath of a git repo).
// We verify by checking that any found root is NOT inside tmp.
if let Some(found) = result {
assert!(!found.starts_with(tmp.path()));
}
}
#[test]
fn find_git_root_from_subdirectory() {
let tmp = TempDir::new().unwrap();
let root = tmp.path();
std::fs::create_dir(root.join(".git")).unwrap();
let deep = root.join("sub").join("deep");
std::fs::create_dir_all(&deep).unwrap();
assert_eq!(find_git_root(&deep), Some(root.to_path_buf()));
}
#[test]
fn load_hint_files_single_at_cwd() {
let tmp = TempDir::new().unwrap();
let cwd = tmp.path();
// No .git → no git root discovery; only cwd is checked.
std::fs::write(cwd.join("AGENTS.md"), "cwd hints").unwrap();
let result = load_hint_files_impl(cwd, None);
assert_eq!(result, "cwd hints");
}
#[test]
fn load_hint_files_git_root_and_cwd() {
let tmp = TempDir::new().unwrap();
let root = tmp.path();
std::fs::create_dir(root.join(".git")).unwrap();
std::fs::write(root.join("AGENTS.md"), "root hints").unwrap();
let sub = root.join("sub");
std::fs::create_dir(&sub).unwrap();
std::fs::write(sub.join("AGENTS.md"), "sub hints").unwrap();
let result = load_hint_files_impl(&sub, None);
// Root hints must come first.
assert!(
result.starts_with("root hints"),
"expected root hints first, got: {result:?}"
);
assert!(result.contains("sub hints"), "missing sub hints");
let root_pos = result.find("root hints").unwrap();
let sub_pos = result.find("sub hints").unwrap();
assert!(root_pos < sub_pos, "root hints should precede sub hints");
}
#[test]
fn load_hint_files_missing_files() {
let tmp = TempDir::new().unwrap();
let result = load_hint_files_impl(tmp.path(), None);
assert_eq!(result, "");
}
#[test]
fn discover_skills_finds_across_dirs() {
let tmp = TempDir::new().unwrap();
let cwd = tmp.path();
// Skill in .agents/skills/
let agents_skill = cwd.join(".agents/skills/my-skill");
std::fs::create_dir_all(&agents_skill).unwrap();
std::fs::write(
agents_skill.join("SKILL.md"),
"---\nname: my-skill\ndescription: A skill\n---\nSkill body here.\n",
)
.unwrap();
// Skill in .goose/skills/
let goose_skill = cwd.join(".goose/skills/other-skill");
std::fs::create_dir_all(&goose_skill).unwrap();
std::fs::write(
goose_skill.join("SKILL.md"),
"---\nname: other-skill\ndescription: Another skill\n---\nOther body.\n",
)
.unwrap();
let skills = discover_skills_impl(cwd, None);
assert_eq!(skills.len(), 2);
let names: Vec<&str> = skills.iter().map(|s| s.name.as_str()).collect();
assert!(names.contains(&"my-skill"), "missing my-skill");
assert!(names.contains(&"other-skill"), "missing other-skill");
// Paths should point to the SKILL.md files.
for skill in &skills {
assert!(skill.path.exists(), "path does not exist: {:?}", skill.path);
assert!(skill.path.ends_with("SKILL.md"));
}
}
#[test]
fn discover_skills_dedup_by_name() {
let tmp = TempDir::new().unwrap();
let cwd = tmp.path();
// Same name in .agents/skills/ (first) and .goose/skills/ (second)
let agents_skill = cwd.join(".agents/skills/shared");
std::fs::create_dir_all(&agents_skill).unwrap();
std::fs::write(
agents_skill.join("SKILL.md"),
"---\nname: shared\ndescription: from agents\n---\nAgents body.\n",
)
.unwrap();
let goose_skill = cwd.join(".goose/skills/shared");
std::fs::create_dir_all(&goose_skill).unwrap();
std::fs::write(
goose_skill.join("SKILL.md"),
"---\nname: shared\ndescription: from goose\n---\nGoose body.\n",
)
.unwrap();
let skills = discover_skills_impl(cwd, None);
assert_eq!(skills.len(), 1, "duplicate name should be deduplicated");
assert_eq!(
skills[0].description, "from agents",
"first wins (.agents/)"
);
// Path should point to the .agents/ version (first wins).
assert!(skills[0]
.path
.to_str()
.unwrap()
.contains(".agents/skills/shared"));
}
#[test]
fn discover_skills_skips_missing_name() {
let tmp = TempDir::new().unwrap();
let cwd = tmp.path();
let skill_dir = cwd.join(".agents/skills/no-name");
std::fs::create_dir_all(&skill_dir).unwrap();
std::fs::write(
skill_dir.join("SKILL.md"),
"---\ndescription: No name here\n---\nBody.\n",
)
.unwrap();
let skills = discover_skills_impl(cwd, None);
assert!(skills.is_empty(), "entry without name should be skipped");
}
#[test]
fn build_hints_section_empty() {
let tmp = TempDir::new().unwrap();
let (result, skills) = build_hints_section_impl(tmp.path(), None);
assert_eq!(result, "");
assert!(skills.is_empty());
}
#[test]
fn build_hints_section_combined() {
let tmp = TempDir::new().unwrap();
let cwd = tmp.path();
std::fs::write(cwd.join("AGENTS.md"), "Project-level hints.").unwrap();
let skill_dir = cwd.join(".agents/skills/buzz-cli");
std::fs::create_dir_all(&skill_dir).unwrap();
std::fs::write(
skill_dir.join("SKILL.md"),
"---\nname: buzz-cli\ndescription: CLI reference for Buzz managed agents\n---\nUse `buzz` to manage agents.\n",
)
.unwrap();
let (result, skills) = build_hints_section_impl(cwd, None);
assert!(
result.contains("# Additional Instructions"),
"missing header"
);
assert!(result.contains("## Project Hints"), "missing Project Hints");
assert!(
result.contains("Project-level hints."),
"missing hints content"
);
assert!(
result.contains("## Available Skills"),
"missing Available Skills"
);
assert!(
result.contains("buzz-cli: CLI reference for Buzz managed agents"),
"missing skill bullet"
);
// Body must NOT be inlined — lazy loading only.
assert!(
!result.contains("Use `buzz` to manage agents."),
"skill body must not be inlined in system prompt"
);
// The load_skill instruction must be present.
assert!(
result.contains("load_skill"),
"missing load_skill instruction"
);
// The old ### heading format must not appear.
assert!(
!result.contains("### buzz-cli"),
"skill body heading must not be inlined"
);
// The returned skills list should contain the discovered skill.
assert_eq!(skills.len(), 1);
assert_eq!(skills[0].name, "buzz-cli");
}
#[test]
fn load_hint_files_global_loaded_first() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
std::fs::write(home.path().join("AGENTS.md"), "global hints").unwrap();
std::fs::write(cwd.path().join("AGENTS.md"), "local hints").unwrap();
let result = load_hint_files_impl(cwd.path(), Some(home.path()));
let global_pos = result.find("global hints").unwrap();
let local_pos = result.find("local hints").unwrap();
assert!(
global_pos < local_pos,
"global hints should precede local hints"
);
}
#[test]
fn load_hint_files_home_missing_agents_md() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
std::fs::write(cwd.path().join("AGENTS.md"), "local only").unwrap();
let result = load_hint_files_impl(cwd.path(), Some(home.path()));
assert_eq!(result, "local only");
}
#[test]
fn load_hint_files_no_home_dir() {
let cwd = TempDir::new().unwrap();
std::fs::write(cwd.path().join("AGENTS.md"), "local only").unwrap();
let result = load_hint_files_impl(cwd.path(), None);
assert_eq!(result, "local only");
}
#[test]
fn load_hint_files_dedup_when_home_in_chain() {
let tmp = TempDir::new().unwrap();
let home = tmp.path();
std::fs::write(home.join("AGENTS.md"), "single load").unwrap();
let result = load_hint_files_impl(home, Some(home));
assert_eq!(
result.matches("single load").count(),
1,
"AGENTS.md should be loaded exactly once when CWD is home"
);
}
#[test]
fn load_hint_files_dedup_when_home_is_git_root() {
let tmp = TempDir::new().unwrap();
let home = tmp.path();
std::fs::create_dir(home.join(".git")).unwrap();
std::fs::write(home.join("AGENTS.md"), "root+home hints").unwrap();
let sub = home.join("sub");
std::fs::create_dir(&sub).unwrap();
let result = load_hint_files_impl(&sub, Some(home));
assert_eq!(
result.matches("root+home hints").count(),
1,
"AGENTS.md should be loaded once when home is git root"
);
}
#[test]
fn discover_skills_global_skills_loaded() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let skill_dir = home.path().join(".agents/skills/global-skill");
std::fs::create_dir_all(&skill_dir).unwrap();
std::fs::write(
skill_dir.join("SKILL.md"),
"---\nname: global-skill\ndescription: A global skill\n---\nGlobal body.\n",
)
.unwrap();
let skills = discover_skills_impl(cwd.path(), Some(home.path()));
assert_eq!(skills.len(), 1);
assert_eq!(skills[0].name, "global-skill");
}
#[test]
fn discover_skills_project_wins_over_global() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let project_skill = cwd.path().join(".agents/skills/shared");
std::fs::create_dir_all(&project_skill).unwrap();
std::fs::write(
project_skill.join("SKILL.md"),
"---\nname: shared\ndescription: from project\n---\nProject body.\n",
)
.unwrap();
let global_skill = home.path().join(".agents/skills/shared");
std::fs::create_dir_all(&global_skill).unwrap();
std::fs::write(
global_skill.join("SKILL.md"),
"---\nname: shared\ndescription: from global\n---\nGlobal body.\n",
)
.unwrap();
let skills = discover_skills_impl(cwd.path(), Some(home.path()));
assert_eq!(skills.len(), 1, "duplicate name should be deduplicated");
assert_eq!(
skills[0].description, "from project",
"project-level should win over global"
);
}
#[test]
fn discover_skills_no_home_dir() {
let cwd = TempDir::new().unwrap();
let skill_dir = cwd.path().join(".agents/skills/local");
std::fs::create_dir_all(&skill_dir).unwrap();
std::fs::write(
skill_dir.join("SKILL.md"),
"---\nname: local\ndescription: Local skill\n---\nBody.\n",
)
.unwrap();
let skills = discover_skills_impl(cwd.path(), None);
assert_eq!(skills.len(), 1);
assert_eq!(skills[0].name, "local");
}
#[test]
fn collect_supporting_files_finds_non_skill_md_files() {
let tmp = TempDir::new().unwrap();
let skill_dir = tmp.path();
// SKILL.md should be excluded.
std::fs::write(skill_dir.join("SKILL.md"), "---\nname: x\n---\n").unwrap();
// A references subdir with files.
let refs = skill_dir.join("references");
std::fs::create_dir_all(&refs).unwrap();
std::fs::write(refs.join("foo.md"), "foo").unwrap();
std::fs::write(refs.join("bar.md"), "bar").unwrap();
// A script at the top level.
std::fs::write(skill_dir.join("setup.sh"), "#!/bin/sh").unwrap();
let files = collect_supporting_files(skill_dir);
let names: Vec<String> = files
.iter()
.map(|p| p.file_name().unwrap().to_string_lossy().into_owned())
.collect();
assert!(
names.contains(&"foo.md".to_owned()),
"missing foo.md: {names:?}"
);
assert!(
names.contains(&"bar.md".to_owned()),
"missing bar.md: {names:?}"
);
assert!(
names.contains(&"setup.sh".to_owned()),
"missing setup.sh: {names:?}"
);
assert!(
!names.contains(&"SKILL.md".to_owned()),
"SKILL.md should be excluded: {names:?}"
);
}
#[test]
fn collect_supporting_files_does_not_descend_into_nested_skills() {
let tmp = TempDir::new().unwrap();
let skill_dir = tmp.path();
std::fs::write(skill_dir.join("SKILL.md"), "---\nname: x\n---\n").unwrap();
std::fs::write(skill_dir.join("helper.sh"), "#!/bin/sh").unwrap();
// A nested subdir that is itself a skill — should not be descended into.
let nested = skill_dir.join("nested-skill");
std::fs::create_dir_all(&nested).unwrap();
std::fs::write(nested.join("SKILL.md"), "---\nname: nested\n---\n").unwrap();
std::fs::write(nested.join("secret.md"), "should not appear").unwrap();
let files = collect_supporting_files(skill_dir);
let names: Vec<String> = files
.iter()
.map(|p| p.file_name().unwrap().to_string_lossy().into_owned())
.collect();
assert!(
names.contains(&"helper.sh".to_owned()),
"missing helper.sh: {names:?}"
);
assert!(
!names.contains(&"secret.md".to_owned()),
"nested skill's files should not appear: {names:?}"
);
}
#[cfg(unix)]
#[test]
fn collect_supporting_files_skips_symlink_cycles() {
let tmp = TempDir::new().unwrap();
let skill_dir = tmp.path();
std::fs::write(skill_dir.join("SKILL.md"), "---\nname: x\n---\n").unwrap();
let refs = skill_dir.join("references");
std::fs::create_dir_all(&refs).unwrap();
let guide = refs.join("guide.md");
std::fs::write(&guide, "guide").unwrap();
std::os::unix::fs::symlink(&refs, refs.join("loop")).unwrap();
let files = collect_supporting_files(skill_dir);
assert_eq!(files, vec![guide]);
}
#[test]
fn discover_skills_populates_supporting_files() {
let tmp = TempDir::new().unwrap();
let cwd = tmp.path();
let skill_dir = cwd.join(".agents/skills/with-refs");
std::fs::create_dir_all(&skill_dir).unwrap();
std::fs::write(
skill_dir.join("SKILL.md"),
"---\nname: with-refs\ndescription: Has refs\n---\nBody.\n",
)
.unwrap();
let refs = skill_dir.join("references");
std::fs::create_dir_all(&refs).unwrap();
std::fs::write(refs.join("guide.md"), "guide content").unwrap();
let skills = discover_skills_impl(cwd, None);
assert_eq!(skills.len(), 1);
assert_eq!(skills[0].name, "with-refs");
assert_eq!(skills[0].supporting_files.len(), 1);
assert!(
skills[0].supporting_files[0].ends_with("references/guide.md"),
"unexpected path: {:?}",
skills[0].supporting_files[0]
);
}
}